forked from devcontainers/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.js
94 lines (86 loc) · 2.98 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
// @ts-check
const esbuild = require('esbuild');
const fs = require('fs');
const path = require('path');
const uri = require('vscode-uri');
const minify = process.argv.indexOf('--production') !== -1;
const watch = process.argv.indexOf('--watch') !== -1;
(async () => {
/** @type esbuild.Plugin */
const plugin = {
name: 'patch up',
setup(build) {
build.onLoad({ filter: /node_modules[\/\\]yargs[\/\\]lib[\/\\]platform-shims[\/\\]esm\.mjs$/ }, async (args) => {
let text = await fs.promises.readFile(args.path, 'utf8');
let fileUri = uri.URI.file(args.path);
fileUri = fileUri.with({
path: path.posix.join('/C:', fileUri.path)
});
return {
contents: text.replace(/import\.meta\.url/g, `'${fileUri.toString()}'`),
loader: 'js',
};
});
build.onLoad({ filter: /node_modules[\/\\]vm2[\/\\]lib[\/\\]vm.js$/ }, async (args) => {
const text = await fs.promises.readFile(args.path, 'utf8');
const regex = /fs\.readFileSync\(`\$\{__dirname\}\/([^`]+)`, 'utf8'\)/g;
const files = await [...new Set(text.matchAll(regex))]
.reduce(async (prevP, m) => {
const text = (await fs.promises.readFile(path.join(path.dirname(args.path), m[1]), 'utf8'));
const prev = await prevP;
prev[m[1]] = text;
return prev;
}, Promise.resolve({}));
const contents = text.replace(regex, (_sub, file) => {
return `\`${files[file].replace(/[`$]/g, '\\$&')}\``;
});
return {
contents,
loader: 'js',
};
});
// Work around https://github.com/TooTallNate/node-pac-proxy-agent/issues/21.
build.onLoad({ filter: /node_modules[\/\\]ftp[\/\\]lib[\/\\]connection.js$/ }, async (args) => {
const text = await fs.promises.readFile(args.path, 'utf8');
return {
contents: text.replace(/\bnew Buffer\(/g, 'Buffer.from('),
loader: 'js',
};
});
},
};
/** @type {import('esbuild').BuildOptions} */
const options = {
bundle: true,
sourcemap: true,
minify,
platform: 'node',
target: 'node14.17.0',
external: ['vscode-dev-containers'],
mainFields: ['module', 'main'],
outdir: 'dist',
plugins: [plugin],
banner: {
js: `
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
`.trimStart()
},
entryPoints: [
'./src/spec-node/devContainersSpecCLI.ts',
],
tsconfig: 'tsconfig.json',
outbase: 'src',
};
if (watch) {
(await esbuild.context(options))
.watch();
} else {
await esbuild.build(options);
}
})().catch(() => process.exit(1));