From 23b37235c5dad2c5b7e831e9f572a665b4e6cebb Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Thu, 7 Dec 2023 16:57:12 -0800 Subject: [PATCH] Prototype localmodule/localcommonjs --- src/compiler/commandLineParser.ts | 2 ++ src/compiler/program.ts | 15 +++++++++++++++ src/compiler/types.ts | 2 ++ 3 files changed, 19 insertions(+) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 4d7bceb5360..6a21c6cdf50 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -580,6 +580,8 @@ const moduleSubOptionDeclarations: readonly CommandLineOption[] = [ nodenext: ModuleFormatDetectionKind.NodeNext, defaultmodule: ModuleFormatDetectionKind.DefaultModule, defaultcommonjs: ModuleFormatDetectionKind.DefaultCommonJS, + localmodule: ModuleFormatDetectionKind.LocalModule, + localcommonjs: ModuleFormatDetectionKind.LocalCommonJS, })), affectsModuleResolution: true, affectsSemanticDiagnostics: true, diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 539e8feec51..ce08fb9621f 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1341,6 +1341,21 @@ export function getImpliedNodeFormatForFileWorker( fileExtensionIsOneOf(fileName, [Extension.Dcts, Extension.Cts, Extension.Cjs]) ? ModuleKind.CommonJS : fileExtensionIsOneOf(fileName, [Extension.Dts, Extension.Ts, Extension.Tsx, Extension.Js, Extension.Jsx]) ? lookupFromPackageJson(defaultFormat) : undefined; // other extensions, like `json` or `tsbuildinfo`, are set as `undefined` here but they should never be fed through the transformer pipeline + case ModuleFormatDetectionKind.LocalModule: + case ModuleFormatDetectionKind.LocalCommonJS: + if (fileExtensionIsOneOf(fileName, [Extension.Dmts, Extension.Mts, Extension.Mjs])) { + return ModuleKind.ESNext; + } + if (fileExtensionIsOneOf(fileName, [Extension.Dcts, Extension.Cts, Extension.Cjs])) { + return ModuleKind.CommonJS; + } + if (!isDeclarationFileName(fileName) && fileExtensionIsOneOf(fileName, [Extension.Ts, Extension.Tsx, Extension.Js, Extension.Jsx])) { + // TODO: replace with more reliable check for "is this an emittable source file of this program" + // TODO: what about nested package.json directories? Should special behavior be restricted to the + // one in scope of the tsconfig? + return formatDetection === ModuleFormatDetectionKind.LocalModule ? ModuleKind.ESNext : ModuleKind.CommonJS; + } + return lookupFromPackageJson(/*defaultFormat*/ ModuleKind.CommonJS); default: return undefined; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index a636899b09b..f9bf15d3270 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -7288,6 +7288,8 @@ export enum ModuleFormatDetectionKind { Bundler = 1, DefaultModule = 2, DefaultCommonJS = 3, + LocalModule = 4, + LocalCommonJS = 5, Node16 = 100, NodeNext = 199, }