Add --moduleFormatDetection=bundler

This commit is contained in:
Andrew Branch
2023-11-20 15:50:07 -08:00
parent d78fbd810e
commit 854ba6fb34
3 changed files with 16 additions and 6 deletions
+2
View File
@@ -1156,9 +1156,11 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
name: "moduleFormatDetection",
type: new Map(Object.entries({
none: ModuleFormatDetectionKind.None,
bundler: ModuleFormatDetectionKind.Bundler,
node16: ModuleFormatDetectionKind.Node16,
nodenext: ModuleFormatDetectionKind.NodeNext,
})),
affectsModuleResolution: true,
category: Diagnostics.Modules,
description: Diagnostics.Specify_how_files_are_determined_to_be_ECMAScript_modules_or_CommonJS_modules,
defaultValueDescription: Diagnostics.node16_when_module_is_node16_nodenext_when_module_is_nodenext_none_otherwise,
+13 -6
View File
@@ -128,6 +128,7 @@ import {
getLineStarts,
getMatchedFileSpec,
getMatchedIncludeSpec,
getModuleFormatDetectionKind,
getNewLineCharacter,
getNormalizedAbsolutePath,
getNormalizedAbsolutePathWithoutRoot,
@@ -225,6 +226,7 @@ import {
ModifierLike,
ModuleBlock,
ModuleDeclaration,
ModuleFormatDetectionKind,
ModuleKind,
ModuleResolutionCache,
ModuleResolutionHost,
@@ -1326,23 +1328,28 @@ export function getImpliedNodeFormatForFileWorker(
host: ModuleResolutionHost,
options: CompilerOptions,
) {
switch (getEmitModuleResolutionKind(options)) {
case ModuleResolutionKind.Node16:
case ModuleResolutionKind.NodeNext:
const formatDetection = getModuleFormatDetectionKind(options);
switch (formatDetection) {
case ModuleFormatDetectionKind.Bundler:
case ModuleFormatDetectionKind.Node16:
case ModuleFormatDetectionKind.NodeNext:
const defaultFormat = formatDetection === ModuleFormatDetectionKind.Bundler ? undefined : ModuleKind.CommonJS;
return fileExtensionIsOneOf(fileName, [Extension.Dmts, Extension.Mts, Extension.Mjs]) ? ModuleKind.ESNext :
fileExtensionIsOneOf(fileName, [Extension.Dcts, Extension.Cts, Extension.Cjs]) ? ModuleKind.CommonJS :
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(defaultFormat) :
undefined; // other extensions, like `json` or `tsbuildinfo`, are set as `undefined` here but they should never be fed through the transformer pipeline
default:
return undefined;
}
function lookupFromPackageJson(): Partial<CreateSourceFileOptions> {
function lookupFromPackageJson(defaultFormat: ResolutionMode): Partial<CreateSourceFileOptions> {
const state = getTemporaryModuleResolutionState(packageJsonInfoCache, host, options);
const packageJsonLocations: string[] = [];
state.failedLookupLocations = packageJsonLocations;
state.affectingLocations = packageJsonLocations;
const packageJsonScope = getPackageScopeForPath(fileName, state);
const impliedNodeFormat = packageJsonScope?.contents.packageJsonContent.type === "module" ? ModuleKind.ESNext : ModuleKind.CommonJS;
const impliedNodeFormat = packageJsonScope?.contents.packageJsonContent.type === "module" ? ModuleKind.ESNext :
packageJsonScope?.contents.packageJsonContent.type === "commonjs" ? ModuleKind.CommonJS :
defaultFormat;
return { impliedNodeFormat, packageJsonLocations, packageJsonScope };
}
}
+1
View File
@@ -7284,6 +7284,7 @@ export enum ModuleKind {
export enum ModuleFormatDetectionKind {
None = 0,
Bundler = 1,
Node16 = 100,
NodeNext = 199,
}