-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.js
55 lines (46 loc) · 1.44 KB
/
serve.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { createRequire } from 'module';
import browserSync from "browser-sync";
import chokidar from 'chokidar';
// Alternative for __require from CommonJS:
const require = createRequire(import.meta.url);
// Importing blazingly fast JS bundler and builder on commonJS manner:
const esbuild = require('esbuild');
// Web-server instance:
const bs = browserSync.create();
// Run web-server:
bs.init({
files: ['web*/'],
watch: true,
server: {
baseDir: 'web',
index: 'index.html'
},
port: 3000,
online: false, // don't use Internet (usefull for site testing on several devices)
notify: false, // don't notify about site updates
open: true, // open default browser automatically
ui: false // turn off web ui to rule browser-sync
});
// function to build and bundle JS:
function buildJS(){
try{
esbuild.build({
entryPoints: ['web/scripts/script.js',],
bundle: true,
minify: true,
sourcemap: true,
target: 'es6',
charset: 'utf8',
outfile: 'web/script.js'
})
} catch(err){
console.error(err);
}
}
// Build and bundle JS on running this script:
buildJS();
// Watch for changes of JS files and rebuild and rebundle them automatically:
const watcher = chokidar.watch('web/scripts/script.js');
watcher.on('change', function(){
buildJS()
})