-
-
Notifications
You must be signed in to change notification settings - Fork 172
/
build.js
135 lines (125 loc) · 2.99 KB
/
build.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const { execSync } = require('child_process');
const { context, build } = require('esbuild');
const { polyfillNode } = require('esbuild-plugin-polyfill-node');
/**
* @type {import('esbuild').Plugin}
*/
const esbuildProblemMatcherPlugin = {
name: 'esbuild-problem-matcher',
setup(build) {
build.onStart(() => {
console.log('[watch] build started');
// Run `gulp copy:files` before build
execSync('gulp copy-files');
console.log('[watch] gulp copy-files');
});
build.onEnd((result) => {
if (result.errors.length) {
result.errors.forEach((error) =>
console.error(
`> ${error.location?.file}:${error.location?.line}:${error.location?.column}: error: ${error.text}`,
),
);
} else {
console.log('[watch] build finished');
}
});
},
};
/**
* @type {import('esbuild').BuildOptions}
*/
const nativeConfig = {
entryPoints: ['./src/extension.ts'],
bundle: true,
minify: true,
platform: 'node', // For CJS
outfile: './out/native/extension.js',
target: 'node16',
format: 'cjs',
external: ['vscode'],
};
// FIX:
const defaultDocument = {
readyState: 'ready',
};
const defaultWindow = {
document: {
currentScript: {
dataset: {},
},
},
location: {
protocol: 'https:',
},
less: {
onReady: false,
async: false,
},
};
/**
* @type {import('esbuild').BuildOptions}
*/
const webConfig = {
entryPoints: ['./src/extension-web.ts'],
bundle: true,
minify: true,
platform: 'browser', // For ESM
outfile: './out/web/extension.js',
target: 'es2020',
format: 'cjs',
external: ['vscode'],
plugins: [
polyfillNode({
polyfills: {
fs: true,
},
globals: {
// global: true,
},
}),
],
define: {
// eslint-disable-next-line @typescript-eslint/naming-convention
// window: 'globalThis',
// global: 'globalThis',
// window: "globalThis",
'window': JSON.stringify(defaultWindow),
// document: JSON.stringify(defaultDocument),
'process.env.IS_VSCODE_WEB_EXTENSION': '"true"',
},
};
async function main() {
try {
// Watch mode
if (process.argv.includes('--watch')) {
// Native
const nativeContext = await context({
...nativeConfig,
sourcemap: true,
minify: false,
plugins: [esbuildProblemMatcherPlugin, ...(nativeConfig.plugins ?? [])],
});
// Web
const webContext = await context({
...webConfig,
sourcemap: true,
minify: false,
define: {
...(webConfig.define ?? {}),
...{
'process.env.IS_VSCODE_WEB_EXTENSION_DEV_MODE': '"true"',
},
},
plugins: [esbuildProblemMatcherPlugin, ...(webConfig.plugins ?? [])],
});
await Promise.all([nativeContext.watch(), webContext.watch()]);
} else {
// Build mode
await Promise.all([build(nativeConfig), build(webConfig)]);
}
} catch (error) {
console.error(error);
}
}
main();