-
Notifications
You must be signed in to change notification settings - Fork 692
/
Copy pathesbuild.js
111 lines (102 loc) · 2.67 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
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
// also see https://code.visualstudio.com/api/working-with-extensions/bundling-extension
const assert = require('assert');
const esbuild = require('esbuild');
const polyfillPlugin = require('esbuild-plugin-polyfill-node');
// pass the platform to esbuild as an argument
function getPlatform() {
const args = process.argv.slice(2);
const pArg = args.find(arg => arg.startsWith('--platform='));
if (pArg) {
return pArg.split('=')[1];
}
throw new Error('No platform specified. Pass --platform <web|node>.');
}
const platform = getPlatform();
assert(['web', 'node'].includes(platform), 'Platform must be "web" or "node".');
const production = process.argv.includes('--production');
const watch = process.argv.includes('--watch');
const config = {
web: {
platform: 'browser',
format: 'cjs',
outfile: `out/bundles/extension-web.js`,
plugins: [
polyfillPlugin.polyfillNode({
// Options (optional)
}),
{
name: 'path-browserify',
setup(build) {
build.onResolve({ filter: /^path$/ }, args => {
return { path: require.resolve('path-browserify') };
});
},
},
{
name: 'wikilink-embed',
setup(build) {
build.onResolve({ filter: /wikilink-embed/ }, args => {
return {
path: require.resolve(
args.resolveDir + '/wikilink-embed-web-extension.ts'
),
};
});
},
},
],
},
node: {
platform: 'node',
format: 'cjs',
outfile: `out/bundles/extension-node.js`,
plugins: [],
},
};
async function main() {
const ctx = await esbuild.context({
...config[platform],
entryPoints: ['src/extension.ts'],
bundle: true,
minify: production,
sourcemap: !production,
sourcesContent: false,
external: ['vscode'],
logLevel: 'silent',
plugins: [
...config[platform].plugins,
/* add to the end of plugins array */
esbuildProblemMatcherPlugin,
],
});
if (watch) {
await ctx.watch();
} else {
await ctx.rebuild();
await ctx.dispose();
}
}
/**
* @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');
});
},
};
main().catch(e => {
console.error(e);
process.exit(1);
});