-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy path.esbuild.ts
More file actions
436 lines (389 loc) · 13.1 KB
/
.esbuild.ts
File metadata and controls
436 lines (389 loc) · 13.1 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as watcher from '@parcel/watcher';
import * as esbuild from 'esbuild';
import * as fs from 'fs';
import { copyFile, mkdir, readdir, rename } from 'fs/promises';
import { glob } from 'glob';
import * as path from 'path';
const REPO_ROOT = import.meta.dirname;
const isWatch = process.argv.includes('--watch');
const isDev = process.argv.includes('--dev');
const isPreRelease = process.argv.includes('--prerelease');
const generateSourceMaps = process.argv.includes('--sourcemaps');
const sourceMapOutDir = './dist-sourcemaps';
const baseBuildOptions = {
bundle: true,
logLevel: 'info',
minify: !isDev,
outdir: './dist',
// In dev mode, use linked source maps for debugging.
// With --sourcemaps flag, generate external source maps (no sourceMappingURL comment in output).
sourcemap: isDev ? 'linked' : (generateSourceMaps ? 'external' : false),
sourcesContent: false,
treeShaking: true
} satisfies esbuild.BuildOptions;
const baseNodeBuildOptions = {
...baseBuildOptions,
external: [
'./package.json',
'./.vscode-test.mjs',
'playwright',
'keytar',
'@azure/functions-core',
'applicationinsights-native-metrics',
'@opentelemetry/instrumentation',
'@azure/opentelemetry-instrumentation-azure-sdk',
'electron', // this is for simulation workbench,
'sqlite3',
'node-pty', // Required by @github/copilot
'@github/copilot',
...(isDev ? [] : ['dotenv', 'source-map-support'])
],
platform: 'node',
mainFields: ["module", "main"], // needed for jsonc-parser,
define: {
'process.env.APPLICATIONINSIGHTS_CONFIGURATION_CONTENT': JSON.stringify(JSON.stringify({
proxyHttpUrl: "",
proxyHttpsUrl: ""
}))
},
} satisfies esbuild.BuildOptions;
const webviewBuildOptions = {
...baseBuildOptions,
platform: 'browser',
target: 'es2024', // Electron 34 -> Chrome 132 -> ES2024
entryPoints: [
{ in: 'src/extension/completions-core/vscode-node/extension/src/copilotPanel/webView/suggestionsPanelWebview.ts', out: 'suggestionsPanelWebview' },
],
} satisfies esbuild.BuildOptions;
const nodeExtHostTestGlobs = [
'src/**/vscode/**/*.test.{ts,tsx}',
'src/**/vscode-node/**/*.test.{ts,tsx}',
// deprecated
'src/extension/**/*.test.{ts,tsx}'
];
const testBundlePlugin: esbuild.Plugin = {
name: 'testBundlePlugin',
setup(build) {
build.onResolve({ filter: /[\/\\]test-extension\.ts$/ }, args => {
if (args.kind !== 'entry-point') {
return;
}
return { path: path.resolve(args.path) };
});
build.onLoad({ filter: /[\/\\]test-extension\.ts$/ }, async args => {
let files = await glob(nodeExtHostTestGlobs, { cwd: REPO_ROOT, posix: true, ignore: ['src/extension/completions-core/**/*'] });
files = files.map(f => path.posix.relative('src', f));
if (files.length === 0) {
throw new Error('No extension tests found');
}
return {
contents: files
.map(f => `require('./${f}');`)
.join(''),
watchDirs: files.map(path.dirname),
watchFiles: files,
};
});
}
};
const nodeExtHostSanityTestGlobs = [
'src/**/vscode-node/**/*.sanity-test.{ts,tsx}',
];
const sanityTestBundlePlugin: esbuild.Plugin = {
name: 'sanityTestBundlePlugin',
setup(build) {
build.onResolve({ filter: /[\/\\]sanity-test-extension\.ts$/ }, args => {
if (args.kind !== 'entry-point') {
return;
}
return { path: path.resolve(args.path) };
});
build.onLoad({ filter: /[\/\\]sanity-test-extension\.ts$/ }, async args => {
let files = await glob(nodeExtHostSanityTestGlobs, { cwd: REPO_ROOT, posix: true, ignore: ['src/extension/completions-core/**/*'] });
files = files.map(f => path.posix.relative('src', f));
if (files.length === 0) {
throw new Error('No extension tests found');
}
return {
contents: files
.map(f => `require('./${f}');`)
.join(''),
watchDirs: files.map(path.dirname),
watchFiles: files,
};
});
}
};
const importMetaPlugin: esbuild.Plugin = {
name: 'claudeAgentSdkImportMetaPlugin',
setup(build) {
// Handle import.meta.url in @anthropic-ai/claude-agent-sdk package
build.onLoad({ filter: /node_modules[\/\\]@anthropic-ai[\/\\]claude-agent-sdk[\/\\].*\.mjs$/ }, async (args) => {
const contents = await fs.promises.readFile(args.path, 'utf8');
return {
contents: contents.replace(
/import\.meta\.url/g,
'require("url").pathToFileURL(__filename).href'
),
loader: 'js'
};
});
}
};
const shimVsCodeTypesPlugin: esbuild.Plugin = {
name: 'shimVsCodeTypesPlugin',
setup(build) {
// Create a virtual module that will try to require vscode at runtime
build.onResolve({ filter: /^vscode$/ }, args => {
return {
path: 'vscode-dynamic',
namespace: 'vscode-fallback'
};
});
build.onLoad({ filter: /^vscode-dynamic$/, namespace: 'vscode-fallback' }, () => {
return {
contents: `
let vscode;
// See test/simulationExtension/extension.js for where and why this is created.
if (typeof COPILOT_SIMULATION_VSCODE !== 'undefined') {
vscode = COPILOT_SIMULATION_VSCODE;
} else {
try {
vscode = eval('require(' + JSON.stringify('vscode') + ')');
} catch (e) {
vscode = require('./src/util/common/test/shims/vscodeTypesShim.ts');
}
}
module.exports = vscode;
`,
resolveDir: REPO_ROOT
};
});
}
};
const nodeExtHostBuildOptions = {
...baseNodeBuildOptions,
entryPoints: [
{ in: './src/extension/extension/vscode-node/extension.ts', out: 'extension' },
{ in: './src/platform/parser/node/parserWorker.ts', out: 'worker2' },
{ in: './src/platform/tokenizer/node/tikTokenizerWorker.ts', out: 'tikTokenizerWorker' },
{ in: './src/platform/diff/node/diffWorkerMain.ts', out: 'diffWorker' },
{ in: './src/platform/tfidf/node/tfidfWorker.ts', out: 'tfidfWorker' },
{ in: './src/extension/onboardDebug/node/copilotDebugWorker/index.ts', out: 'copilotDebugCommand' },
{ in: './src/extension/chatSessions/vscode-node/copilotCLIShim.ts', out: 'copilotCLIShim' },
{ in: './src/test-extension.ts', out: 'test-extension' },
{ in: './src/sanity-test-extension.ts', out: 'sanity-test-extension' },
],
loader: { '.ps1': 'text' },
plugins: [testBundlePlugin, sanityTestBundlePlugin, importMetaPlugin],
external: [
...baseNodeBuildOptions.external,
'vscode'
]
} satisfies esbuild.BuildOptions;
const webExtHostBuildOptions = {
...baseBuildOptions,
platform: 'browser',
entryPoints: [
{ in: './src/extension/extension/vscode-worker/extension.ts', out: 'web' },
],
format: 'cjs', // Necessary to export activate function from bundle for extension
external: [
'vscode',
'http',
]
} satisfies esbuild.BuildOptions;
const nodeExtHostSimulationTestOptions = {
...nodeExtHostBuildOptions,
outdir: '.vscode/extensions/test-extension/dist',
entryPoints: [
{ in: '.vscode/extensions/test-extension/main.ts', out: './simulation-extension' }
]
} satisfies esbuild.BuildOptions;
const nodeSimulationBuildOptions = {
...baseNodeBuildOptions,
entryPoints: [
{ in: './test/simulationMain.ts', out: 'simulationMain' },
],
plugins: [testBundlePlugin, shimVsCodeTypesPlugin],
external: [
...baseNodeBuildOptions.external,
]
} satisfies esbuild.BuildOptions;
const nodeSimulationWorkbenchUIBuildOptions = {
...baseNodeBuildOptions,
platform: 'browser', // @ulugbekna: important to target 'browser' for correct bundling using 'window'
mainFields: ["browser", "module", "main"],
entryPoints: [
{ in: './test/simulation/workbench/simulationWorkbench.tsx', out: 'simulationWorkbench' },
],
alias: {
'vscode': './src/util/common/test/shims/vscodeTypesShim.ts'
},
external: [
...baseNodeBuildOptions.external,
'../../node_modules/monaco-editor/*',
// @ulugbekna: libs provided by node that need to be specified manually because of 'platform' is set to 'browser'
'fs',
'path',
'readline',
'child_process',
'http',
'assert',
],
} satisfies esbuild.BuildOptions;
async function typeScriptServerPluginPackageJsonInstall(): Promise<void> {
await mkdir('./node_modules/@vscode/copilot-typescript-server-plugin', { recursive: true });
const source = path.join(import.meta.dirname, './src/extension/typescriptContext/serverPlugin/package.json');
const destination = path.join(import.meta.dirname, './node_modules/@vscode/copilot-typescript-server-plugin/package.json');
try {
await copyFile(source, destination);
} catch (error) {
console.error('Error copying package.json:', error);
}
}
const typeScriptServerPluginBuildOptions = {
bundle: true,
format: 'cjs',
// keepNames: true,
logLevel: 'info',
minify: !isDev,
outdir: './node_modules/@vscode/copilot-typescript-server-plugin/dist',
platform: 'node',
sourcemap: isDev ? 'linked' : false,
sourcesContent: false,
treeShaking: true,
external: [
"typescript",
"typescript/lib/tsserverlibrary"
],
entryPoints: [
{ in: './src/extension/typescriptContext/serverPlugin/src/node/main.ts', out: 'main' },
]
} satisfies esbuild.BuildOptions;
/**
* Moves all .map files from the output directories to a separate source maps directory.
* This keeps source maps out of the packaged extension while making them available for upload.
*/
async function moveSourceMapsToSeparateDir(): Promise<void> {
if (!generateSourceMaps) {
return;
}
const outputDirs = [
'./dist',
'./node_modules/@vscode/copilot-typescript-server-plugin/dist',
];
await mkdir(sourceMapOutDir, { recursive: true });
for (const dir of outputDirs) {
try {
const files = await readdir(dir);
for (const file of files) {
if (file.endsWith('.map')) {
const sourcePath = path.join(dir, file);
// Prefix with directory name to avoid collisions
const prefix = dir === './dist' ? '' : 'ts-plugin-';
const destPath = path.join(sourceMapOutDir, prefix + file);
await rename(sourcePath, destPath);
console.log(`Moved source map: ${sourcePath} -> ${destPath}`);
}
}
} catch (error) {
// Directory might not exist in some build configurations
console.warn(`Could not process directory ${dir}:`, error);
}
}
}
async function main() {
if (!isDev) {
applyPackageJsonPatch(isPreRelease);
}
await typeScriptServerPluginPackageJsonInstall();
if (isWatch) {
const contexts: esbuild.BuildContext[] = [];
const nodeExtHostContext = await esbuild.context(nodeExtHostBuildOptions);
contexts.push(nodeExtHostContext);
const webExtHostContext = await esbuild.context(webExtHostBuildOptions);
contexts.push(webExtHostContext);
const nodeSimulationContext = await esbuild.context(nodeSimulationBuildOptions);
contexts.push(nodeSimulationContext);
const nodeSimulationWorkbenchUIContext = await esbuild.context(nodeSimulationWorkbenchUIBuildOptions);
contexts.push(nodeSimulationWorkbenchUIContext);
const nodeExtHostSimulationContext = await esbuild.context(nodeExtHostSimulationTestOptions);
contexts.push(nodeExtHostSimulationContext);
const typeScriptServerPluginContext = await esbuild.context(typeScriptServerPluginBuildOptions);
contexts.push(typeScriptServerPluginContext);
let debounce: NodeJS.Timeout | undefined;
const rebuild = async () => {
if (debounce) {
clearTimeout(debounce);
}
debounce = setTimeout(async () => {
console.log('[watch] build started');
for (const ctx of contexts) {
try {
await ctx.cancel();
await ctx.rebuild();
} catch (error) {
console.error('[watch]', error);
}
}
console.log('[watch] build finished');
}, 100);
};
watcher.subscribe(REPO_ROOT, (err, events) => {
for (const event of events) {
console.log(`File change detected: ${event.path}`);
}
rebuild();
}, {
ignore: [
`**/.git/**`,
`**/.simulation/**`,
`**/test/outcome/**`,
`.vscode-test/**`,
`**/.venv/**`,
`**/dist/**`,
`**/node_modules/**`,
`**/*.txt`,
`**/baseline.json`,
`**/baseline.old.json`,
`**/*.w.json`,
'**/*.sqlite',
'**/*.sqlite-journal',
'test/aml/out/**'
]
});
rebuild();
} else {
await Promise.all([
esbuild.build(nodeExtHostBuildOptions),
esbuild.build(webExtHostBuildOptions),
esbuild.build(nodeSimulationBuildOptions),
esbuild.build(nodeSimulationWorkbenchUIBuildOptions),
esbuild.build(nodeExtHostSimulationTestOptions),
esbuild.build(typeScriptServerPluginBuildOptions),
esbuild.build(webviewBuildOptions),
]);
// Move source maps to separate directory so they're not packaged with the extension
await moveSourceMapsToSeparateDir();
}
}
function applyPackageJsonPatch(isPreRelease: boolean) {
const packagejsonPath = path.join(import.meta.dirname, './package.json');
const json = JSON.parse(fs.readFileSync(packagejsonPath).toString());
const newProps: any = {
buildType: 'prod',
isPreRelease,
};
const patchedPackageJson = Object.assign(json, newProps);
// Remove fields which might reveal our development process
delete patchedPackageJson['scripts'];
delete patchedPackageJson['devDependencies'];
delete patchedPackageJson['dependencies'];
fs.writeFileSync(packagejsonPath, JSON.stringify(patchedPackageJson));
}
main();