-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.mjs
85 lines (73 loc) · 1.87 KB
/
build.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
80
81
82
83
84
import * as esbuild from 'esbuild'
import * as path from 'path'
import Module from 'module'
const buildWithESBuildAndEval = async(filepath) => {
const result = await esbuild.build({
entryPoints: [filepath],
bundle: true,
format: 'cjs',
target: [`node12`],
write: false,
})
const builtModuleAsString = new TextDecoder("utf-8").decode(result.outputFiles[0].contents)
var m = new Module();
m._compile(builtModuleAsString, '');
return m.exports;
}
/** This hook would run ".preprocess.ts" files at build time and
* include the result instead of the source files. */
const preprocessPlugin = {
name: 'preprocess',
setup(build) {
build.onLoad({ filter: /\.preprocess(\.js|\.ts|)$/ }, async (args) =>{
const module = await buildWithESBuildAndEval(args.path);
let contents = ''
for(let [key, value] of Object.entries(module)){
contents+=`export ${key === 'default' ? 'default' : 'const ' + key + ' ='} ${JSON.stringify(value)}\n`
}
return { contents, loader: "js" };
})
},
}
// css
esbuild.build({
entryPoints: ['./src/index.css'],
bundle: true,
outfile: 'dist/runcss.css',
})
esbuild.build({
entryPoints: ['./src/index.css'],
bundle: true,
minify: true,
outfile: 'dist/runcss.min.css',
})
// iife script
esbuild.build({
entryPoints: ['./src/iife.js'],
bundle: true,
outfile: 'dist/runcss.js',
plugins: [preprocessPlugin]
})
esbuild.build({
entryPoints: ['./src/iife.js'],
bundle: true,
minify: true,
outfile: 'dist/runcss.min.js',
plugins: [preprocessPlugin]
})
// module
esbuild.build({
entryPoints: ['./src/index.js'],
bundle: true,
format: 'esm',
outfile: 'dist/runcss.mjs',
plugins: [preprocessPlugin]
})
esbuild.build({
entryPoints: ['./src/index.js'],
bundle: true,
minify: true,
format: 'esm',
outfile: 'dist/runcss.min.mjs',
plugins: [preprocessPlugin]
})