-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
build.js
104 lines (94 loc) · 2.39 KB
/
build.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// prettier-ignore
const { promises: { readFile } } = require('fs');
const { build, context } = require('esbuild');
const { sassPlugin } = require('esbuild-sass-plugin');
const DEV = process.argv.includes('--dev');
const CSS = process.argv.includes('--css');
/** @type {import('esbuild').BuildOptions} */
const esbuildBase = {
entryPoints: ['./src/index.js'],
format: 'esm',
bundle: true,
minify: true,
platform: 'browser',
charset: 'utf8',
sourcemap: 'external',
target: ['es2020', 'edge79', 'firefox67', 'chrome64', 'safari11.1'],
};
/** @type {import('esbuild').Plugin} */
const kayrosFixPlugin = {
name: 'kayros-fix-plugin',
setup (build) {
build.onLoad({ filter: /kayros\.min\.css$/ }, async (args) => {
const fileContents = await readFile(args.path, 'utf8');
/**
* @aegis-framework/kayros.css contains an invalid css, let's fix that
*/
const modifiedContents = fileContents.replace('*zoom:1', 'zoom:1');
return {
contents: modifiedContents,
loader: 'css',
};
});
},
};
async function main () {
/** @type {Record<string, Partial<import('esbuild').BuildOptions>>} */
const builds = {
esm: {
format: 'esm',
outfile: './lib/monogatari.module.js',
sourcemap: 'linked',
},
cjs: {
format: 'cjs',
outfile: './lib/monogatari.node.js',
sourcemap: 'linked',
define: {
'import.meta.url': 'location.origin',
},
},
iife: {
entryPoints: ['./src/browser.js'],
format: 'iife',
outfile: './dist/engine/core/monogatari.js',
globalName: 'MonogatariGlobal',
define: {
'import.meta.url': 'location.origin',
},
},
debug: {
entryPoints: ['./debug/index.js'],
format: 'iife',
outfile: './dist/engine/debug/debug.js',
globalName: 'MonogatariDebug',
},
css: {
entryPoints: ['./src/index.css'],
outfile: './dist/engine/core/monogatari.css',
plugins: [kayrosFixPlugin, sassPlugin()],
},
};
if (DEV) {
const ctx = await context({
...esbuildBase,
...builds.iife,
entryPoints: [!CSS ? './src/browser.js' : './src/index.css'],
outfile: !CSS
? './dist/engine/core/monogatari.js'
: './dist/engine/core/monogatari.css',
plugins: [kayrosFixPlugin, sassPlugin()],
legalComments: 'none',
});
await ctx.watch();
if (!CSS) await ctx.serve({ servedir: './dist', port: 5173 });
} else {
for (const key in builds) {
await build({
...esbuildBase,
...builds[key],
});
}
}
}
main();