forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 441
/
esbuild-webview-common.js
90 lines (80 loc) · 2.43 KB
/
esbuild-webview-common.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// @ts-check
/**
* @fileoverview Common build script for extension scripts used in in webviews.
*/
const path = require('path');
const esbuild = require('esbuild');
/**
* @typedef {Partial<import('esbuild').BuildOptions> & {
* entryPoints: string[] | Record<string, string> | { in: string, out: string }[];
* outdir: string;
* }} BuildOptions
*/
/**
* Build the source code once using esbuild.
*
* @param {BuildOptions} options
* @param {(outDir: string) => unknown} [didBuild]
*/
async function build(options, didBuild) {
await esbuild.build({
bundle: true,
minify: true,
sourcemap: false,
format: 'esm',
platform: 'browser',
target: ['es2020'],
...options,
});
await didBuild?.(options.outdir);
}
/**
* Build the source code once using esbuild, logging errors instead of throwing.
*
* @param {BuildOptions} options
* @param {(outDir: string) => unknown} [didBuild]
*/
async function tryBuild(options, didBuild) {
try {
await build(options, didBuild);
} catch (err) {
console.error(err);
}
}
/**
* @param {{
* srcDir: string;
* outdir: string;
* entryPoints: string[] | Record<string, string> | { in: string, out: string }[];
* additionalOptions?: Partial<import('esbuild').BuildOptions>
* }} config
* @param {string[]} args
* @param {(outDir: string) => unknown} [didBuild]
*/
module.exports.run = async function (config, args, didBuild) {
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);
}
/** @type {BuildOptions} */
const resolvedOptions = {
entryPoints: config.entryPoints,
outdir,
...(config.additionalOptions || {}),
};
const isWatch = args.indexOf('--watch') >= 0;
if (isWatch) {
await tryBuild(resolvedOptions, didBuild);
const watcher = require('@parcel/watcher');
watcher.subscribe(config.srcDir, () => tryBuild(resolvedOptions, didBuild));
} else {
return build(resolvedOptions, didBuild).catch(() => process.exit(1));
}
};