forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesbuild-extension-common.mts
More file actions
102 lines (93 loc) · 2.75 KB
/
esbuild-extension-common.mts
File metadata and controls
102 lines (93 loc) · 2.75 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/**
* @fileoverview Common build script for extensions.
*/
import path from 'node:path';
import esbuild from 'esbuild';
type BuildOptions = Partial<esbuild.BuildOptions> & {
outdir: string;
};
interface RunConfig {
readonly platform: 'node' | 'browser';
readonly format?: 'cjs' | 'esm';
readonly srcDir: string;
readonly outdir: string;
readonly entryPoints: string[] | Record<string, string> | { in: string; out: string }[];
readonly additionalOptions?: Partial<esbuild.BuildOptions>;
}
function resolveOptions(config: RunConfig, outdir: string): BuildOptions {
const options: BuildOptions = {
platform: config.platform,
bundle: true,
minify: true,
treeShaking: true,
sourcemap: true,
target: ['es2024'],
external: ['vscode'],
format: config.format ?? 'cjs',
entryPoints: config.entryPoints,
outdir,
logOverride: {
'import-is-undefined': 'error',
},
...(config.additionalOptions || {}),
};
if (config.platform === 'node') {
options.mainFields = ['module', 'main'];
} else if (config.platform === 'browser') {
options.mainFields = ['browser', 'module', 'main'];
options.alias = {
'path': 'path-browserify',
};
options.define = {
'process.platform': JSON.stringify('web'),
'process.env': JSON.stringify({}),
'process.env.BROWSER_ENV': JSON.stringify('true'),
};
}
return options;
}
export async function run(config: RunConfig, args: string[], didBuild?: (outDir: string) => unknown): Promise<void> {
let outdir = config.outdir;
const outputRootIndex = args.indexOf('--outputRoot');
if (outputRootIndex >= 0) {
const outputRoot = args[outputRootIndex + 1];
const outputDirName = path.basename(outdir);
outdir = path.join(outputRoot, outputDirName);
}
const resolvedOptions = resolveOptions(config, outdir);
const isWatch = args.indexOf('--watch') >= 0;
if (isWatch) {
if (didBuild) {
resolvedOptions.plugins = [
...(resolvedOptions.plugins || []),
{
name: 'did-build', setup(pluginBuild) {
pluginBuild.onEnd(async result => {
if (result.errors.length > 0) {
return;
}
try {
await didBuild(outdir);
} catch (error) {
console.error('didBuild failed:', error);
}
});
},
}
];
}
const ctx = await esbuild.context(resolvedOptions);
await ctx.watch();
} else {
try {
await esbuild.build(resolvedOptions);
await didBuild?.(outdir);
} catch {
process.exit(1);
}
}
}