|
@@ -0,0 +1,77 @@
|
|
1
|
+import * as esbuild from 'esbuild';
|
|
2
|
+import { readFile } from 'fs/promises';
|
|
3
|
+
|
|
4
|
+const argv = process.argv.slice(2);
|
|
5
|
+const json = await readFile(new URL('package.json', import.meta.url));
|
|
6
|
+const pack = JSON.parse(json);
|
|
7
|
+const production = (argv.indexOf('--dev') < 0);
|
|
8
|
+const minify = (argv.indexOf('--minify') >= 0);
|
|
9
|
+const serve = (argv.indexOf('--serve') >= 0);
|
|
10
|
+
|
|
11
|
+const options = {
|
|
12
|
+ //entryPoints: ['src/main.ts'], // AR.default
|
|
13
|
+ stdin: {
|
|
14
|
+ contents: 'import AR from "./main.ts";\nmodule.exports = AR;',
|
|
15
|
+ resolveDir: 'src',
|
|
16
|
+ sourcefile: '__main.ts',
|
|
17
|
+ },
|
|
18
|
+ bundle: true,
|
|
19
|
+ minify: minify,
|
|
20
|
+ target: ['es2020'],
|
|
21
|
+ format: 'iife',
|
|
22
|
+ globalName: 'AR',
|
|
23
|
+ define: {
|
|
24
|
+ __AR_VERSION__: JSON.stringify(pack.version),
|
|
25
|
+ __AR_DEVELOPMENT_MODE__: String(!production),
|
|
26
|
+ __AR_WEBSITE__: JSON.stringify(pack.homepage),
|
|
27
|
+ },
|
|
28
|
+ legalComments: 'inline',
|
|
29
|
+ banner: { js: generateBanner() },
|
|
30
|
+ footer: { js: serve ? generateLiveReloadCode() : '' },
|
|
31
|
+ outfile: 'www/dist/' + (minify ? 'encantar.min.js' : 'encantar.js'),
|
|
32
|
+ sourcemap: !production && 'linked',
|
|
33
|
+ logLevel: 'info',
|
|
34
|
+};
|
|
35
|
+
|
|
36
|
+if(!serve) {
|
|
37
|
+ await esbuild.build(options);
|
|
38
|
+ process.exit(0);
|
|
39
|
+}
|
|
40
|
+
|
|
41
|
+const ctx = await esbuild.context(options);
|
|
42
|
+await ctx.watch();
|
|
43
|
+await ctx.serve({
|
|
44
|
+ host: '0.0.0.0',
|
|
45
|
+ port: 8000,
|
|
46
|
+ servedir: 'www',
|
|
47
|
+});
|
|
48
|
+
|
|
49
|
+function generateBanner()
|
|
50
|
+{
|
|
51
|
+ const { version, description, homepage, license } = pack;
|
|
52
|
+ const author = pack.author.replace('@', '(at)');
|
|
53
|
+ const year = new Date().getFullYear();
|
|
54
|
+ const date = new Date().toISOString();
|
|
55
|
+
|
|
56
|
+ return [
|
|
57
|
+ `/*!`,
|
|
58
|
+ ` * encantar.js version ${version}`,
|
|
59
|
+ ` * ${description}`,
|
|
60
|
+ ` * Copyright 2022-${year} ${author}`,
|
|
61
|
+ ` * ${homepage}`,
|
|
62
|
+ ` *`,
|
|
63
|
+ ` * @license ${license}`,
|
|
64
|
+ ` * Date: ${date}`,
|
|
65
|
+ `*/`
|
|
66
|
+ ].join('\n');
|
|
67
|
+}
|
|
68
|
+
|
|
69
|
+function generateLiveReloadCode()
|
|
70
|
+{
|
|
71
|
+ return `
|
|
72
|
+ (function liveReload() {
|
|
73
|
+ new EventSource('/esbuild').
|
|
74
|
+ addEventListener('change', () => location.reload());
|
|
75
|
+ })();
|
|
76
|
+ `;
|
|
77
|
+}
|