-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
module: expose module format by module loader #57777
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
Open
legendecas
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
legendecas:module/resolve
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ const { | |
let defaultResolve, defaultLoad, defaultLoadSync, importMetaInitializer; | ||
|
||
const { tracingChannel } = require('diagnostics_channel'); | ||
const { validateObject } = require('internal/validators'); | ||
const onImport = tracingChannel('module.import'); | ||
|
||
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => { | ||
|
@@ -1061,9 +1062,40 @@ function register(specifier, parentURL = undefined, options) { | |
); | ||
} | ||
|
||
async function loadModule(specifier, parentURL, options = kEmptyObject) { | ||
specifier = `${specifier}`; | ||
parentURL = `${parentURL}`; | ||
|
||
validateObject(options, 'options'); | ||
const { importAttributes = { __proto__: null } } = options; | ||
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. Nit: This |
||
validateObject(importAttributes, 'options.importAttributes'); | ||
|
||
const loader = getOrInitializeCascadedLoader(); | ||
const { url, format: resolvedFormat } = await loader.resolve(specifier, parentURL, importAttributes); | ||
const result = await loader.load(url, { format: resolvedFormat, importAttributes }); | ||
const { source } = result; | ||
let { format: finalFormat } = result; | ||
|
||
// Translate internal formats to public ones. | ||
if (finalFormat === 'commonjs-typescript') { | ||
finalFormat = 'commonjs'; | ||
} | ||
if (finalFormat === 'module-typescript') { | ||
finalFormat = 'module'; | ||
} | ||
|
||
return { | ||
__proto__: null, | ||
url, | ||
format: finalFormat, | ||
source, | ||
}; | ||
} | ||
|
||
module.exports = { | ||
createModuleLoader, | ||
getHooksProxy, | ||
getOrInitializeCascadedLoader, | ||
register, | ||
loadModule, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Legacy TypeScript Module | ||
|
||
When `tsconfig.json` is set to `module: "node16"` or any `node*`, the TypeScript compiler will | ||
produce the output in the format by the extension (e.g. `.cts` or `.mts`), or set by the | ||
`package.json#type` field, regardless of the syntax of the original source code. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "commonjs" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const foo: string = 'Hello, TypeScript!'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const foo: string = 'Hello, TypeScript!'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const foo: string = 'Hello, TypeScript!'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Flags: --no-experimental-strip-types | ||
|
||
'use strict'; | ||
|
||
require('../common'); | ||
const test = require('node:test'); | ||
const assert = require('node:assert'); | ||
const { pathToFileURL } = require('node:url'); | ||
const fixtures = require('../common/fixtures'); | ||
const { loadModule } = require('node:module'); | ||
|
||
const parentURL = pathToFileURL(__filename); | ||
|
||
test('should reject a TypeScript module', async () => { | ||
const fileUrl = fixtures.fileURL('typescript/legacy-module/test-module-export.ts'); | ||
await assert.rejects( | ||
async () => { | ||
await loadModule(fileUrl, parentURL); | ||
}, | ||
{ | ||
code: 'ERR_UNKNOWN_FILE_EXTENSION', | ||
} | ||
); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Flags: --experimental-strip-types | ||
|
||
'use strict'; | ||
|
||
require('../common'); | ||
const test = require('node:test'); | ||
const assert = require('node:assert'); | ||
const { pathToFileURL } = require('node:url'); | ||
const fixtures = require('../common/fixtures'); | ||
const { loadModule, kModuleFormats } = require('node:module'); | ||
|
||
const parentURL = pathToFileURL(__filename); | ||
|
||
test('should load a TypeScript module source by package.json type', async () => { | ||
// Even if the .ts file contains module syntax, it should be loaded as a CommonJS module | ||
// because the package.json type is set to "commonjs". | ||
|
||
const fileUrl = fixtures.fileURL('typescript/legacy-module/test-module-export.ts'); | ||
const { url, format, source } = await loadModule(fileUrl, parentURL); | ||
assert.strictEqual(format, kModuleFormats.commonjs); | ||
assert.strictEqual(url, fileUrl.href); | ||
|
||
// Built-in TypeScript loader loads the source. | ||
assert.ok(Buffer.isBuffer(source)); | ||
}); | ||
|
||
test('should load a TypeScript cts module source by extension', async () => { | ||
// By extension, .cts files should be loaded as CommonJS modules. | ||
|
||
const fileUrl = fixtures.fileURL('typescript/legacy-module/test-module-export.cts'); | ||
const { url, format, source } = await loadModule(fileUrl, parentURL); | ||
assert.strictEqual(format, kModuleFormats.commonjs); | ||
assert.strictEqual(url, fileUrl.href); | ||
|
||
// Built-in TypeScript loader loads the source. | ||
assert.ok(Buffer.isBuffer(source)); | ||
}); | ||
|
||
test('should load a TypeScript mts module source by extension', async () => { | ||
// By extension, .mts files should be loaded as ES modules. | ||
|
||
const fileUrl = fixtures.fileURL('typescript/legacy-module/test-module-export.mts'); | ||
const { url, format, source } = await loadModule(fileUrl, parentURL); | ||
assert.strictEqual(format, kModuleFormats.module); | ||
assert.strictEqual(url, fileUrl.href); | ||
|
||
// Built-in TypeScript loader loads the source. | ||
assert.ok(Buffer.isBuffer(source)); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const test = require('node:test'); | ||
const assert = require('node:assert'); | ||
const { pathToFileURL } = require('node:url'); | ||
const fixtures = require('../common/fixtures'); | ||
const { loadModule, kModuleFormats } = require('node:module'); | ||
|
||
const parentURL = pathToFileURL(__filename); | ||
|
||
test('kModuleFormats is a frozen object', () => { | ||
assert.ok(typeof kModuleFormats === 'object'); | ||
assert.ok(Object.isFrozen(kModuleFormats)); | ||
}); | ||
|
||
test('should throw if the module is not found', async () => { | ||
await assert.rejects( | ||
async () => { | ||
await loadModule('nonexistent-module', parentURL); | ||
}, | ||
{ | ||
code: 'ERR_MODULE_NOT_FOUND', | ||
} | ||
); | ||
}); | ||
|
||
test('should load a module', async () => { | ||
const fileUrl = fixtures.fileURL('es-modules/cjs.js'); | ||
const { url, format, source } = await loadModule(fileUrl, parentURL); | ||
assert.strictEqual(format, kModuleFormats.commonjs); | ||
assert.strictEqual(url, fileUrl.href); | ||
|
||
// `source` is null and the final builtin loader will read the file. | ||
assert.strictEqual(source, null); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This returns a Promise, but I didn't find a good example on how to document the object properties.
Eg:
node/doc/api/readline.md
Line 536 in bd3f271
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.
And maybe you could add more information what could make this promise to rejects, like specifier not found, etc...