forked from vercel/pkg
-
-
Notifications
You must be signed in to change notification settings - Fork 61
fix(detector): stop silent dependency drops in SEA mode (ESM parse, dynamic import, decorators) #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix(detector): stop silent dependency drops in SEA mode (ESM parse, dynamic import, decorators) #268
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
133d1c7
fix(sea): parse ESM files as modules and walk dynamic import() literals
robertsLando b61a9f2
docs(detector): add concise jsdoc for visitor and helper functions
robertsLando 228318b
Merge branch 'main' into fix/264-esm-parse-import-meta
robertsLando e3d455b
fix(detector): enable decorators-legacy plugin for babel parse
robertsLando 339797a
fix(detector): reject non-string specifiers in dynamic import matcher
robertsLando File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // `import.meta` is only valid in `sourceType: "module"`. Before the fix for | ||
| // issue #264 the SEA walker parsed this body in script mode, so the parse | ||
| // failed and the detector never saw the imports below — neither the static | ||
| // one nor the dynamic one ended up in the snapshot. | ||
| import { greet } from './lib/helper.mjs'; | ||
|
|
||
| const here = new URL(import.meta.url).pathname; | ||
| console.log('here:' + here.split('/').pop()); | ||
| console.log('static:' + greet('world')); | ||
|
|
||
| const dyn = await import('./lib/dyn.mjs'); | ||
| console.log('dynamic:' + dyn.shout('world')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Decorator syntax forces the walker's Babel parser to enable the | ||
| // `decorators-legacy` plugin. Without it pkg would log "Babel parse has | ||
| // failed: This experimental syntax requires enabling one of the following | ||
| // parser plugin(s)" and silently drop this file's dependency graph. This | ||
| // file is walked via `pkg.scripts` but never imported at runtime — Node | ||
| // can't execute raw decorator syntax. | ||
| function log(Cls) { | ||
| return Cls; | ||
| } | ||
|
|
||
| @log | ||
| export class Widget { | ||
| greet(name) { | ||
| return 'widget:' + name; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export function shout(name) { | ||
| return 'HELLO ' + name.toUpperCase(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export function greet(name) { | ||
| return 'hello ' + name; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "name": "test-94-sea-esm-import-meta", | ||
| "version": "1.0.0", | ||
| "type": "module", | ||
| "main": "index.mjs", | ||
| "bin": "index.mjs", | ||
| "pkg": { | ||
| "scripts": [ | ||
| "lib/decorated.js" | ||
| ] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const assert = require('assert'); | ||
| const utils = require('../utils.js'); | ||
|
|
||
| // Enhanced SEA requires Node.js >= 22 | ||
| if (utils.getNodeMajorVersion() < 22) { | ||
| return; | ||
| } | ||
|
|
||
| assert(__dirname === process.cwd()); | ||
|
|
||
| const input = './app/package.json'; | ||
| const testName = 'test-94-sea-esm-import-meta'; | ||
|
|
||
| const SEA_PLATFORM_SUFFIX = { | ||
| linux: 'linux', | ||
| darwin: 'macos', | ||
| win32: 'win.exe', | ||
| }; | ||
| const suffix = SEA_PLATFORM_SUFFIX[process.platform]; | ||
|
|
||
| const newcomers = utils.seaHostOutputs(testName); | ||
| const before = utils.filesBefore(newcomers); | ||
|
|
||
| // Capture pkg's output so we can assert the Babel parse warning (issue #264) | ||
| // never surfaces when walking ESM files that use `import.meta`. | ||
| const args = suffix | ||
| ? [input, '--sea', '--target', 'host', '--output', `${testName}-${suffix}`] | ||
| : [input, '--sea']; | ||
|
|
||
| const build = utils.pkg.sync(args, { stdio: ['pipe', 'pipe', 'pipe'] }); | ||
| const buildLog = build.stdout + build.stderr; | ||
|
|
||
| assert( | ||
| buildLog.indexOf('Babel parse has failed') === -1, | ||
| 'pkg must parse ESM files as modules (issue #264)\npkg output was:\n' + | ||
| buildLog, | ||
| ); | ||
|
|
||
| // A successful parse means both imports were walked and bundled — running the | ||
| // binary proves it by printing the imported values. Skip on unsupported hosts. | ||
| if (suffix) { | ||
| utils.assertSeaOutput( | ||
| testName, | ||
| 'here:index.mjs\nstatic:hello world\ndynamic:HELLO WORLD\n', | ||
| ); | ||
| } | ||
|
|
||
| utils.filesAfter(before, newcomers, { tolerateWindowsEbusy: true }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.