Skip to content
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

esm: implement import.meta.main #32223

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,10 @@ import _ from 'data:application/json,"world!"';
* {Object}

The `import.meta` metaproperty is an `Object` that contains the following
property:
properties:

* `main` {boolean} `true` when the current module is the entry point of
the current process.
* `url` {string} The absolute `file:` URL of the module.

jasnell marked this conversation as resolved.
Show resolved Hide resolved
## Differences Between ES Modules and CommonJS
Expand Down Expand Up @@ -820,6 +822,8 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
```

Equivalent of `require.main === module` is [`import.meta.main`][].

### No `require.resolve`

Former use cases relying on `require.resolve` to determine the resolved path
Expand Down Expand Up @@ -1694,6 +1698,7 @@ success!
[`getFormat` hook]: #esm_code_getformat_code_hook
[`import()`]: #esm_import-expressions
[`import.meta.url`]: #esm_import_meta
[`import.meta.main`]: #esm_import_meta
[`import`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
[`module.createRequire()`]: modules.html#modules_module_createrequire_filename
[`module.syncBuiltinESMExports()`]: modules.html#modules_module_syncbuiltinesmexports
Expand Down
9 changes: 7 additions & 2 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
/* global WebAssembly */

const {
Boolean,
FunctionPrototypeBind,
JSONParse,
ObjectKeys,
SafeMap,
Expand Down Expand Up @@ -66,11 +68,12 @@ function initializeImportMeta(meta, { url }) {
// Alphabetical
if (experimentalImportMetaResolve)
meta.resolve = createImportMetaResolve(url);
meta.main = Boolean(this?.isMain);
meta.url = url;
}

// Strategy for loading a standard JavaScript module
translators.set('module', async function moduleStrategy(url) {
translators.set('module', async function moduleStrategy(url, isMain) {
let { source } = await this._getSource(
url, { format: 'module' }, defaultGetSource);
source = `${source}`;
Expand All @@ -80,7 +83,9 @@ translators.set('module', async function moduleStrategy(url) {
debug(`Translating StandardModule ${url}`);
const module = new ModuleWrap(url, undefined, source, 0, 0);
moduleWrap.callbackMap.set(module, {
initializeImportMeta,
initializeImportMeta: isMain ?
FunctionPrototypeBind(initializeImportMeta, { isMain }) :
initializeImportMeta,
importModuleDynamically,
});
return module;
Expand Down
11 changes: 11 additions & 0 deletions test/es-module/test-esm-import-meta-main.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { mustCall } from '../common/index.mjs';
import fixtures from '../common/fixtures.js';
import assert from 'assert';

assert.strictEqual(import.meta.main, true);

import(fixtures.path('/es-modules/import-meta-main.mjs')).then(
mustCall(({ isMain }) => {
assert.strictEqual(isMain, false);
})
);
2 changes: 1 addition & 1 deletion test/es-module/test-esm-import-meta.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import assert from 'assert';

assert.strictEqual(Object.getPrototypeOf(import.meta), null);

const keys = ['url'];
const keys = ['main', 'url'];
assert.deepStrictEqual(Reflect.ownKeys(import.meta), keys);

const descriptors = Object.getOwnPropertyDescriptors(import.meta);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/es-modules/import-meta-main.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isMain = import.meta.main;