forked from SAP/open-ux-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuildConfig.js
117 lines (109 loc) · 3.82 KB
/
esbuildConfig.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
105
106
107
108
109
110
111
112
113
114
115
116
117
const { sassPlugin, postcssModules } = require('esbuild-sass-plugin');
const autoprefixer = require('autoprefixer');
const postcss = require('postcss');
const yargsParser = require('yargs-parser');
const { writeFileSync } = require('fs');
const { resolve, join } = require('path');
// from https://github.com/bvaughn/react-virtualized/issues/1212#issuecomment-847759202 workaround for https://github.com/bvaughn/react-virtualized/issues/1632 until it is released.
const resolveFixup = {
name: 'resolve-fixup',
setup(build) {
build.onResolve({ filter: /react-virtualized/ }, async (args) => {
return {
path: require.resolve('react-virtualized/dist/umd/react-virtualized.js')
};
});
}
};
const commonConfig = {
write: true,
bundle: true,
metafile: true,
sourcemap: true,
minify: true,
logLevel: 'warning',
loader: {
'.jpg': 'file',
'.gif': 'file',
'.mp4': 'file',
'.graphql': 'text',
'.png': 'file',
'.svg': 'file'
},
external: [],
plugins: []
};
const transformModule = postcssModules({});
const browserConfig = {
entryPoints: {
index: 'src/index.ts',
bundle: 'src/webview/index.tsx'
},
mainFields: ['browser', 'module', 'main'],
outdir: './dist',
platform: 'browser',
target: 'chrome90',
format: 'iife',
plugins: [
resolveFixup,
sassPlugin({
async transform(source, dirname, path) {
if (path.endsWith('.module.scss')) {
return transformModule.apply(this, [source, dirname, path]);
}
const { css } = await postcss([autoprefixer]).process(source);
return css;
}
})
]
};
const handleCliParams = (options, args = []) => {
const outOptions = { ...options };
const yargs = yargsParser(args);
outOptions.minify = yargs.minify ? true : outOptions.minify;
outOptions.minify = yargs.minify === 'false' ? false : outOptions.minify;
outOptions.watch = yargs.watch ? true : outOptions.watch;
outOptions.watch = yargs.watch === 'false' ? false : outOptions.watch;
outOptions.metafile = yargs.metafile ? true : outOptions.metafile;
outOptions.metafile = yargs.metafile === 'false' ? false : outOptions.metafile;
outOptions.sourcemap = yargs.sourcemap !== undefined ? yargs.sourcemap : outOptions.sourcemap;
return outOptions;
};
const build = (options, args) => {
const finalConfig = handleCliParams(options, args);
if (finalConfig.watch) {
// needed by https://github.com/connor4312/esbuild-problem-matchers if installed in vscode
finalConfig.watch = {
onRebuild(error, result) {
console.log('[watch] build started');
if (error) {
error.errors.forEach((error) =>
console.error(
`> ${error.location.file}:${error.location.line}:${error.location.column}: error: ${error.text}`
)
);
} else console.log('[watch] build finished');
}
};
}
require('esbuild')
.build(finalConfig)
.then((result) => {
if (finalConfig.metafile) {
const statsFile = 'esbuild-stats.json';
writeFileSync(statsFile, JSON.stringify(result.metafile));
console.log(`Wrote esbuild stats file ${statsFile}. Analyse at https://bundle-buddy.com/esbuild/`);
}
})
.then(() => {
console.log('[watch] build finished');
})
.catch((error) => {
console.log(error.message);
process.exit(1);
});
};
module.exports = {
esbuildOptionsBrowser: { ...commonConfig, ...browserConfig },
build
};