Skip to content

Commit 3c0817c

Browse files
authored
fix: make sourcemap renames scope aware (#1827)
Uses Acorn to parse compiled files and create a tree of scopes. Then, only apply renames in scopes when appropriate. This is far better than what we were doing before when we were just looking for nearby renames. This is not very fast for large bundled files. I initially worked on building a fork of https://github.com/guybedford/es-module-lexer that would scan for blocks. This worked in many cases, but failed for cases when there's no BlockStatement. For example, `for (const foo of bar) foo()` would not detect a 'scope' around `foo()`. This is not great, especially with minified code where such patterns are common. Instead, we'll parse large bundles (>512KB) in a one-off worker_thread. We also some work in our build process to vendor acorn and acorn-loose to avoid duplicating it in the worker thread code.
1 parent 5613912 commit 3c0817c

16 files changed

+553
-394
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ This changelog records changes to stable releases since 1.50.2. "TBA" changes he
44

55
## Nightly (only)
66

7+
- fix: make source map renames scope-aware
78
- fix: error when processing private properties with a map ([#1824](https://github.com/microsoft/vscode-js-debug/issues/1824))
89

910
## v1.83 (September 2023)

gulpfile.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,54 @@ async function getConstantDefines() {
192192
};
193193
}
194194

195+
function compileVendorLibrary(name) {
196+
return {
197+
name,
198+
entryPoints: [require.resolve(name)],
199+
outdir: `${buildSrcDir}/vendor`,
200+
entryNames: `${name}`,
201+
};
202+
}
203+
195204
async function compileTs({
196205
packages = [],
197206
sourcemap = false,
198207
compileInPlace = false,
199208
minify = isWatch ? false : true,
200209
watch = false,
201210
} = options) {
211+
const vendorPrefix = 'vendor';
212+
213+
// don't watch these, they won't really change:
214+
const vendors = new Map(
215+
await Promise.all(
216+
[
217+
{
218+
...compileVendorLibrary('acorn-loose'),
219+
plugins: [esbuildPlugins.hackyVendorBundle(new Map([['acorn', './acorn']]))],
220+
},
221+
compileVendorLibrary('acorn'),
222+
].map(async ({ name, ...opts }) => {
223+
await esbuild.build({
224+
...opts,
225+
sourcemap,
226+
bundle: true,
227+
platform: 'node',
228+
format: 'cjs',
229+
target: 'node18',
230+
minify,
231+
});
232+
233+
return [name, `./${vendorPrefix}/${name}.js`];
234+
}),
235+
),
236+
);
237+
202238
// add the entrypoints common to both vscode and vs here
203239
packages = [
204240
...packages,
205241
{ entry: `${srcDir}/common/hash/hash.ts`, library: false },
242+
{ entry: `${srcDir}/common/sourceMaps/renameWorker.ts`, library: false },
206243
{ entry: `${srcDir}/targets/node/bootloader.ts`, library: false, target: 'node10' },
207244
{ entry: `${srcDir}/targets/node/watchdog.ts`, library: false, target: 'node10' },
208245
{
@@ -222,7 +259,7 @@ async function compileTs({
222259
library,
223260
isInVsCode,
224261
nodePackages,
225-
target = 'node16',
262+
target = 'node18',
226263
} of packages) {
227264
todo.push(
228265
incrementalEsbuild({
@@ -245,6 +282,7 @@ async function compileTs({
245282
esbuildPlugins.nativeNodeModulesPlugin(),
246283
esbuildPlugins.importGlobLazy(),
247284
esbuildPlugins.dirname(/src.test./),
285+
esbuildPlugins.hackyVendorBundle(vendors),
248286
],
249287
format: library ? 'cjs' : 'iife',
250288
}),

0 commit comments

Comments
 (0)