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

modules: add import.meta.__dirname and import.meta.__filename #39147

Closed
wants to merge 1 commit 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
38 changes: 38 additions & 0 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,44 @@ modules it can be used to load ES modules.
The `import.meta` meta property is an `Object` that contains the following
properties.

### `import.meta.__dirname`

> Stability: 3 - Legacy: Use `import.meta.url` instead.

* {string} A convenience shortcut that provides the equivalent of the CommonJS
`__dirname` pseudo-global. Specifically, it returns the result of converting
the `import.meta.url` into a file path then returning the directory portion.

The property is only available when `import.meta.url` specifies a `file://`
Copy link
Member

Choose a reason for hiding this comment

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

the check in the impl only uses file: should you be using // in the impl or remove that bit from here to just be file:? generally when talking about scheme we just have scheme: (E.G. https://nodejs.org/dist/latest-v14.x/docs/api/esm.html#esm_file_urls )

URL.

```mjs
const { __dirname } = import.meta;
```

The `import.meta.__dirname` is provided as a convenience to help ease the
transition from existing CommonJS code to ESM. New ESM code should use the
`import.meta.url` property directly.

### `import.meta.__filename`

> Stability: 3 - Legacy: Use `import.meta.url` instead.

* {string} A convenience shortcut that provides the equivalent of the CommonJS
`__filename` pseudo-global. Specifically, it returns the result of converting
the `import.meta.url` into a file path.

The property is only available when `import.meta.url` specifies a `file://`
URL.

```mjs
const { __filename } = import.meta;
```

The `import.meta.__filename` is provided as a convenience to help ease the
transition from existing CommonJS code to ESM. New ESM code should use the
`import.meta.url` property directly.

### `import.meta.url`

* {string} The absolute `file:` URL of the module.
Expand Down
7 changes: 7 additions & 0 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
ArrayPrototypeMap,
Boolean,
JSONParse,
ObjectDefineProperties,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ObjectDefineProperties,

Unused primordial.

ObjectGetPrototypeOf,
ObjectPrototypeHasOwnProperty,
ObjectKeys,
Expand Down Expand Up @@ -39,6 +40,7 @@ const {
cjsParseCache
} = require('internal/modules/cjs/loader');
const internalURLModule = require('internal/url');
const internalPathModule = require('path');
const { defaultGetSource } = require(
'internal/modules/esm/get_source');
const { defaultTransformSource } = require(
Expand Down Expand Up @@ -129,6 +131,11 @@ function createImportMetaResolve(defaultParentUrl) {

function initializeImportMeta(meta, { url }) {
// Alphabetical
if (StringPrototypeStartsWith(url, 'file:')) {
meta.__filename = internalURLModule.fileURLToPath(new URL(url));
meta.__dirname = internalPathModule.dirname(meta.__filename);
Comment on lines +135 to +136
Copy link
Member

Choose a reason for hiding this comment

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

can this be done with getters / lazily?

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately not, there's a test that checks that import.meta properties are writable

Copy link
Member

Choose a reason for hiding this comment

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

seems fine

}

if (experimentalImportMetaResolve)
meta.resolve = createImportMetaResolve(url);
meta.url = url;
Expand Down
8 changes: 7 additions & 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 = ['__filename', '__dirname', 'url'];
assert.deepStrictEqual(Reflect.ownKeys(import.meta), keys);

const descriptors = Object.getOwnPropertyDescriptors(import.meta);
Expand All @@ -18,3 +18,9 @@ for (const descriptor of Object.values(descriptors)) {

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

const filenameReg = /^\/.*\/test\/es-module\/test-esm-import-meta\.mjs$/;
assert(import.meta.__filename.match(filenameReg));

const dirnameReg = /^\/.*\/test\/es-module$/;
assert(import.meta.__dirname.match(dirnameReg));