Skip to content
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 0 additions & 1 deletion src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,6 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
bundler: ModuleResolutionKind.Bundler,
})),
deprecatedKeys: new Set(["node"]),
affectsSourceFile: true,
affectsModuleResolution: true,
paramType: Diagnostics.STRATEGY,
category: Diagnostics.Modules,
Expand Down
52 changes: 18 additions & 34 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1421,12 +1421,9 @@ export function getImpliedNodeFormatForFileWorker(
host: ModuleResolutionHost,
options: CompilerOptions,
): ResolutionMode | Partial<CreateSourceFileOptions> | undefined {
const moduleResolution = getEmitModuleResolutionKind(options);
const shouldLookupFromPackageJson = ModuleResolutionKind.Node16 <= moduleResolution && moduleResolution <= ModuleResolutionKind.NodeNext
|| pathContainsNodeModules(fileName);
return fileExtensionIsOneOf(fileName, [Extension.Dmts, Extension.Mts, Extension.Mjs]) ? ModuleKind.ESNext :
fileExtensionIsOneOf(fileName, [Extension.Dcts, Extension.Cts, Extension.Cjs]) ? ModuleKind.CommonJS :
shouldLookupFromPackageJson && fileExtensionIsOneOf(fileName, [Extension.Dts, Extension.Ts, Extension.Tsx, Extension.Js, Extension.Jsx]) ? lookupFromPackageJson() :
fileExtensionIsOneOf(fileName, [Extension.Dts, Extension.Ts, Extension.Tsx, Extension.Js, Extension.Jsx]) ? lookupFromPackageJson() :
undefined; // other extensions, like `json` or `tsbuildinfo`, are set as `undefined` here but they should never be fed through the transformer pipeline

function lookupFromPackageJson(): Partial<CreateSourceFileOptions> {
Expand Down Expand Up @@ -3827,22 +3824,20 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
}

let redirectedPath: Path | undefined;
if (!useSourceOfProjectReferenceRedirect) {
const redirectProject = getProjectReferenceRedirectProject(fileName);
if (redirectProject) {
if (redirectProject.commandLine.options.outFile) {
// Shouldnt create many to 1 mapping file in --out scenario
return undefined;
}
const redirect = getProjectReferenceOutputName(redirectProject, fileName);
fileName = redirect;
// Once we start redirecting to a file, we can potentially come back to it
// via a back-reference from another file in the .d.ts folder. If that happens we'll
// end up trying to add it to the program *again* because we were tracking it via its
// original (un-redirected) name. So we have to map both the original path and the redirected path
// to the source file we're about to find/create
redirectedPath = toPath(redirect);
const redirectProject = getProjectReferenceRedirectProject(fileName);
if (!useSourceOfProjectReferenceRedirect && redirectProject) {
if (redirectProject.commandLine.options.outFile) {
// Shouldnt create many to 1 mapping file in --out scenario
return undefined;
}
const redirect = getProjectReferenceOutputName(redirectProject, fileName);
fileName = redirect;
// Once we start redirecting to a file, we can potentially come back to it
// via a back-reference from another file in the .d.ts folder. If that happens we'll
// end up trying to add it to the program *again* because we were tracking it via its
// original (un-redirected) name. So we have to map both the original path and the redirected path
// to the source file we're about to find/create
redirectedPath = toPath(redirect);
}

// We haven't looked for this file, do so now and cache result
Expand Down Expand Up @@ -5254,24 +5249,13 @@ export function getEmitModuleFormatOfFileWorker(sourceFile: Pick<SourceFile, "fi
/** @internal Prefer `program.getImpliedNodeFormatForEmit` when possible. */
export function getImpliedNodeFormatForEmitWorker(sourceFile: Pick<SourceFile, "fileName" | "impliedNodeFormat" | "packageJsonScope">, options: CompilerOptions): ResolutionMode {
const moduleKind = getEmitModuleKind(options);
if (ModuleKind.Node16 <= moduleKind && moduleKind <= ModuleKind.NodeNext) {
return sourceFile.impliedNodeFormat;
}
if (
sourceFile.impliedNodeFormat === ModuleKind.CommonJS
&& (sourceFile.packageJsonScope?.contents.packageJsonContent.type === "commonjs"
|| fileExtensionIsOneOf(sourceFile.fileName, [Extension.Cjs, Extension.Cts]))
ModuleKind.Node16 <= moduleKind && moduleKind <= ModuleKind.NodeNext
|| fileExtensionIsOneOf(sourceFile.fileName, [Extension.Cts, Extension.Dcts, Extension.Cjs, Extension.Mts, Extension.Dmts, Extension.Mjs])
|| pathContainsNodeModules(sourceFile.fileName)

This comment was marked as duplicate.

) {
return ModuleKind.CommonJS;
}
if (
sourceFile.impliedNodeFormat === ModuleKind.ESNext
&& (sourceFile.packageJsonScope?.contents.packageJsonContent.type === "module"
|| fileExtensionIsOneOf(sourceFile.fileName, [Extension.Mjs, Extension.Mts]))
) {
return ModuleKind.ESNext;
return sourceFile.impliedNodeFormat;
}
return undefined;
}
/** @internal Prefer `program.getDefaultResolutionModeForFile` when possible. */
export function getDefaultResolutionModeForFileWorker(sourceFile: Pick<SourceFile, "fileName" | "impliedNodeFormat" | "packageJsonScope">, options: CompilerOptions): ResolutionMode {
Expand Down
19 changes: 6 additions & 13 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4355,21 +4355,14 @@ export interface SourceFile extends Declaration, LocalsContainer {
languageVersion: ScriptTarget;

/**
* When `module` is `Node16` or `NodeNext`, this field controls whether the
* source file in question is an ESNext-output-format file, or a CommonJS-output-format
* module. This is derived by the module resolver as it looks up the file, since
* it is derived from either the file extension of the module, or the containing
* `package.json` context, and affects both checking and emit.
* This field controls whether the source file in question is an ESNext-output-format file,
* or a CommonJS-output-format module. This is derived by the module resolver as it looks
* up the file, since it is derived from either the file extension of the module, or the
* containing `package.json` context, and may affect both checking and emit, depending on
* `module` and `moduleResolution` compiler options.
*
* It is _public_ so that (pre)transformers can set this field,
* since it switches the builtin `node` module transform. Generally speaking, if unset,
* the field is treated as though it is `ModuleKind.CommonJS`.
*
* Note that this field is only set by the module resolution process when
* `moduleResolution` is `Node16` or `NodeNext`, which is implied by the `module` setting
* of `Node16` or `NodeNext`, respectively, but may be overriden (eg, by a `moduleResolution`
* of `node`). If so, this field will be unset and source files will be considered to be
* CommonJS-output-format by the node module transformer and type checker, regardless of extension or context.
* since it switches the builtin `node` module transform.
*/
impliedNodeFormat?: ResolutionMode;
/** @internal */ packageJsonLocations?: readonly string[];
Expand Down
6 changes: 3 additions & 3 deletions src/testRunner/unittests/tscWatch/incremental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,19 @@ describe("unittests:: tscWatch:: incremental:: emit file --incremental", () => {
version: system.createHash(libFile.content),
signature: system.createHash(libFile.content),
affectsGlobalScope: true,
impliedFormat: undefined,
impliedFormat: ts.ModuleKind.CommonJS,
});
assert.deepEqual(builderProgram.state.fileInfos.get(file1.path as ts.Path), {
version: system.createHash(file1.content),
signature: system.createHash(file1.content),
affectsGlobalScope: undefined,
impliedFormat: undefined,
impliedFormat: ts.ModuleKind.CommonJS,
});
assert.deepEqual(builderProgram.state.fileInfos.get(file2.path as ts.Path), {
version: system.createHash(fileModified.content),
signature: system.createHash(fileModified.content),
affectsGlobalScope: undefined,
impliedFormat: undefined,
impliedFormat: ts.ModuleKind.CommonJS,
});

assert.deepEqual(builderProgram.state.compilerOptions, {
Expand Down
23 changes: 20 additions & 3 deletions tests/baselines/reference/allowJsCrossMonorepoPackage.trace.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[
"Found 'package.json' at '/packages/main/package.json'.",
"======== Resolving module 'shared' from '/packages/main/index.ts'. ========",
"Explicitly specified module resolution kind: 'Bundler'.",
"Resolving in CJS mode with conditions 'import', 'types'.",
"Found 'package.json' at '/packages/main/package.json'.",
"File '/packages/main/package.json' exists according to earlier cached lookups.",
"Loading module 'shared' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Found 'package.json' at '/packages/main/node_modules/shared/package.json'.",
Expand Down Expand Up @@ -46,6 +47,7 @@
"Directory '/node_modules/@types' does not exist, skipping all lookups in it.",
"Resolving real path for '/packages/main/node_modules/shared/index.js', result '/packages/shared/index.js'.",
"======== Module name 'shared' was successfully resolved to '/packages/shared/index.js' with Package ID 'shared/[email protected]'. ========",
"Found 'package.json' at '/packages/shared/package.json'.",
"======== Resolving module './utils.js' from '/packages/shared/index.js'. ========",
"Explicitly specified module resolution kind: 'Bundler'.",
"Resolving in CJS mode with conditions 'import', 'types'.",
Expand All @@ -56,10 +58,11 @@
"File '/packages/shared/utils.d.ts' does not exist.",
"File '/packages/shared/utils.js' exists - use it as a name resolution result.",
"======== Module name './utils.js' was successfully resolved to '/packages/shared/utils.js'. ========",
"File '/packages/shared/package.json' exists according to earlier cached lookups.",
"======== Resolving module 'pkg' from '/packages/shared/utils.js'. ========",
"Explicitly specified module resolution kind: 'Bundler'.",
"Resolving in CJS mode with conditions 'import', 'types'.",
"Found 'package.json' at '/packages/shared/package.json'.",
"File '/packages/shared/package.json' exists according to earlier cached lookups.",
"Loading module 'pkg' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Directory '/packages/shared/node_modules' does not exist, skipping all lookups in it.",
Expand All @@ -76,6 +79,8 @@
"File '/node_modules/pkg/package.json' does not exist according to earlier cached lookups.",
"File '/node_modules/package.json' does not exist.",
"File '/package.json' does not exist.",
"File '/.ts/package.json' does not exist.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-es5' from '/packages/main/__lib_node_modules_lookup_lib.es5.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-es5' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand All @@ -90,6 +95,8 @@
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/packages/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-es5' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-decorators' from '/packages/main/__lib_node_modules_lookup_lib.decorators.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-decorators' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand All @@ -104,6 +111,8 @@
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/packages/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-decorators' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-decorators/legacy' from '/packages/main/__lib_node_modules_lookup_lib.decorators.legacy.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-decorators/legacy' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand All @@ -118,6 +127,8 @@
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/packages/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-decorators/legacy' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-dom' from '/packages/main/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand All @@ -132,6 +143,8 @@
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/packages/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-dom' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-webworker/importscripts' from '/packages/main/__lib_node_modules_lookup_lib.webworker.importscripts.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-webworker/importscripts' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand All @@ -146,6 +159,8 @@
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/packages/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-webworker/importscripts' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-scripthost' from '/packages/main/__lib_node_modules_lookup_lib.scripthost.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-scripthost' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand All @@ -159,5 +174,7 @@
"Loading module '@typescript/lib-scripthost' from 'node_modules' folder, target file types: JavaScript.",
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/packages/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-scripthost' was not resolved. ========"
"======== Module name '@typescript/lib-scripthost' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups."
]
19 changes: 6 additions & 13 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5894,21 +5894,14 @@ declare namespace ts {
hasNoDefaultLib: boolean;
languageVersion: ScriptTarget;
/**
* When `module` is `Node16` or `NodeNext`, this field controls whether the
* source file in question is an ESNext-output-format file, or a CommonJS-output-format
* module. This is derived by the module resolver as it looks up the file, since
* it is derived from either the file extension of the module, or the containing
* `package.json` context, and affects both checking and emit.
* This field controls whether the source file in question is an ESNext-output-format file,
* or a CommonJS-output-format module. This is derived by the module resolver as it looks
* up the file, since it is derived from either the file extension of the module, or the
* containing `package.json` context, and may affect both checking and emit, depending on
* `module` and `moduleResolution` compiler options.
*
* It is _public_ so that (pre)transformers can set this field,
* since it switches the builtin `node` module transform. Generally speaking, if unset,
* the field is treated as though it is `ModuleKind.CommonJS`.
*
* Note that this field is only set by the module resolution process when
* `moduleResolution` is `Node16` or `NodeNext`, which is implied by the `module` setting
* of `Node16` or `NodeNext`, respectively, but may be overriden (eg, by a `moduleResolution`
* of `node`). If so, this field will be unset and source files will be considered to be
* CommonJS-output-format by the node module transformer and type checker, regardless of extension or context.
* since it switches the builtin `node` module transform.
*/
impliedNodeFormat?: ResolutionMode;
}
Expand Down
Loading