Skip to content

Commit

Permalink
esm: implement import.meta.main
Browse files Browse the repository at this point in the history
Boolean value to check if a module is run as entry point of the current
process.

Fixes: https://gtihub.com/nodejs/modules/issues/274
  • Loading branch information
aduh95 committed Mar 19, 2020
1 parent ffdf1de commit 382d66c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
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.

## 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;

0 comments on commit 382d66c

Please sign in to comment.