Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
esm: add import.meta.dirname and import.meta.filename#48740
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
22d1c70dac01b40b6e16f37f048639def2a3c74b9f2643e7b983e142bba3fd688180cb603f050a77a8a8File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import assert from 'assert'; | ||
| assert.ok(import.meta.dirname); | ||
| assert.ok(import.meta.filename); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| (async function () { | ||
| for (let i = 0; i < 1000; i += 1) { | ||
| await import(`./esm-dir-file.mjs?i=${i}`); | ||
| } | ||
| }()); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -309,6 +309,35 @@ 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` | ||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
| > Stability: 1.2 - Release candidate | ||
GeoffreyBooth marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| * {string} The directory name of the current module. This is the same as the | ||
| [`path.dirname()`][] of the [`import.meta.filename`][]. | ||
| > **Caveat**: only present on `file:` modules. | ||
| ### `import.meta.filename` | ||
GeoffreyBooth marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
jsumners marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| > Stability: 1.2 - Release candidate | ||
GeoffreyBooth marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| * {string} The full absolute path and filename of the current module, with | ||
| * symlinks resolved. | ||
| * This is the same as the [`url.fileURLToPath()`][] of the | ||
| * [`import.meta.url`][]. | ||
| > **Caveat** only local modules support this property. Modules not using the | ||
| > `file:` protocol will not provide it. | ||
| ### `import.meta.url` | ||
| * {string} The absolute `file:` URL of the module. | ||
| @@ -503,7 +532,7 @@ If needed, a `require` function can be constructed within an ES module using | ||
| These CommonJS variables are not available in ES modules. | ||
| `__filename` and `__dirname` use cases can be replicated via | ||
| [`import.meta.url`][]. | ||
| [`import.meta.filename`][] and [`import.meta.dirname`][]. | ||
| #### No Addon Loading | ||
| @@ -1065,13 +1094,17 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][]. | ||
| [`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs | ||
| [`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export | ||
| [`import()`]: #import-expressions | ||
| [`import.meta.dirname`]: #importmetadirname | ||
| [`import.meta.filename`]: #importmetafilename | ||
| [`import.meta.resolve`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve | ||
| [`import.meta.url`]: #importmetaurl | ||
| [`import`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import | ||
| [`module.createRequire()`]: module.md#modulecreaterequirefilename | ||
| [`module.syncBuiltinESMExports()`]: module.md#modulesyncbuiltinesmexports | ||
| [`package.json`]: packages.md#nodejs-packagejson-field-definitions | ||
| [`path.dirname()`]: path.md#pathdirnamepath | ||
| [`process.dlopen`]: process.md#processdlopenmodule-filename-flags | ||
| [`url.fileURLToPath()`]: url.md#urlfileurltopathurl | ||
| [cjs-module-lexer]: https://github.com/nodejs/cjs-module-lexer/tree/1.2.2 | ||
| [commonjs-extension-resolution-loader]: https://github.com/nodejs/loaders-test/tree/main/commonjs-extension-resolution-loader | ||
| [custom https loader]: module.md#import-from-https | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3,7 +3,7 @@ import assert from 'assert'; | ||
| assert.strictEqual(Object.getPrototypeOf(import.meta), null); | ||
| const keys = ['resolve', 'url']; | ||
| const keys = ['dirname', 'filename', 'resolve', 'url']; | ||
| assert.deepStrictEqual(Reflect.ownKeys(import.meta), keys); | ||
| const descriptors = Object.getOwnPropertyDescriptors(import.meta); | ||
| @@ -18,3 +18,17 @@ for (const descriptor of Object.values(descriptors)) { | ||
| const urlReg = /^file:\/\/\/.*\/test\/es-module\/test-esm-import-meta\.mjs$/; | ||
| assert(import.meta.url.match(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); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need any separate tests for Windows paths? What about ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Is this done for the CJS implementation? What would we actually be testing? That
That looks like a mounted file system to me. | ||
| // 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); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #include<node_api.h> | ||
| EXTERN_C_START | ||
| napi_valueInit(napi_envenv, napi_valueexports); | ||
| EXTERN_C_END | ||
| NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) |
Uh oh!
There was an error while loading. Please reload this page.