| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c9e6e4e commit eac9cc5
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + import assert from 'assert'; | ||
| 2 | + assert.ok(import.meta.dirname); | ||
| 3 | + assert.ok(import.meta.filename); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,5 @@ | |||
| 1 | + (async function () { | ||
| 2 | + for (let i = 0; i < 1000; i += 1) { | ||
| 3 | + await import(`./esm-dir-file.mjs?i=${i}`); | ||
| 4 | + } | ||
| 5 | + }()); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ const bench = common.createBenchmark(main, { | |||
| 9 | 9 | script: [ | |
| 10 | 10 | 'benchmark/fixtures/require-builtins', | |
| 11 | 11 | 'test/fixtures/semicolon', | |
| 12 | + 'benchmark/fixtures/load-esm-dir-file', | ||
| 12 | 13 | ], | |
| 13 | 14 | mode: ['process', 'worker'], | |
| 14 | 15 | count: [30], | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -332,6 +332,35 @@ modules it can be used to load ES modules. | |||
| 332 | 332 | The `import.meta` meta property is an `Object` that contains the following | |
| 333 | 333 | properties. | |
| 334 | 334 | ||
| 335 | + ### `import.meta.dirname` | ||
| 336 | + | ||
| 337 | + <!-- YAML | ||
| 338 | + added: REPLACEME | ||
| 339 | + --> | ||
| 340 | + | ||
| 341 | + > Stability: 1.2 - Release candidate | ||
| 342 | + | ||
| 343 | + * {string} The directory name of the current module. This is the same as the | ||
| 344 | + [`path.dirname()`][] of the [`import.meta.filename`][]. | ||
| 345 | + | ||
| 346 | + > **Caveat**: only present on `file:` modules. | ||
| 347 | + | ||
| 348 | + ### `import.meta.filename` | ||
| 349 | + | ||
| 350 | + <!-- YAML | ||
| 351 | + added: REPLACEME | ||
| 352 | + --> | ||
| 353 | + | ||
| 354 | + > Stability: 1.2 - Release candidate | ||
| 355 | + | ||
| 356 | + * {string} The full absolute path and filename of the current module, with | ||
| 357 | + * symlinks resolved. | ||
| 358 | + * This is the same as the [`url.fileURLToPath()`][] of the | ||
| 359 | + * [`import.meta.url`][]. | ||
| 360 | + | ||
| 361 | + > **Caveat** only local modules support this property. Modules not using the | ||
| 362 | + > `file:` protocol will not provide it. | ||
| 363 | + | ||
| 335 | 364 | ### `import.meta.url` | |
| 336 | 365 | ||
| 337 | 366 | * {string} The absolute `file:` URL of the module. | |
@@ -526,7 +555,7 @@ If needed, a `require` function can be constructed within an ES module using | |||
| 526 | 555 | These CommonJS variables are not available in ES modules. | |
| 527 | 556 | ||
| 528 | 557 | `__filename` and `__dirname` use cases can be replicated via | |
| 529 | - [`import.meta.url`][]. | ||
| 558 | + [`import.meta.filename`][] and [`import.meta.dirname`][]. | ||
| 530 | 559 | ||
| 531 | 560 | #### No Addon Loading | |
| 532 | 561 | ||
@@ -1107,13 +1136,17 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][]. | |||
| 1107 | 1136 | [`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs | |
| 1108 | 1137 | [`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export | |
| 1109 | 1138 | [`import()`]: #import-expressions | |
| 1139 | + [`import.meta.dirname`]: #importmetadirname | ||
| 1140 | + [`import.meta.filename`]: #importmetafilename | ||
| 1110 | 1141 | [`import.meta.resolve`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve | |
| 1111 | 1142 | [`import.meta.url`]: #importmetaurl | |
| 1112 | 1143 | [`import`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import | |
| 1113 | 1144 | [`module.createRequire()`]: module.md#modulecreaterequirefilename | |
| 1114 | 1145 | [`module.syncBuiltinESMExports()`]: module.md#modulesyncbuiltinesmexports | |
| 1115 | 1146 | [`package.json`]: packages.md#nodejs-packagejson-field-definitions | |
| 1147 | + [`path.dirname()`]: path.md#pathdirnamepath | ||
| 1116 | 1148 | [`process.dlopen`]: process.md#processdlopenmodule-filename-flags | |
| 1149 | + [`url.fileURLToPath()`]: url.md#urlfileurltopathurl | ||
| 1117 | 1150 | [cjs-module-lexer]: https://github.com/nodejs/cjs-module-lexer/tree/1.2.2 | |
| 1118 | 1151 | [commonjs-extension-resolution-loader]: https://github.com/nodejs/loaders-test/tree/main/commonjs-extension-resolution-loader | |
| 1119 | 1152 | [custom https loader]: module.md#import-from-https | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,9 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | + const { StringPrototypeStartsWith } = primordials; | ||
| 3 | 4 | const { getOptionValue } = require('internal/options'); | |
| 5 | + const { fileURLToPath } = require('internal/url'); | ||
| 6 | + const { dirname } = require('path'); | ||
| 4 | 7 | const experimentalImportMetaResolve = getOptionValue('--experimental-import-meta-resolve'); | |
| 5 | 8 | ||
| 6 | 9 | /** | |
@@ -45,12 +48,20 @@ function createImportMetaResolve(defaultParentURL, loader, allowParentURL) { | |||
| 45 | 48 | * @param {object} meta | |
| 46 | 49 | * @param {{url: string}} context | |
| 47 | 50 | * @param {typeof import('./loader.js').ModuleLoader} loader Reference to the current module loader | |
| 48 | - * @returns {{url: string, resolve?: Function}} | ||
| 51 | + * @returns {{dirname?: string, filename?: string, url: string, resolve?: Function}} | ||
| 49 | 52 | */ | |
| 50 | 53 | function initializeImportMeta(meta, context, loader) { | |
| 51 | 54 | const { url } = context; | |
| 52 | 55 | ||
| 53 | 56 | // Alphabetical | |
| 57 | + if (StringPrototypeStartsWith(url, 'file:') === true) { | ||
| 58 | + // These only make sense for locally loaded modules, | ||
| 59 | + // i.e. network modules are not supported. | ||
| 60 | + const filePath = fileURLToPath(url); | ||
| 61 | + meta.dirname = dirname(filePath); | ||
| 62 | + meta.filename = filePath; | ||
| 63 | + } | ||
| 64 | + | ||
| 54 | 65 | if (!loader || loader.allowImportMetaResolve) { | |
| 55 | 66 | meta.resolve = createImportMetaResolve(url, loader, experimentalImportMetaResolve); | |
| 56 | 67 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ import assert from 'assert'; | |||
| 3 | 3 | ||
| 4 | 4 | assert.strictEqual(Object.getPrototypeOf(import.meta), null); | |
| 5 | 5 | ||
| 6 | - const keys = ['resolve', 'url']; | ||
| 6 | + const keys = ['dirname', 'filename', 'resolve', 'url']; | ||
| 7 | 7 | assert.deepStrictEqual(Reflect.ownKeys(import.meta), keys); | |
| 8 | 8 | ||
| 9 | 9 | const descriptors = Object.getOwnPropertyDescriptors(import.meta); | |
@@ -18,3 +18,17 @@ for (const descriptor of Object.values(descriptors)) { | |||
| 18 | 18 | ||
| 19 | 19 | const urlReg = /^file:\/\/\/.*\/test\/es-module\/test-esm-import-meta\.mjs$/; | |
| 20 | 20 | assert(import.meta.url.match(urlReg)); | |
| 21 | + | ||
| 22 | + // Match *nix paths: `/some/path/test/es-module` | ||
| 23 | + // Match Windows paths: `d:\\some\\path\\test\\es-module` | ||
| 24 | + const dirReg = /^(\/|\w:\\).*(\/|\\)test(\/|\\)es-module$/; | ||
| 25 | + assert.match(import.meta.dirname, dirReg); | ||
| 26 | + | ||
| 27 | + // Match *nix paths: `/some/path/test/es-module/test-esm-import-meta.mjs` | ||
| 28 | + // Match Windows paths: `d:\\some\\path\\test\\es-module\\test-esm-import-meta.js` | ||
| 29 | + const fileReg = /^(\/|\w:\\).*(\/|\\)test(\/|\\)es-module(\/|\\)test-esm-import-meta\.mjs$/; | ||
| 30 | + assert.match(import.meta.filename, fileReg); | ||
| 31 | + | ||
| 32 | + // Verify that `data:` imports do not behave like `file:` imports. | ||
| 33 | + import dataDirname from 'data:text/javascript,export default "dirname" in import.meta'; | ||
| 34 | + assert.strictEqual(dataDirname, false); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,7 @@ | |||
| 1 | + #include <node_api.h> | ||
| 2 | + | ||
| 3 | + EXTERN_C_START | ||
| 4 | + napi_value Init(napi_env env, napi_value exports); | ||
| 5 | + EXTERN_C_END | ||
| 6 | + | ||
| 7 | + NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments