-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
module: expose resolveLoadAndCache
API
#55756
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
base: main
Are you sure you want to change the base?
Changes from all commits
0bf0a30
be50311
0629c4d
74dca81
e04c622
434cbf2
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 |
---|---|---|
|
@@ -15,18 +15,26 @@ | |
enableCompileCache, | ||
flushCompileCache, | ||
getCompileCacheDir, | ||
resolve, | ||
load, | ||
resolveAndLoad, | ||
} = require('internal/modules/helpers'); | ||
const { | ||
findPackageJSON, | ||
} = require('internal/modules/package_json_reader'); | ||
const { stripTypeScriptTypes } = require('internal/modules/typescript'); | ||
|
||
Module.register = register; | ||
Module.constants = constants; | ||
Module.enableCompileCache = enableCompileCache; | ||
Module.findPackageJSON = findPackageJSON; | ||
Module.findSourceMap = findSourceMap; | ||
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. Seems like unrelated changes. |
||
Module.flushCompileCache = flushCompileCache; | ||
Module.getCompileCacheDir = getCompileCacheDir; | ||
Module.register = register; | ||
Module.resolve= resolve; | ||
Module.load = load; | ||
Module.resolveAndLoad= resolveAndLoad; | ||
Module.SourceMap = SourceMap; | ||
Module.stripTypeScriptTypes = stripTypeScriptTypes; | ||
|
||
// SourceMap APIs | ||
|
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "commonjs" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const foo: string = 'Hello, TypeScript!'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const foo: string = 'Hello, TypeScript!'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const foo: string = 'Hello, TypeScript!'; |
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 { resolveLoadAndCache } = 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( | ||
Check failure on line 16 in test/parallel/test-module-loadmodule-no-typescript.js
|
||
async () => { | ||
await resolveLoadAndCache(fileUrl, parentURL); | ||
}, | ||
{ | ||
code: 'ERR_UNKNOWN_FILE_EXTENSION', | ||
} | ||
); | ||
}); |
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 { resolveLoadAndCache } = 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 resolveLoadAndCache(fileUrl, parentURL); | ||
Check failure on line 19 in test/parallel/test-module-loadmodule-typescript.js
|
||
assert.strictEqual(format, 'commonjs-typescript'); | ||
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 resolveLoadAndCache(fileUrl, parentURL); | ||
assert.strictEqual(format, 'commonjs-typescript'); | ||
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 resolveLoadAndCache(fileUrl, parentURL); | ||
assert.strictEqual(format, 'module-typescript'); | ||
assert.strictEqual(url, fileUrl.href); | ||
|
||
// Built-in TypeScript loader loads the source. | ||
assert.ok(Buffer.isBuffer(source)); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'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 { resolveLoadAndCache } = require('node:module'); | ||
|
||
const parentURL = pathToFileURL(__filename); | ||
|
||
test('should throw if the module is not found', async () => { | ||
await assert.rejects( | ||
Check failure on line 13 in test/parallel/test-module-loadmodule.js
|
||
async () => { | ||
await resolveLoadAndCache('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 resolveLoadAndCache(fileUrl, parentURL); | ||
assert.strictEqual(format, 'commonjs'); | ||
assert.strictEqual(url, fileUrl.href); | ||
|
||
// `source` is null and the final builtin loader will read the file. | ||
assert.strictEqual(source, null); | ||
}); |
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.
When
--experimental-strip-types
is enabled and performing this operation on a.ts
file, thisformat
may containscommonjs-typescript
andmodule-typescript
which are not documented at https://nodejs.org/api/module.html#loadurl-context-nextload as final formats.I think we should convert
commonjs-typescript
andmodule-typescript
tocommonjs
andmodule
respectively. Alternatively, document the other two and make them public.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.
I think we probably want to document it, as it's certainly going to be helpful for the ecosystem to know which files are TypeScript