-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.js
50 lines (46 loc) · 1.53 KB
/
esbuild.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
import * as esbuild from 'esbuild'
import { readFile } from 'fs/promises';
import * as path from 'path';
const cssResolvePlugin = {
name: 'cssresolve',
setup(build) {
build.onResolve({ filter: /.*/ }, args => {
if (args.kind == 'url-token')
return { path: path.join(args.resolveDir, args.path) , namespace: 'cssresolveNS' }
return null;
})
build.onLoad({ filter: /.*/, namespace: 'cssresolveNS' }, async args => {
const css = await readFile(args.path, 'utf8');
return { contents:css, loader: 'base64' };
})
}
}
const cssConstructStylesheetPlugin = {
name: 'css imports',
setup(build) {
build.onLoad({ filter: /\.css$/ }, async (args) => {
if (args.with.type === 'css') {
const result = await esbuild.build({
bundle: true,
entryPoints: [args.path],
minify: build.initialOptions.minify,
plugins: [cssResolvePlugin],
write: false
});
const contents = `
const styles = new CSSStyleSheet();
styles.replaceSync(\`${result.outputFiles[0].text}\`);
export default styles;`;
return { contents, loader: 'js' };
}
});
}
}
await esbuild.build({
entryPoints: ['./dist/index.js'],
bundle: true,
minify: true,
format: 'esm',
outfile: './dist/index-bundle.js',
plugins: [cssConstructStylesheetPlugin],
});