-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathesbuild.mjs
79 lines (72 loc) · 2.1 KB
/
esbuild.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import * as esbuild from 'esbuild';
import path from 'path';
import { readFile } from 'fs/promises';
const argv = process.argv.slice(2);
const json = await readFile(new URL('./package.json', import.meta.url), { encoding: 'utf8' });
const pack = JSON.parse(json);
const production = !pack.version.endsWith('-dev');
const minify = (argv.indexOf('--minify') >= 0);
const serve = (argv.indexOf('--serve') >= 0);
const options = {
//entryPoints: ['src/main.ts'], // AR.AR
stdin: {
contents: 'import { AR } from "./main.ts";\nmodule.exports = AR;',
resolveDir: 'src',
sourcefile: 'index.ts',
},
bundle: true,
minify: minify,
target: ['es2020'],
format: 'iife',
globalName: 'AR',
define: {
__AR_VERSION__: JSON.stringify(pack.version),
__AR_WEBSITE__: JSON.stringify(pack.homepage),
},
legalComments: 'inline',
banner: { js: generateBanner() },
footer: { js: serve ? generateLiveReloadCode() : '' },
outfile: 'www/dist/' + (minify ? 'encantar.min.js' : 'encantar.js'),
sourcemap: !production && 'linked',
logLevel: 'info',
};
if(!serve) {
await esbuild.build(options);
process.exit(0);
}
const ctx = await esbuild.context(options);
await ctx.watch();
await ctx.serve({
host: '0.0.0.0',
port: 8000,
servedir: 'www',
keyfile: path.join(import.meta.dirname, '.local-server.key'),
certfile: path.join(import.meta.dirname, '.local-server.cert'),
});
function generateBanner()
{
const { version, description, homepage, license } = pack;
const author = pack.author.replace('@', '(at)');
const year = new Date().getFullYear();
const date = new Date().toISOString();
return [
`/*!`,
` * encantar.js version ${version}`,
` * ${description}`,
` * Copyright 2022-${year} ${author}`,
` * ${homepage}`,
` *`,
` * @license ${license}`,
` * Date: ${date}`,
`*/`
].join('\n');
}
function generateLiveReloadCode()
{
return `
(function liveReload() {
new EventSource('/esbuild').
addEventListener('change', () => location.reload());
})();
`;
}