-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
esm: provide named exports for all builtin libraries #18131
Changes from all commits
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 |
---|---|---|
|
@@ -59,11 +59,18 @@ translators.set('cjs', async (url) => { | |
// through normal resolution | ||
translators.set('builtin', async (url) => { | ||
debug(`Translating BuiltinModule ${url}`); | ||
return createDynamicModule(['default'], url, (reflect) => { | ||
debug(`Loading BuiltinModule ${url}`); | ||
const exports = NativeModule.require(url.slice(5)); | ||
reflect.exports.default.set(exports); | ||
}); | ||
// slice 'node:' scheme | ||
const id = url.slice(5); | ||
NativeModule.require(id); | ||
const module = NativeModule.getCached(id); | ||
return createDynamicModule( | ||
[...module.exportKeys, 'default'], url, (reflect) => { | ||
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. i'm not sure if the ordering matters here at all - is https://tc39.github.io/ecma262/#sec-modulenamespacecreate step 7 suggests that all export keys, including "default" if present, should be alphabetically sorted. (i do see at least one test that validates the ordering, but i'm not sure if that test covers this code or not) 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 order doesn't matter there actually as it just gets injected into a generated source text |
||
debug(`Loading BuiltinModule ${url}`); | ||
module.reflect = reflect; | ||
for (const key of module.exportKeys) | ||
reflect.exports[key].set(module.exports[key]); | ||
reflect.exports.default.set(module.exports); | ||
}); | ||
}); | ||
|
||
// Strategy for loading a node native module | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Flags: --experimental-modules | ||
|
||
import '../common'; | ||
import assert from 'assert'; | ||
|
||
import fs, { readFile } from 'fs'; | ||
|
||
const s = Symbol(); | ||
const fn = () => s; | ||
|
||
delete fs.readFile; | ||
assert.strictEqual(fs.readFile, undefined); | ||
assert.strictEqual(readFile, undefined); | ||
|
||
fs.readFile = fn; | ||
|
||
assert.strictEqual(fs.readFile(), s); | ||
assert.strictEqual(readFile(), s); | ||
|
||
Reflect.deleteProperty(fs, 'readFile'); | ||
|
||
Reflect.defineProperty(fs, 'readFile', { | ||
value: fn, | ||
configurable: true, | ||
writable: true, | ||
}); | ||
|
||
assert.strictEqual(fs.readFile(), s); | ||
assert.strictEqual(readFile(), s); | ||
|
||
Reflect.deleteProperty(fs, 'readFile'); | ||
assert.strictEqual(fs.readFile, undefined); | ||
assert.strictEqual(readFile, undefined); | ||
|
||
Reflect.defineProperty(fs, 'readFile', { | ||
get() { return fn; }, | ||
set() {}, | ||
configurable: true, | ||
}); | ||
|
||
assert.strictEqual(fs.readFile(), s); | ||
assert.strictEqual(readFile(), s); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import _url from 'url'; | ||
import { URL } from 'url'; | ||
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. No need to import URL at all in Node.js v10.x :) 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. you never know where this might be backported to |
||
|
||
const builtins = new Set( | ||
Object.keys(process.binding('natives')).filter(str => | ||
/^(?!(?:internal|node|v8)\/)/.test(str)) | ||
) | ||
|
||
const baseURL = new _url.URL('file://'); | ||
const baseURL = new URL('file://'); | ||
baseURL.pathname = process.cwd() + '/'; | ||
|
||
export function resolve (specifier, base = baseURL) { | ||
|
@@ -15,7 +16,7 @@ export function resolve (specifier, base = baseURL) { | |
}; | ||
} | ||
// load all dependencies as esm, regardless of file extension | ||
const url = new _url.URL(specifier, base).href; | ||
const url = new URL(specifier, base).href; | ||
return { | ||
url, | ||
format: 'esm' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it ok that
delete Array.prototype.includes
can break this code?If not, you could copy
Array.prototype.includes
to be an own property onthis.exportKeys
, perhaps?(same question on
has
/get
/set
on collection instances)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ljharb There's another issue (here) for isolating internals to avoid ad hoc primordial scaffolding in each module. Trying to isolate
includes
,Object.keys
,Reflect
or other is probably out of scope for this specific PR (esp. since it's experimental and the issue is larger than this PR).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point, just wanted to call it out :-)