-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
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
Changes from 6 commits
22d1c70
dac01b4
0b6e16f
37f0486
39def2a
3c74b9f
2643e7b
983e142
bba3fd6
88180cb
603f050
a77a8a8
File filter
Filter by extension
Conversations
Jump to
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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,13 @@ 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 dirReg = /^\/.*\/test\/es-module$/; | ||||||||
assert.match(import.meta.dirname, dirReg); | ||||||||
|
||||||||
const fileReg = /^\/.*\/test\/es-module\/test-esm-import-meta\.mjs$/; | ||||||||
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. Perhaps, we can make this regular expression more portable and versatile across different platforms and URL schemes, something like this:
Suggested change
WDYT? 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. What is the issue with the current, simpler, expression? 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. The current is very specific to Unix-like file paths. It assumes that the file path starts with a single forward slash (/) followed by directories, which might not hold true for Windows paths or file URLs. I assume that code needs to run on multiple platforms, so. 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. By definition this can’t be a 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. Ultimately, the string being evaluated here comes from Line 1342 in f6f681b
I'd like to see some unit tests for that function to understand what it outputs, but I don't get any results for such tests:
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. If this PR passes CI, then it will be proven to work in Windows, and that’s good enough. 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. Based on the example provided in #48740 (comment), I have updated the regular expressions to match Unix and Windows paths. Please take a look: |
||||||||
assert.match(import.meta.filename, fileReg); | ||||||||
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 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 import.meta.dirname'; | ||||||||
assert.strictEqual(dataDirname, undefined); | ||||||||
aduh95 marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include <node_api.h> | ||
|
||
EXTERN_C_START | ||
napi_value Init(napi_env env, napi_value exports); | ||
EXTERN_C_END | ||
|
||
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) |
Uh oh!
There was an error while loading. Please reload this page.