-
-
Notifications
You must be signed in to change notification settings - Fork 32k
esm: implement import.meta.main #57804
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
nodejs-github-bot
merged 45 commits into
nodejs:main
from
Lordfirespeed:import-meta-main
May 26, 2025
Merged
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
e59c214
esm: implement import.meta.main
Lordfirespeed 53fc3b0
fix: `require.main === module` is still accurate, remove todo
Lordfirespeed 4ab6d86
fix: remove copy-paste artifact
Lordfirespeed 10b0626
improve example usage of `import.meta.main`
Lordfirespeed 151ca43
style: `if` blocks should be wrapped in braces
Lordfirespeed 0154d53
style: arrange values alphabetically
Lordfirespeed df72c4c
fix: document correct stability, remove todo
Lordfirespeed 642db17
style: assign `import.meta` members in alphabetical order
Lordfirespeed 7224e88
move assignment of `module.isMain` to `compileSourceTextModule`
Lordfirespeed c31fc3e
fix: copy-paste artefact
Lordfirespeed fd7d355
fix: pass `context` parameter from translator function
Lordfirespeed 3d3c3cc
Fix: missing semicolon
Lordfirespeed cc753b9
fix: failing test in folder with unusual characters
Lordfirespeed 43f3121
remove todo; various reviewers had no qualms
Lordfirespeed 3046280
prefer `kEmptyObject` to `{}`
Lordfirespeed 31e1c93
add test for worker threads (passing)
Lordfirespeed c1223c2
improve assertion messages
Lordfirespeed cff69a1
fix: import `kEmptyObject`
Lordfirespeed 22c4bc6
fix: remove newline + adhere to linting rules
Lordfirespeed 36318dd
add `--eval` tests for `import.meta.main`
Lordfirespeed a050fa7
style: fix linter complaints
Lordfirespeed 9926876
docs: add side-effect to `main()`
Lordfirespeed 4ccf6eb
fix: assign `import.meta.main` to `true` for `--eval`
Lordfirespeed 61c0d9e
style: resolve linter complaints
Lordfirespeed 0dcf2a0
prefer `spawnPromisified` to `exec` with shell escaping
Lordfirespeed a6a9614
simplify test using top-level await
Lordfirespeed 420c585
style: resolve linter complaints
Lordfirespeed b9836f6
commit @aduh95's suggestions r.e. test refactor-ability
Lordfirespeed 46cfa4b
fix: accidental directory import is not permitted
Lordfirespeed 45c3dfd
prefer `deepStrictEqual` to consecutive `strictEqual`
Lordfirespeed 0a05adc
add `import.meta.main` test for worker with url input
Lordfirespeed 28cb8fb
style: adjust test function names
Lordfirespeed 48125ac
fix: run worker with `--expose-internals` and adjust url 'wrapper'
Lordfirespeed 16e8bb6
avoid lookup in prototype by calling directly
Lordfirespeed 0794ffa
docs: `*` bullets are reserved for signature annotations; convert off…
Lordfirespeed 250573a
refactor: use functions to wrap scripts for each scenario
Lordfirespeed 92e1993
refactor: extract `convertScriptSourceToDataUrl` function
Lordfirespeed 4fe6f22
use a different strategy for running `data:` URL workers
Lordfirespeed 616963b
prefer `runEntryPointWithESMLoader` to `getSourceSync` -> `stringify`…
Lordfirespeed 98c3cfb
remove unused import
Lordfirespeed dc8d4f9
revert changes to `options.execArgv` handling
Lordfirespeed f0c6abc
inline `execOptions`
Lordfirespeed 0bf97dd
prefer `JSON.stringify` to literal doublequotes
Lordfirespeed e065731
refactor: rewrite using `node:test` to ensure failure is caught in ca…
Lordfirespeed 41d80f0
style: resolve linter complaints
Lordfirespeed 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
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,74 @@ | ||
import { spawnPromisified } from '../common/index.mjs'; | ||
import * as fixtures from '../common/fixtures.js'; | ||
import assert from 'node:assert/strict'; | ||
import { describe, it } from 'node:test'; | ||
|
||
const importMetaMainScript = ` | ||
import assert from 'node:assert/strict'; | ||
|
||
assert.strictEqual(import.meta.main, true, 'import.meta.main should evaluate true in main module'); | ||
|
||
const { isMain: importedModuleIsMain } = await import( | ||
${JSON.stringify(fixtures.fileURL('es-modules/import-meta-main.mjs'))} | ||
); | ||
assert.strictEqual(importedModuleIsMain, false, 'import.meta.main should evaluate false in imported module'); | ||
`; | ||
|
||
function wrapScriptInEvalWorker(script) { | ||
return ` | ||
import { Worker } from 'node:worker_threads'; | ||
new Worker(${JSON.stringify(script)}, { eval: true }); | ||
`; | ||
} | ||
|
||
function convertScriptSourceToDataUrl(script) { | ||
return new URL(`data:text/javascript,${encodeURIComponent(script)}`); | ||
} | ||
|
||
function wrapScriptInUrlWorker(script) { | ||
return ` | ||
import { Worker } from 'node:worker_threads'; | ||
new Worker(new URL(${JSON.stringify(convertScriptSourceToDataUrl(script))})); | ||
`; | ||
} | ||
|
||
describe('import.meta.main in evaluated scripts', () => { | ||
it('should evaluate true in evaluated script', async () => { | ||
const result = await spawnPromisified( | ||
process.execPath, | ||
['--input-type=module', '--eval', importMetaMainScript], | ||
); | ||
assert.deepStrictEqual(result, { | ||
stderr: '', | ||
stdout: '', | ||
code: 0, | ||
signal: null, | ||
}); | ||
}); | ||
|
||
it('should evaluate true in worker instantiated with module source by evaluated script', async () => { | ||
const result = await spawnPromisified( | ||
process.execPath, | ||
['--input-type=module', '--eval', wrapScriptInEvalWorker(importMetaMainScript)], | ||
); | ||
assert.deepStrictEqual(result, { | ||
stderr: '', | ||
stdout: '', | ||
code: 0, | ||
signal: null, | ||
}); | ||
}); | ||
|
||
it('should evaluate true in worker instantiated with `data:` URL by evaluated script', async () => { | ||
const result = await spawnPromisified( | ||
process.execPath, | ||
['--input-type=module', '--eval', wrapScriptInUrlWorker(importMetaMainScript)], | ||
); | ||
assert.deepStrictEqual(result, { | ||
stderr: '', | ||
stdout: '', | ||
code: 0, | ||
signal: null, | ||
}); | ||
}); | ||
}); |
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,26 @@ | ||
import '../common/index.mjs'; | ||
import assert from 'node:assert/strict'; | ||
import { Worker } from 'node:worker_threads'; | ||
|
||
function get_environment() { | ||
if (process.env.HAS_STARTED_WORKER) return 'in worker thread started by ES Module'; | ||
return 'in ES Module'; | ||
} | ||
|
||
assert.strictEqual( | ||
import.meta.main, | ||
true, | ||
`\`import.meta.main\` at top-level module ${get_environment()} should evaluate \`true\`` | ||
); | ||
|
||
const { isMain: importedModuleIsMain } = await import('../fixtures/es-modules/import-meta-main.mjs'); | ||
assert.strictEqual( | ||
importedModuleIsMain, | ||
false, | ||
`\`import.meta.main\` at dynamically imported module ${get_environment()} should evaluate \`false\`` | ||
); | ||
|
||
if (!process.env.HAS_STARTED_WORKER) { | ||
process.env.HAS_STARTED_WORKER = 1; | ||
new Worker(import.meta.filename); | ||
} |
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 @@ | ||
export const isMain = import.meta.main; |
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.