Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
cleanup code
  • Loading branch information
gasnier committed May 5, 2021
commit 093ef04bbb5ae72084c73101d6027f677c203730
42 changes: 20 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ function successLoader(

const filePath =
instance.loaderOptions.appendTsSuffixTo.length > 0 ||
instance.loaderOptions.appendTsxSuffixTo.length > 0
instance.loaderOptions.appendTsxSuffixTo.length > 0
? appendSuffixesIfMatch(
{
'.ts': instance.loaderOptions.appendTsSuffixTo,
'.tsx': instance.loaderOptions.appendTsxSuffixTo,
},
rawFilePath
)
{
'.ts': instance.loaderOptions.appendTsSuffixTo,
'.tsx': instance.loaderOptions.appendTsxSuffixTo,
},
rawFilePath
)
: rawFilePath;

const fileVersion = updateFileInCache(
Expand Down Expand Up @@ -109,10 +109,10 @@ function makeSourceMapAndFinish(
? ' The most common cause for this is having errors when building referenced projects.'
: !instance.loaderOptions.allowTsInNodeModules &&
filePath.indexOf('node_modules') !== -1
? ' By default, ts-loader will not compile .ts files in node_modules.\n' +
? ' By default, ts-loader will not compile .ts files in node_modules.\n' +
'You should not need to recompile .ts files there, but if you really want to, use the allowTsInNodeModules option.\n' +
'See: https://github.com/Microsoft/TypeScript/issues/12358'
: '';
: '';

callback(
new Error(
Expand Down Expand Up @@ -274,7 +274,6 @@ function makeLoaderOptions(instanceName: string, loaderOptions: LoaderOptions) {
compilerOptions: {},
appendTsSuffixTo: [],
appendTsxSuffixTo: [],
transformers: {},
happyPackMode: false,
colors: true,
onlyCompileBundledFiles: false,
Expand Down Expand Up @@ -428,7 +427,7 @@ function getEmit(
// the real dependency that webpack should watch is the JS output file.
addDependency(
getInputFileNameFromOutput(instance, path.resolve(resolvedFileName)) ||
originalFileName
originalFileName
);
}
}
Expand All @@ -441,16 +440,16 @@ function getEmit(
'@' +
(isReferencedFile(instance, defFilePath)
? instance
.solutionBuilderHost!.getInputFileStamp(defFilePath)
.toString()
.solutionBuilderHost!.getInputFileStamp(defFilePath)
.toString()
: (
instance.files.get(instance.filePathKeyMapper(defFilePath)) ||
instance.otherFiles.get(
instance.filePathKeyMapper(defFilePath)
) || {
version: '?',
}
).version)
instance.files.get(instance.filePathKeyMapper(defFilePath)) ||
instance.otherFiles.get(
instance.filePathKeyMapper(defFilePath)
) || {
version: '?',
}
).version)
);

return getOutputAndSourceMapFromOutputFiles(outputFiles);
Expand Down Expand Up @@ -604,7 +603,6 @@ function getTranspilationEmit(
diagnostics,
} = instance.compiler.transpileModule(contents, {
compilerOptions: { ...instance.compilerOptions, rootDir: undefined },
transformers: instance.transformers,
reportDiagnostics: true,
fileName,
});
Expand Down Expand Up @@ -660,5 +658,5 @@ export = loader;
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace loader {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Options extends LoaderOptions {}
export interface Options extends LoaderOptions { }
}
56 changes: 26 additions & 30 deletions src/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ function createFilePathKeyMapper(
const filePathKey = pathResolve(filePath);
cachedPath = fileNameLowerCaseRegExp.test(filePathKey)
? (filePathKey.replace(fileNameLowerCaseRegExp, ch =>
ch.toLowerCase()
) as FilePathKey)
ch.toLowerCase()
) as FilePathKey)
: filePathKey;
filePathMapperCache.set(filePath, cachedPath);
}
Expand Down Expand Up @@ -197,15 +197,15 @@ function successfulTypeScriptInstance(

const appendTsTsxSuffixesIfRequired =
loaderOptions.appendTsSuffixTo.length > 0 ||
loaderOptions.appendTsxSuffixTo.length > 0
loaderOptions.appendTsxSuffixTo.length > 0
? (filePath: string) =>
appendSuffixesIfMatch(
{
'.ts': loaderOptions.appendTsSuffixTo,
'.tsx': loaderOptions.appendTsxSuffixTo,
},
filePath
)
appendSuffixesIfMatch(
{
'.ts': loaderOptions.appendTsSuffixTo,
'.tsx': loaderOptions.appendTsxSuffixTo,
},
filePath
)
: (filePath: string) => filePath;

if (loaderOptions.transpileOnly) {
Expand All @@ -222,7 +222,6 @@ function successfulTypeScriptInstance(
version: 0,
program: undefined, // temporary, to be set later
dependencyGraph: new Map(),
transformers: {} as typescript.CustomTransformers, // this is only set temporarily, custom transformers are created further down
colors,
initialSetupPending: true,
reportTranspileErrors: true,
Expand All @@ -245,8 +244,8 @@ function successfulTypeScriptInstance(
try {
const filesToLoad = loaderOptions.onlyCompileBundledFiles
? configParseResult.fileNames.filter(fileName =>
dtsDtsxOrDtsDtsxMapRegex.test(fileName)
)
dtsDtsxOrDtsDtsxMapRegex.test(fileName)
)
: configParseResult.fileNames;
filesToLoad.forEach(filePath => {
normalizedFilePath = path.normalize(filePath);
Expand Down Expand Up @@ -279,7 +278,6 @@ function successfulTypeScriptInstance(
otherFiles,
languageService: null,
version: 0,
transformers: {} as typescript.CustomTransformers, // this is only set temporarily, custom transformers are created further down
dependencyGraph: new Map(),
colors,
initialSetupPending: true,
Expand Down Expand Up @@ -349,10 +347,10 @@ export function initializeInstance(
instance.program =
instance.configParseResult.projectReferences !== undefined
? instance.compiler.createProgram({
rootNames: instance.configParseResult.fileNames,
options: instance.configParseResult.options,
projectReferences: instance.configParseResult.projectReferences,
})
rootNames: instance.configParseResult.fileNames,
options: instance.configParseResult.options,
projectReferences: instance.configParseResult.projectReferences,
})
: instance.compiler.createProgram([], instance.compilerOptions);

// Setup watch run for solution building
Expand Down Expand Up @@ -550,13 +548,13 @@ function getOutputPathWithoutChangingExt(
) {
return outputDir
? (instance.compiler as any).resolvePath(
outputDir,
(instance.compiler as any).getRelativePathFromDirectory(
rootDirOfOptions(instance, configFile),
inputFileName,
ignoreCase
)
outputDir,
(instance.compiler as any).getRelativePathFromDirectory(
rootDirOfOptions(instance, configFile),
inputFileName,
ignoreCase
)
)
: inputFileName;
}

Expand All @@ -582,8 +580,8 @@ function getOutputJSFileName(
? '.json'
: fileExtensionIs(inputFileName, '.tsx') &&
configFile.options.jsx === instance.compiler.JsxEmit.Preserve
? '.jsx'
: '.js'
? '.jsx'
: '.js'
);
return !isJsonFile ||
(instance.compiler as any).comparePaths(
Expand Down Expand Up @@ -713,8 +711,7 @@ export function getEmitFromWatchHost(instance: TSInstance, filePath?: string) {
const result = builderProgram.emitNextAffectedFile(
writeFile,
/*cancellationToken*/ undefined,
/*emitOnlyDtsFiles*/ false,
instance.transformers
/*emitOnlyDtsFiles*/ false
);
if (!result) {
break;
Expand Down Expand Up @@ -762,8 +759,7 @@ export function getEmitOutput(instance: TSInstance, filePath: string) {
sourceFile,
writeFile,
/*cancellationToken*/ undefined,
/*emitOnlyDtsFiles*/ false,
instance.transformers
/*emitOnlyDtsFiles*/ false
);
return outputFiles;
} else {
Expand Down
17 changes: 8 additions & 9 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface CacheableHost extends HostMayBeCacheable {

export interface ModuleResolutionHostMayBeCacheable
extends typescript.ModuleResolutionHost,
HostMayBeCacheable {
HostMayBeCacheable {
readFile(filePath: string, encoding?: string): string | undefined;
trace: NonNullable<typescript.ModuleResolutionHost['trace']>;
directoryExists: NonNullable<
Expand All @@ -65,11 +65,11 @@ export interface ModuleResolutionHostMayBeCacheable

export interface ServiceHostWhichMayBeCacheable
extends typescript.LanguageServiceHost,
HostMayBeCacheable {}
HostMayBeCacheable { }

export interface WatchHost
extends typescript.WatchCompilerHostOfFilesAndCompilerOptions<typescript.EmitAndSemanticDiagnosticsBuilderProgram>,
HostMayBeCacheable {
HostMayBeCacheable {
invokeFileWatcher: WatchFactory['invokeFileWatcher'];
updateRootFileNames(): void;
outputFiles: Map<FilePathKey, typescript.OutputFile[]>;
Expand Down Expand Up @@ -104,7 +104,7 @@ export type FilePathKey = string & { __filePathKeyBrand: any };

export interface SolutionBuilderWithWatchHost
extends typescript.SolutionBuilderWithWatchHost<typescript.EmitAndSemanticDiagnosticsBuilderProgram>,
WatchFactory {
WatchFactory {
diagnostics: SolutionDiagnostics;
writtenFiles: typescript.OutputFile[];
configFileInfo: Map<FilePathKey, ConfigFileInfo>;
Expand Down Expand Up @@ -209,7 +209,6 @@ export interface TSInstance {
version: number;
dependencyGraph: DependencyGraph;
filesWithErrors?: TSFiles;
transformers: typescript.CustomTransformers;
colors: Chalk;

otherFiles: TSFiles;
Expand Down Expand Up @@ -282,10 +281,10 @@ export interface LoaderOptions {
appendTsxSuffixTo: (RegExp | string)[];
happyPackMode: boolean;
getCustomTransformers:
| string
| ((
program: typescript.Program
) => typescript.CustomTransformers | undefined);
| string
| ((
program: typescript.Program
) => typescript.CustomTransformers | undefined);
experimentalWatchApi: boolean;
allowTsInNodeModules: boolean;
experimentalFileCaching: boolean;
Expand Down
40 changes: 19 additions & 21 deletions src/servicesHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ function makeResolversAndModuleResolutionHost(
compilerOptions.newLine === constants.CarriageReturnLineFeedCode
? constants.CarriageReturnLineFeed
: compilerOptions.newLine === constants.LineFeedCode
? constants.LineFeed
: constants.EOL;
? constants.LineFeed
: constants.EOL;

// loader.context seems to work fine on Linux / Mac regardless causes problems for @types resolution on Windows for TypeScript < 2.3
const getCurrentDirectory = () => loader.context;
Expand Down Expand Up @@ -229,9 +229,7 @@ export function makeServicesHost(

// used for (/// <reference types="...">) see https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/250#issuecomment-485061329
resolveTypeReferenceDirectives,
resolveModuleNames,

getCustomTransformers: () => instance.transformers,
resolveModuleNames
};

return servicesHost;
Expand Down Expand Up @@ -980,12 +978,12 @@ export function makeSolutionBuilderHost(
configInfo.tsbuildInfoFile = instance.compiler
.getTsBuildInfoEmitOutputFilePath
? instance.compiler.getTsBuildInfoEmitOutputFilePath(
configInfo.config.options
)
configInfo.config.options
)
: // before api
(instance.compiler as any).getOutputPathForBuildInfo(
configInfo.config.options
);
(instance.compiler as any).getOutputPathForBuildInfo(
configInfo.config.options
);
}

function getOutputFileAndKeyFromReferencedProject(
Expand All @@ -994,9 +992,9 @@ export function makeSolutionBuilderHost(
const outputFile = ensureOutputFile(outputFileName);
return outputFile !== undefined
? {
key: getOutputFileKeyFromReferencedProject(outputFileName)!,
outputFile,
}
key: getOutputFileKeyFromReferencedProject(outputFileName)!,
outputFile,
}
: undefined;
}

Expand Down Expand Up @@ -1076,10 +1074,10 @@ export function makeSolutionBuilderHost(
const text = compiler.sys.readFile(outputFileName);
return text !== undefined
? {
name: outputFileName,
text,
writeByteOrderMark: false,
}
name: outputFileName,
text,
writeByteOrderMark: false,
}
: undefined;
}

Expand Down Expand Up @@ -1125,8 +1123,8 @@ export function makeSolutionBuilderHost(
existing == missingFileModifiedTime
? compiler.FileWatcherEventKind.Created
: newTime === missingFileModifiedTime
? compiler.FileWatcherEventKind.Deleted
: compiler.FileWatcherEventKind.Changed;
? compiler.FileWatcherEventKind.Deleted
: compiler.FileWatcherEventKind.Changed;
solutionBuilderHost.invokeFileWatcher(fileName, eventKind);
}
}
Expand Down Expand Up @@ -1239,7 +1237,7 @@ function resolveModule(
resolutionResult = { resolvedFileName, originalFileName };
}
}
} catch (e) {}
} catch (e) { }

const tsResolution = resolveModuleName(
moduleName,
Expand All @@ -1258,7 +1256,7 @@ function resolveModule(

return resolutionResult! === undefined ||
resolutionResult.resolvedFileName ===
tsResolutionResult.resolvedFileName ||
tsResolutionResult.resolvedFileName ||
isJsImplementationOfTypings(resolutionResult!, tsResolutionResult)
? tsResolutionResult
: resolutionResult!;
Expand Down
Loading