Skip to content

Commit 4ecaa95

Browse files
Merge pull request #738 from lukecotter/bug-go-to-code-error
fix: preserve ANTLR ATN data in rolldown build
2 parents 5360c97 + 7a0c41d commit 4ecaa95

2 files changed

Lines changed: 41 additions & 15 deletions

File tree

rolldown.config.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineConfig } from 'rolldown';
1+
import { defineConfig, Plugin } from 'rolldown';
22

33
// rolldown plugins
44
import nodePolyfills from '@rolldown/plugin-node-polyfills';
@@ -22,16 +22,39 @@ const getSwcOptions = (dirPath: string) =>
2222
jsc: {
2323
transform: { useDefineForClassFields: false },
2424
minify: {
25-
compress: production,
26-
mangle: production
27-
? {
28-
keepClassNames: true,
29-
}
30-
: false,
25+
compress: production ? { keep_classnames: true, keep_fnames: true } : false,
26+
mangle: production ? { keep_classnames: true } : false,
3127
},
3228
},
3329
});
3430

31+
/**
32+
* Workaround for oxc printer lone-surrogate bug (https://github.com/oxc-project/oxc/issues/3526).
33+
* The oxc codegen replaces lone surrogates (0xD800-0xDFFF) with U+FFFD in long CJS strings,
34+
* corrupting ANTLR ATN serialized data which uses surrogates as raw char codes.
35+
* This plugin converts the string literals to numeric char code arrays before the printer
36+
* sees them. Can be removed once the upstream fix fully covers CJS long strings.
37+
*/
38+
function preserveAntlrATN(): Plugin {
39+
const segmentPattern =
40+
/(\w+\._serializedATNSegment\d+)\s*=\s*("(?:[^"\\]|\\.)*"(?:\s*\+\s*"(?:[^"\\]|\\.)*")*)\s*;/g;
41+
42+
return {
43+
name: 'preserve-antlr-atn',
44+
transform(code, id) {
45+
if (!id.includes('node_modules') || !/_serializedATNSegment\d+\s*=/.test(code)) {
46+
return;
47+
}
48+
49+
return code.replace(segmentPattern, (_match, varName: string, expr: string) => {
50+
const str = new Function(`return ${expr}`)() as string;
51+
const charCodes = Array.from(str, (c) => c.charCodeAt(0));
52+
return `${varName} = String.fromCharCode(${charCodes.join(',')});`;
53+
});
54+
},
55+
};
56+
}
57+
3558
const production = process.env.NODE_ENV === 'production';
3659
console.log('Package mode:', production ? 'production' : 'development');
3760
export default defineConfig([
@@ -42,11 +65,18 @@ export default defineConfig([
4265
dir: './lana/out',
4366
chunkFileNames: 'lana-[name].js',
4467
sourcemap: false,
68+
keepNames: true,
4569
},
4670
tsconfig: production ? './lana/tsconfig.json' : './lana/tsconfig-dev.json',
4771
platform: 'node',
72+
resolve: {
73+
alias: {
74+
'apex-log-parser': path.resolve(__dirname, 'apex-log-parser/src/index.ts'),
75+
},
76+
},
77+
4878
external: ['vscode'],
49-
plugins: [swc(getSwcOptions('./lana'))],
79+
plugins: [preserveAntlrATN(), swc(getSwcOptions('./lana'))],
5080
},
5181
{
5282
input: { bundle: './log-viewer/src/Main.ts' },
@@ -56,13 +86,13 @@ export default defineConfig([
5686
dir: './log-viewer/out',
5787
chunkFileNames: 'log-viewer-[name].js',
5888
sourcemap: false,
89+
keepNames: true,
5990
},
6091
],
6192
platform: 'browser',
6293
resolve: {
6394
alias: { eventemitter3: path.resolve(__dirname, 'node_modules/eventemitter3/index.js') },
6495
},
65-
keepNames: true,
6696
moduleTypes: {
6797
'.css': 'js',
6898
},

rollup.config.mjs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,8 @@ export default [
4646
tsconfig: production ? './lana/tsconfig.json' : './lana/tsconfig-dev.json',
4747
jsc: {
4848
minify: {
49-
compress: production,
50-
mangle: production
51-
? {
52-
keep_classnames: true,
53-
}
54-
: false,
49+
compress: production ? { keep_classnames: true, keep_fnames: true } : false,
50+
mangle: production? { keep_classnames: true, } : false,
5551
},
5652
},
5753
}),

0 commit comments

Comments
 (0)