-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesbuild.mjs
139 lines (123 loc) · 4.16 KB
/
esbuild.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import * as esbuild from 'esbuild';
import fs from 'fs';
const watch = process.argv.includes('--watch');
const production = process.argv.includes('--production');
const metafile = process.argv.includes('--metafile');
/**
* @type {import('esbuild').Plugin}
*/
const esbuildProblemMatcherPlugin = {
name: 'esbuild-problem-matcher',
setup(build) {
build.onStart(() => {
console.log('[watch] build started');
});
build.onEnd(result => {
result.errors.forEach(({ text, location }) => {
console.error(`✘ [ERROR] ${text}`);
console.error(` ${location.file}:${location.line}:${location.column}:`);
});
console.log('[watch] build finished');
});
},
};
const browsers = ['chrome114', 'firefox113', 'safari16.5', 'edge114'];
// VSCode webview cannot load .map files. See https://github.com/microsoft/vscode/issues/145184
// So we create inline sourcemaps.
// Also we must make sure that sourcesContent is not set to false or the actual source
// will not be included in the inline sourcemap.
const sourcemap = production ? false : 'inline';
const common = {
bundle: true,
// When using the build API, all process.env.NODE_ENV expressions are
// automatically defined to "production" if all minification options are
// enabled and "development" otherwise. This only happens if process,
// process.env, and process.env.NODE_ENV are not already defined.
minify: production,
sourcemap,
logLevel: 'silent', // silent the default logger
plugins: [esbuildProblemMatcherPlugin],
metafile,
};
const viewJs = await esbuild.context({
...common,
entryPoints: ['src/view/webview.tsx'],
outfile: 'dist/webview.js',
target: browsers,
alias: {
path: 'path-browserify',
},
});
const viewCss = await esbuild.context({
...common,
entryPoints: ['src/view/webview.css'],
outfile: 'dist/webview.css',
target: browsers,
loader: { '.ttf': 'file', '.woff2': 'file', '.ttf': 'file' },
});
/** @type {import('esbuild').BuildOptions} */
const extensionJs = await esbuild.context({
...common,
entryPoints: ['src/extension/extension.ts'],
outfile: 'dist/extension.js',
platform: 'node',
format: 'cjs',
external: ['vscode'],
});
if (watch) {
await viewJs.watch();
await viewCss.watch();
await extensionJs.watch();
} else {
const viewJsRes = await viewJs.rebuild();
const viewCssRes = await viewCss.rebuild();
const extensionJsRes = await extensionJs.rebuild();
if (metafile) {
fs.writeFileSync('dist/webview.js.json', JSON.stringify(viewJsRes.metafile));
fs.writeFileSync('dist/webview.css.json', JSON.stringify(viewCssRes.metafile));
fs.writeFileSync('dist/extension.js.json', JSON.stringify(extensionJsRes.metafile));
}
await viewJs.dispose();
await viewCss.dispose();
await extensionJs.dispose();
}
// if (mustWatch) {
// const jsCtx = await esbuild.context({ ...viewJs, plugins: [messagePlugin('JS')] });
// const cssCtx = await esbuild.context({ ...viewCss, plugins: [messagePlugin('CSS')] });
// await jsCtx.watch();
// await cssCtx.watch();
// console.log('\nesbuild is watching javascript and css files...');
// } else {
// await esbuild.build(viewJs);
// await esbuild.build(viewCss);
// }
// function messagePlugin(name) {
// return {
// name: 'Message',
// setup(build) {
// build.onEnd(result => {
// for (const error of result.errors) {
// // console.error(JSON.stringify(error, null, 2));
// console.error(itemToStr(error));
// }
// for (const warning of result.warnings) {
// // console.error(JSON.stringify(warning, null, 2));
// console.error(itemToStr(warning));
// }
// if (result.errors.length === 0 && result.warnings.length === 0) {
// console.log(`${GREEN}esbuild ${name} built successfully.${RESET}`);
// }
// });
// },
// };
// }
// function itemToStr(item) {
// let msg;
// if (item.location) {
// msg = itemLocationToStr(item.location)
// msg = `${item.location.file}:${item.location.line}:${item.location.column} - error esbuild: ${item.text}`;
// } else {
// msg = `Unknown:0:0 - error unknown: ${item.text}`;
// }
// return msg;
// }