mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 00:20:08 +01:00
Boolean value to check if an ES Module is the entrypoint of the current process. Implements: #57226 Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: https://github.com/nodejs/node/pull/57804 Fixes: https://github.com/nodejs/node/issues/57226 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
35 lines
1.3 KiB
JavaScript
35 lines
1.3 KiB
JavaScript
import '../common/index.mjs';
|
|
import assert from 'assert';
|
|
|
|
assert.strictEqual(Object.getPrototypeOf(import.meta), null);
|
|
|
|
const keys = ['dirname', 'filename', 'main', 'resolve', 'url'];
|
|
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
|
|
});
|
|
}
|
|
|
|
const urlReg = /^file:\/\/\/.*\/test\/es-module\/test-esm-import-meta\.mjs$/;
|
|
assert.match(import.meta.url, urlReg);
|
|
|
|
// Match *nix paths: `/some/path/test/es-module`
|
|
// Match Windows paths: `d:\\some\\path\\test\\es-module`
|
|
const dirReg = /^(\/|\w:\\).*(\/|\\)test(\/|\\)es-module$/;
|
|
assert.match(import.meta.dirname, dirReg);
|
|
|
|
// Match *nix paths: `/some/path/test/es-module/test-esm-import-meta.mjs`
|
|
// Match Windows paths: `d:\\some\\path\\test\\es-module\\test-esm-import-meta.js`
|
|
const fileReg = /^(\/|\w:\\).*(\/|\\)test(\/|\\)es-module(\/|\\)test-esm-import-meta\.mjs$/;
|
|
assert.match(import.meta.filename, fileReg);
|
|
|
|
// Verify that `data:` imports do not behave like `file:` imports.
|
|
import dataDirname from 'data:text/javascript,export default "dirname" in import.meta';
|
|
assert.strictEqual(dataDirname, false);
|