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

Import.meta.require #21317

Closed
wants to merge 5 commits into from
Closed
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
5 changes: 5 additions & 0 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ The `import.meta` metaproperty is an `Object` that contains the following
property:

* `url` {string} The absolute `file:` URL of the module.
* `require` {Function} To require CommonJS modules. This function enables
interoperability between CJS and ESM. See [`require()`]. None of the
properties generally exposed on require are available via
`import.meta.require`.

### Unsupported

Expand Down Expand Up @@ -256,3 +260,4 @@ in the import tree.
[Node.js EP for ES Modules]: https://github.com/nodejs/node-eps/blob/master/002-es-modules.md
[addons]: addons.html
[dynamic instantiate hook]: #esm_dynamic_instantiate_hook
[`require()`]: modules.html#modules_require
22 changes: 21 additions & 1 deletion lib/internal/process/esm_loader.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
'use strict';

const { makeRequireFunction } = require('internal/modules/cjs/helpers');
const Module = require('module');
const { internalBinding } = require('internal/bootstrap/loaders');

const {
setImportModuleDynamicallyCallback,
setInitializeImportMetaObjectCallback
} = internalBinding('module_wrap');
const { defineProperty } = Object;

const { getURLFromFilePath } = require('internal/url');
const { getURLFromFilePath, getPathFromURL } = require('internal/url');
const Loader = require('internal/modules/esm/loader');
const path = require('path');
const { URL } = require('url');
Expand All @@ -27,6 +31,22 @@ function initializeImportMetaObject(wrap, meta) {
if (vmModule === undefined) {
// This ModuleWrap belongs to the Loader.
meta.url = wrap.url;
let req;
defineProperty(meta, 'require', {
enumerable: true,
configurable: true,
get() {
if (req !== undefined)
return req;
const url = new URL(meta.url);
const path = getPathFromURL(url);
const mod = new Module(path, null);
mod.filename = path;
mod.paths = Module._nodeModulePaths(path);
req = makeRequireFunction(mod).bind(null);
return req;
}
});
} else {
const initializeImportMeta = initImportMetaMap.get(vmModule);
if (initializeImportMeta !== undefined) {
Expand Down
39 changes: 33 additions & 6 deletions test/es-module/test-esm-import-meta.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,47 @@
import '../common';
import assert from 'assert';

const fixtures = import.meta.require('../common/fixtures');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be exposed by common.mjs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that using it this way offered an extra smoke test for import.meta.require.

more than happy to change it though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at best we can take it as an excuse to build up common.mjs... i don't like the idea of "smoke tests" but that's just an opinion.


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

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

const descriptors = Object.getOwnPropertyDescriptors(import.meta);

for (const descriptor of Object.values(descriptors)) {
delete descriptor.value; // Values are verified below.
assert.deepStrictEqual(descriptor, {
enumerable: true,
writable: true,
configurable: true
});
}

assert.deepStrictEqual(descriptors.url, {
enumerable: true,
writable: true,
configurable: true
});

assert.deepStrictEqual(descriptors.require, {
get: descriptors.require.get,
set: undefined,
enumerable: true,
configurable: true
});

assert.strictEqual(import.meta.require.cache, undefined);
assert.strictEqual(import.meta.require.extensions, undefined);
assert.strictEqual(import.meta.require.main, undefined);
assert.strictEqual(import.meta.require.paths, undefined);

const urlReg = /^file:\/\/\/.*\/test\/es-module\/test-esm-import-meta\.mjs$/;
assert(import.meta.url.match(urlReg));

const a = import.meta.require(
fixtures.path('module-require', 'relative', 'dot.js')
);
const b = import.meta.require(
fixtures.path('module-require', 'relative', 'dot-slash.js')
);

assert.strictEqual(a.value, 42);
// require(".") should resolve like require("./")
assert.strictEqual(a, b);