-
Notifications
You must be signed in to change notification settings - Fork 433
/
bundle.mjs
59 lines (51 loc) · 1.59 KB
/
bundle.mjs
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
56
57
58
59
import * as esbuild from 'esbuild';
import fs from 'fs';
const plugins = [{
name: 'my-plugin',
setup(build) {
let count = 0;
build.onEnd(({ errors, warnings }) => {
count++;
const message = errors.length === 0 && warnings.length === 0
? 'Build completed.'
: `Build completed with ${errors.length} error(s) and ${warnings.length} warning(s).`;
console.log(`[BUILD #${count.toString().padStart(3, '0')}]:`, message); });
},
}];
const buildOptions = {
bundle: true,
entryPoints: ['./src/js/L.PM.js'],
loader: {
'.js': 'jsx',
'.css': 'css',
'.svg': 'dataurl' },
minify: true,
outfile: './dist/leaflet-geoman.js',
sourcemap: true,
}
const ctx = await esbuild.context({ ...buildOptions, plugins });
if (process.env.DEV) {
// Watch in dev mode
await ctx.watch();
console.log('watching...');
const { host, port } = await ctx.serve({
port: 5500,
servedir: '.',
fallback: "./index.html"
});
console.log(`Serving app at http://${host}:${port}/demo`);
} else {
// Clean /dist folder
fs.rmSync("./dist", { recursive: true, force: true });
// Build
await ctx.rebuild();
// Dispose context
ctx.dispose();
// Replace incorrect closing tag in <\/style>
const data = fs.readFileSync('./dist/leaflet-geoman.css', 'utf8');
const result = data.replace(/<\\\/style>/g, '</style>');
fs.writeFileSync('./dist/leaflet-geoman.css', result, 'utf8');
// Copy types
fs.copyFileSync('leaflet-geoman.d.ts', './dist/leaflet-geoman.d.ts');
fs.copyFileSync('./dist/leaflet-geoman.js', './dist/leaflet-geoman.min.js');
}