-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
67 lines (63 loc) · 1.76 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
const fs = require('fs');
const {version} = require('./package.json');
const StyleLoader = {
name: 'inline-style',
setup({onLoad}) {
const template = css =>
// eslint-disable-next-line
`typeof document<'u'&&` +
// eslint-disable-next-line
`document.head.appendChild(document.createElement('style'))` +
`.appendChild(document.createTextNode(${JSON.stringify(css)}))`;
onLoad({filter: /\.css$/}, async (args) => {
const css = await fs.promises.readFile(args.path, 'utf8');
return {contents: template(css.replace(/\s+/g, ' '))};
});
},
};
const options = {
entryPoints: ['src/index-themed.js'],
outfile: 'dist/jcode-md.js',
bundle: true,
loader: {
'.png': 'base64',
'.svg': 'base64',
'.woff': 'file',
'.ttf': 'file',
'.woff2': 'file',
},
define: {
VERSION: `"${version}"`,
},
plugins: [StyleLoader],
};
if(process.env.mode === 'production') {
require('esbuild').build({minify: true, ...options});
require('esbuild').build({
minify: true,
...options,
entryPoints: ['src/jcode-md.js'],
outfile: 'dist/jcode-md-pure.js',
});
require('esbuild').build({
...options,
format: 'esm',
entryPoints: ['src/jcode-md-themed.js'],
outfile: 'dist/jcode-md.esm.js',
});
require('esbuild').build({
...options,
format: 'esm',
entryPoints: ['src/jcode-md.js'],
outfile: 'dist/jcode-md-pure.esm.js',
});
} else {
require('esbuild').serve({
servedir: '.',
}, options).then((server) => {
console.log(`Server is running at ${server.host}:${server.port}`);
const scriptURL = `http://localhost:${server.port}/${options.outfile}`;
console.log(`打开 https://code.juejin.cn
设置 ${scriptURL} 到 script 依赖资源,进行调试。`);
});
}