Skip to content

Commit e3d455b

Browse files
committed
fix(detector): enable decorators-legacy plugin for babel parse
Third-party sources that ship raw `@decorator` syntax (fontkit, older MobX / Nest builds) tripped the same silent-drop failure mode as #264: babel.parse threw, `detect()` logged a warning, and the file's dependency graph was dropped. Enable `decorators-legacy` in both the walker's detector and the ESM-transformer parse calls so these sources parse cleanly and their requires/imports get bundled. Extend test-94 with a decorator fixture walked via `pkg.scripts`.
1 parent 228318b commit e3d455b

5 files changed

Lines changed: 34 additions & 5 deletions

File tree

eslint.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module.exports = [
1515
'test/test-51-esm-import-meta/esm-module/test-import-meta-basic.js',
1616
'lib/log.js', // ESM re-export file
1717
'test/test-50-extensions/test-y-esnext.js', // ESM test file
18+
'test/test-94-sea-esm-import-meta/app/lib/decorated.js', // decorator syntax fixture
19+
1820
'prelude/sea-bootstrap.bundle.js', // Generated by esbuild
1921
'prelude/sea-bootstrap-esm.bundle.mjs', // Generated by esbuild
2022
],

lib/detector.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,12 +549,18 @@ function traverse(ast: babelTypes.File, visitor: VisitorFunction) {
549549
}
550550
}
551551

552-
/** `babel.parse` wrapper. `isEsm` selects `sourceType: 'module'` so `import.meta` / top-level await parse cleanly. */
552+
/**
553+
* `babel.parse` wrapper. `isEsm` selects `sourceType: 'module'` so `import.meta`
554+
* / top-level await parse cleanly. `decorators-legacy` is enabled so third-party
555+
* sources that ship raw `@decorator` syntax (fontkit, older MobX/Nest builds,
556+
* etc.) don't trip the parser and silently drop their dependency graph.
557+
*/
553558
export function parse(body: string, isEsm = false) {
554559
return babel.parse(body, {
555560
allowImportExportEverywhere: true,
556561
allowReturnOutsideFunction: true,
557562
sourceType: isEsm ? 'module' : 'script',
563+
plugins: ['decorators-legacy'],
558564
});
559565
}
560566

lib/esm-transformer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function hasImportMeta(code: string): boolean {
3535
try {
3636
const ast = babel.parse(code, {
3737
sourceType: 'module',
38-
plugins: [],
38+
plugins: ['decorators-legacy'],
3939
});
4040

4141
if (!ast) {
@@ -85,7 +85,7 @@ function detectESMFeatures(
8585
try {
8686
const ast = babel.parse(code, {
8787
sourceType: 'module',
88-
plugins: [],
88+
plugins: ['decorators-legacy'],
8989
});
9090

9191
if (!ast) {
@@ -300,7 +300,7 @@ export function transformESMtoCJS(
300300
// Parse the code to check for exports and collect imports
301301
const ast = babel.parse(code, {
302302
sourceType: 'module',
303-
plugins: [],
303+
plugins: ['decorators-legacy'],
304304
});
305305

306306
let hasExports = false;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Decorator syntax forces the walker's Babel parser to enable the
2+
// `decorators-legacy` plugin. Without it pkg would log "Babel parse has
3+
// failed: This experimental syntax requires enabling one of the following
4+
// parser plugin(s)" and silently drop this file's dependency graph. This
5+
// file is walked via `pkg.scripts` but never imported at runtime — Node
6+
// can't execute raw decorator syntax.
7+
function log(Cls) {
8+
return Cls;
9+
}
10+
11+
@log
12+
export class Widget {
13+
greet(name) {
14+
return 'widget:' + name;
15+
}
16+
}

test/test-94-sea-esm-import-meta/app/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@
33
"version": "1.0.0",
44
"type": "module",
55
"main": "index.mjs",
6-
"bin": "index.mjs"
6+
"bin": "index.mjs",
7+
"pkg": {
8+
"scripts": [
9+
"lib/decorated.js"
10+
]
11+
}
712
}

0 commit comments

Comments
 (0)