ts-loader
Advanced tools
Comparing version 8.0.14 to 8.4.0
# Changelog | ||
## v8.4.0 | ||
* [fix: cater for change in resolveTypeReferenceDirective API in 4.7](https://github.com/TypeStrong/ts-loader/pull/1446) - thanks @dragomirtitian | ||
* This is a backport from v9.2.7 for webpack 4 compatibility | ||
## v8.3.0 | ||
* [Fixed impossibility to have several instances of ts-loader with different compiler options](https://github.com/TypeStrong/ts-loader/issues/1316) - thanks @timocov | ||
* This is a backport from v9.2.0 for webpack 4 compatibility | ||
## v8.2.0 | ||
* [Use caches for module resolution and type reference directives when using compiler default functions](https://github.com/TypeStrong/ts-loader/pull/1287) - thanks @sheetalkamat - uses: https://github.com/microsoft/TypeScript/pull/43700 | ||
* This is a backport from v9.1.0 for webpack 4 compatibility | ||
## v8.1.0 | ||
* [feat: remove top-level typescript import statements](https://github.com/TypeStrong/ts-loader/pull/1259) - thanks @ulivz | ||
## v8.0.18 | ||
* [Perf: Optimize fileExists callback path](https://github.com/TypeStrong/ts-loader/issues/1266) - thanks @berickson1 | ||
## v8.0.17 | ||
* [Included correct webpack source location in emitted errors](https://github.com/TypeStrong/ts-loader/issues/1199) - thanks @lorenzodallavecchia | ||
## v8.0.16 | ||
* [Re-Fixed missing errors in watch mode in webpack5](https://github.com/TypeStrong/ts-loader/issues/1204) - thanks @appzuka | ||
## v8.0.15 | ||
* [Update definition files in watch mode in webpack@5](https://github.com/TypeStrong/ts-loader/pull/1249) - thanks @appzuka,@JonWallsten,@alexander-akait | ||
* [Add afterDeclarations to getCustomTransformers in README.md](https://github.com/TypeStrong/ts-loader/pull/1248) - thanks @appzuka | ||
## v8.0.14 | ||
@@ -3,0 +35,0 @@ * [Upgrade `chalk`, `loader-utils`, and `semver` to latest stable versions](https://github.com/TypeStrong/ts-loader/pull/1237) - thanks Avi Vahl |
@@ -9,3 +9,3 @@ import * as webpack from 'webpack'; | ||
*/ | ||
export declare function makeAfterCompile(instance: TSInstance, addAssets: boolean, provideErrors: boolean, configFilePath: string | undefined): (compilation: webpack.compilation.Compilation, callback: () => void) => void; | ||
export declare function makeAfterCompile(instance: TSInstance, configFilePath: string | undefined): (compilation: webpack.compilation.Compilation, callback: () => void) => void; | ||
//# sourceMappingURL=after-compile.d.ts.map |
@@ -14,3 +14,3 @@ "use strict"; | ||
*/ | ||
function makeAfterCompile(instance, addAssets, provideErrors, configFilePath) { | ||
function makeAfterCompile(instance, configFilePath) { | ||
let getCompilerOptionDiagnostics = true; | ||
@@ -25,5 +25,3 @@ let checkAllFilesForErrors = true; | ||
if (instance.loaderOptions.transpileOnly) { | ||
if (addAssets) { | ||
provideAssetsFromSolutionBuilderHost(instance, compilation); | ||
} | ||
provideAssetsFromSolutionBuilderHost(instance, compilation); | ||
callback(); | ||
@@ -33,5 +31,3 @@ return; | ||
removeCompilationTSLoaderErrors(compilation, instance.loaderOptions); | ||
if (provideErrors) { | ||
provideCompilerOptionDiagnosticErrorsToWebpack(getCompilerOptionDiagnostics, compilation, instance, configFilePath); | ||
} | ||
provideCompilerOptionDiagnosticErrorsToWebpack(getCompilerOptionDiagnostics, compilation, instance, configFilePath); | ||
getCompilerOptionDiagnostics = false; | ||
@@ -42,11 +38,7 @@ const modules = determineModules(compilation, instance); | ||
const filesWithErrors = new Map(); | ||
if (provideErrors) { | ||
provideErrorsToWebpack(filesToCheckForErrors, filesWithErrors, compilation, modules, instance); | ||
provideSolutionErrorsToWebpack(compilation, modules, instance); | ||
} | ||
if (addAssets) { | ||
provideDeclarationFilesToWebpack(filesToCheckForErrors, instance, compilation); | ||
provideTsBuildInfoFilesToWebpack(instance, compilation); | ||
provideAssetsFromSolutionBuilderHost(instance, compilation); | ||
} | ||
provideErrorsToWebpack(filesToCheckForErrors, filesWithErrors, compilation, modules, instance); | ||
provideSolutionErrorsToWebpack(compilation, modules, instance); | ||
provideDeclarationFilesToWebpack(filesToCheckForErrors, instance, compilation); | ||
provideTsBuildInfoFilesToWebpack(instance, compilation); | ||
provideAssetsFromSolutionBuilderHost(instance, compilation); | ||
instance.filesWithErrors = filesWithErrors; | ||
@@ -65,3 +57,3 @@ instance.modifiedFiles = undefined; | ||
const { languageService, loaderOptions, compiler, program } = instance; | ||
const errors = utils_1.formatErrors(program === undefined | ||
const errors = (0, utils_1.formatErrors)(program === undefined | ||
? languageService.getCompilerOptionsDiagnostics() | ||
@@ -111,6 +103,6 @@ : program.getOptionsDiagnostics(), loaderOptions, instance.colors, compiler, { file: configFilePath || 'tsconfig.json' }, compilation.compiler.context); | ||
modifiedFiles.size) { | ||
const reverseDependencyGraph = utils_1.populateReverseDependencyGraph(instance); | ||
const reverseDependencyGraph = (0, utils_1.populateReverseDependencyGraph)(instance); | ||
// check all modified files, and all dependants | ||
for (const modifiedFileName of modifiedFiles.keys()) { | ||
for (const fileName of utils_1.collectAllDependants(reverseDependencyGraph, modifiedFileName).keys()) { | ||
for (const fileName of (0, utils_1.collectAllDependants)(reverseDependencyGraph, modifiedFileName).keys()) { | ||
const fileToCheckForErrors = files.get(fileName) || otherFiles.get(fileName); | ||
@@ -129,3 +121,3 @@ addFileToCheckForErrors(fileName, fileToCheckForErrors); | ||
function addFileToCheckForErrors(filePath, file) { | ||
if (!utils_1.isReferencedFile(instance, filePath)) { | ||
if (!(0, utils_1.isReferencedFile)(instance, filePath)) { | ||
filesToCheckForErrors.set(filePath, file); | ||
@@ -141,3 +133,3 @@ } | ||
// I’m pretty sure this will never be undefined here | ||
const program = utils_1.ensureProgram(instance); | ||
const program = (0, utils_1.ensureProgram)(instance); | ||
for (const [filePath, { fileName }] of filesToCheckForErrors.entries()) { | ||
@@ -166,3 +158,3 @@ if (fileName.match(filePathRegex) === null) { | ||
// append errors | ||
const formattedErrors = utils_1.formatErrors(errors, loaderOptions, instance.colors, compiler, { module }, compilation.compiler.context); | ||
const formattedErrors = (0, utils_1.formatErrors)(errors, loaderOptions, instance.colors, compiler, { module }, compilation.compiler.context); | ||
formattedErrors.forEach(error => { | ||
@@ -181,3 +173,3 @@ if (module.addError) { | ||
// otherwise it's a more generic error | ||
const formattedErrors = utils_1.formatErrors(errors, loaderOptions, instance.colors, compiler, { file: fileName }, compilation.compiler.context); | ||
const formattedErrors = (0, utils_1.formatErrors)(errors, loaderOptions, instance.colors, compiler, { file: fileName }, compilation.compiler.context); | ||
compilation.errors.push(...formattedErrors); | ||
@@ -201,3 +193,3 @@ } | ||
// append errors | ||
const formattedErrors = utils_1.formatErrors(perFileDiagnostics, loaderOptions, instance.colors, compiler, { module }, compilation.compiler.context); | ||
const formattedErrors = (0, utils_1.formatErrors)(perFileDiagnostics, loaderOptions, instance.colors, compiler, { module }, compilation.compiler.context); | ||
formattedErrors.forEach(error => { | ||
@@ -216,3 +208,3 @@ if (module.addError) { | ||
// otherwise it's a more generic error | ||
const formattedErrors = utils_1.formatErrors(perFileDiagnostics, loaderOptions, instance.colors, compiler, { file: path.resolve(perFileDiagnostics[0].file.fileName) }, compilation.compiler.context); | ||
const formattedErrors = (0, utils_1.formatErrors)(perFileDiagnostics, loaderOptions, instance.colors, compiler, { file: path.resolve(perFileDiagnostics[0].file.fileName) }, compilation.compiler.context); | ||
compilation.errors.push(...formattedErrors); | ||
@@ -222,3 +214,3 @@ } | ||
// Add global solution errors | ||
compilation.errors.push(...utils_1.formatErrors(diagnostics.global, instance.loaderOptions, instance.colors, instance.compiler, { file: 'tsconfig.json' }, compilation.compiler.context)); | ||
compilation.errors.push(...(0, utils_1.formatErrors)(diagnostics.global, instance.loaderOptions, instance.colors, instance.compiler, { file: 'tsconfig.json' }, compilation.compiler.context)); | ||
} | ||
@@ -233,3 +225,3 @@ /** | ||
} | ||
addDeclarationFilesAsAsset(instances_1.getEmitOutput(instance, fileName), compilation); | ||
addDeclarationFilesAsAsset((0, instances_1.getEmitOutput)(instance, fileName), compilation); | ||
} | ||
@@ -262,3 +254,3 @@ } | ||
// Ensure emit is complete | ||
instances_1.getEmitFromWatchHost(instance); | ||
(0, instances_1.getEmitFromWatchHost)(instance); | ||
if (instance.watchHost.tsbuildinfo) { | ||
@@ -289,3 +281,3 @@ outputFileToAsset(instance.watchHost.tsbuildinfo, compilation); | ||
function removeCompilationTSLoaderErrors(compilation, loaderOptions) { | ||
compilation.errors = compilation.errors.filter(error => error.loaderSource !== utils_1.tsLoaderSource(loaderOptions)); | ||
compilation.errors = compilation.errors.filter(error => error.loaderSource !== (0, utils_1.tsLoaderSource)(loaderOptions)); | ||
} | ||
@@ -303,9 +295,9 @@ function removeModuleTSLoaderError(module, loaderOptions) { | ||
Array.from(errors || []) | ||
.filter((error) => error.loaderSource !== utils_1.tsLoaderSource(loaderOptions)) | ||
.filter((error) => error.loaderSource !== (0, utils_1.tsLoaderSource)(loaderOptions)) | ||
.forEach(error => module.addError(error)); | ||
} | ||
else { | ||
module.errors = module.errors.filter(error => error.loaderSource !== utils_1.tsLoaderSource(loaderOptions)); | ||
module.errors = module.errors.filter(error => error.loaderSource !== (0, utils_1.tsLoaderSource)(loaderOptions)); | ||
} | ||
} | ||
//# sourceMappingURL=after-compile.js.map |
@@ -1,2 +0,2 @@ | ||
import * as typescript from 'typescript'; | ||
import type * as typescript from 'typescript'; | ||
import { LoaderOptions } from './interfaces'; | ||
@@ -10,3 +10,3 @@ import * as logger from './logger'; | ||
}; | ||
export declare function getCompilerOptions(configParseResult: typescript.ParsedCommandLine): typescript.CompilerOptions; | ||
export declare function getCompilerOptions(configParseResult: typescript.ParsedCommandLine, compiler: typeof typescript): typescript.CompilerOptions; | ||
//# sourceMappingURL=compilerSetup.d.ts.map |
@@ -5,3 +5,2 @@ "use strict"; | ||
const semver = require("semver"); | ||
const typescript = require("typescript"); | ||
function getCompiler(loaderOptions, log) { | ||
@@ -46,6 +45,6 @@ let compiler; | ||
exports.getCompiler = getCompiler; | ||
function getCompilerOptions(configParseResult) { | ||
function getCompilerOptions(configParseResult, compiler) { | ||
const compilerOptions = Object.assign({}, configParseResult.options, { | ||
skipLibCheck: true, | ||
suppressOutputPathCheck: true, | ||
suppressOutputPathCheck: true, // This is why: https://github.com/Microsoft/TypeScript/issues/7363 | ||
}); | ||
@@ -55,4 +54,4 @@ // if `module` is not specified and not using ES6+ target, default to CJS module output | ||
compilerOptions.target !== undefined && | ||
compilerOptions.target < typescript.ScriptTarget.ES2015) { | ||
compilerOptions.module = typescript.ModuleKind.CommonJS; | ||
compilerOptions.target < compiler.ScriptTarget.ES2015) { | ||
compilerOptions.module = compiler.ModuleKind.CommonJS; | ||
} | ||
@@ -59,0 +58,0 @@ if (configParseResult.options.configFile) { |
import { Chalk } from 'chalk'; | ||
import * as typescript from 'typescript'; | ||
import type * as typescript from 'typescript'; | ||
import * as webpack from 'webpack'; | ||
@@ -4,0 +4,0 @@ import { LoaderOptions, WebpackError } from './interfaces'; |
@@ -20,3 +20,3 @@ "use strict"; | ||
if (configFile.error !== undefined) { | ||
configFileError = utils_1.formatErrors([configFile.error], loaderOptions, colors, compiler, { file: configFilePath }, loader.context)[0]; | ||
configFileError = (0, utils_1.formatErrors)([configFile.error], loaderOptions, colors, compiler, { file: configFilePath }, loader.context)[0]; | ||
} | ||
@@ -85,3 +85,3 @@ } | ||
function getConfigParseResult(compiler, configFile, basePath, configFilePath, loaderOptions) { | ||
const configParseResult = compiler.parseJsonConfigFileContent(configFile.config, Object.assign(Object.assign({}, compiler.sys), { useCaseSensitiveFileNames: utils_1.useCaseSensitiveFileNames(compiler, loaderOptions) }), basePath, getCompilerOptionsToExtend(compiler, loaderOptions, basePath, configFilePath || 'tsconfig.json')); | ||
const configParseResult = compiler.parseJsonConfigFileContent(configFile.config, Object.assign(Object.assign({}, compiler.sys), { useCaseSensitiveFileNames: (0, utils_1.useCaseSensitiveFileNames)(compiler, loaderOptions) }), basePath, getCompilerOptionsToExtend(compiler, loaderOptions, basePath, configFilePath || 'tsconfig.json')); | ||
if (!loaderOptions.projectReferences) { | ||
@@ -99,7 +99,7 @@ configParseResult.projectReferences = undefined; | ||
function getParsedCommandLine(compiler, loaderOptions, configFilePath) { | ||
const result = compiler.getParsedCommandLineOfConfigFile(configFilePath, getCompilerOptionsToExtend(compiler, loaderOptions, path.dirname(configFilePath), configFilePath), Object.assign(Object.assign({}, compiler.sys), { useCaseSensitiveFileNames: utils_1.useCaseSensitiveFileNames(compiler, loaderOptions), | ||
const result = compiler.getParsedCommandLineOfConfigFile(configFilePath, getCompilerOptionsToExtend(compiler, loaderOptions, path.dirname(configFilePath), configFilePath), Object.assign(Object.assign({}, compiler.sys), { useCaseSensitiveFileNames: (0, utils_1.useCaseSensitiveFileNames)(compiler, loaderOptions), | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
onUnRecoverableConfigFileDiagnostic: () => { } }), extendedConfigCache); | ||
if (result) { | ||
result.options = compilerSetup_1.getCompilerOptions(result); | ||
result.options = (0, compilerSetup_1.getCompilerOptions)(result, compiler); | ||
} | ||
@@ -106,0 +106,0 @@ return result; |
@@ -16,3 +16,3 @@ "use strict"; | ||
const options = getLoaderOptions(this); | ||
const instanceOrError = instances_1.getTypeScriptInstance(options, this); | ||
const instanceOrError = (0, instances_1.getTypeScriptInstance)(options, this); | ||
if (instanceOrError.error !== undefined) { | ||
@@ -23,12 +23,12 @@ callback(new Error(instanceOrError.error.message)); | ||
const instance = instanceOrError.instance; | ||
instances_1.buildSolutionReferences(instance, this); | ||
(0, instances_1.buildSolutionReferences)(instance, this); | ||
successLoader(this, contents, callback, instance); | ||
} | ||
function successLoader(loaderContext, contents, callback, instance) { | ||
instances_1.initializeInstance(loaderContext, instance); | ||
instances_1.reportTranspileErrors(instance, loaderContext); | ||
(0, instances_1.initializeInstance)(loaderContext, instance); | ||
(0, instances_1.reportTranspileErrors)(instance, loaderContext); | ||
const rawFilePath = path.normalize(loaderContext.resourcePath); | ||
const filePath = instance.loaderOptions.appendTsSuffixTo.length > 0 || | ||
instance.loaderOptions.appendTsxSuffixTo.length > 0 | ||
? utils_1.appendSuffixesIfMatch({ | ||
? (0, utils_1.appendSuffixesIfMatch)({ | ||
'.ts': instance.loaderOptions.appendTsSuffixTo, | ||
@@ -47,3 +47,3 @@ '.tsx': instance.loaderOptions.appendTsxSuffixTo, | ||
setModuleMeta(loaderContext, instance, fileVersion); | ||
const additionalGuidance = utils_1.isReferencedFile(instance, filePath) | ||
const additionalGuidance = (0, utils_1.isReferencedFile)(instance, filePath) | ||
? ' The most common cause for this is having errors when building referenced projects.' | ||
@@ -83,4 +83,5 @@ : !instance.loaderOptions.allowTsInNodeModules && | ||
const value = loaderOptions[key]; | ||
if (value) { | ||
hash.update(key + value.toString()); | ||
if (value !== undefined) { | ||
const valueString = typeof value === 'function' ? value.toString() : JSON.stringify(value); | ||
hash.update(key + valueString); | ||
} | ||
@@ -184,3 +185,3 @@ }); | ||
}, loaderOptions); | ||
options.ignoreDiagnostics = utils_1.arrify(options.ignoreDiagnostics).map(Number); | ||
options.ignoreDiagnostics = (0, utils_1.arrify)(options.ignoreDiagnostics).map(Number); | ||
options.logLevel = options.logLevel.toUpperCase(); | ||
@@ -204,3 +205,3 @@ options.instance = instanceName; | ||
if (file !== undefined) { | ||
if (!utils_1.isReferencedFile(instance, filePath)) { | ||
if (!(0, utils_1.isReferencedFile)(instance, filePath)) { | ||
instance.otherFiles.delete(key); | ||
@@ -216,3 +217,3 @@ instance.files.set(key, file); | ||
file = { fileName: filePath, version: 0 }; | ||
if (!utils_1.isReferencedFile(instance, filePath)) { | ||
if (!(0, utils_1.isReferencedFile)(instance, filePath)) { | ||
instance.files.set(key, file); | ||
@@ -232,3 +233,3 @@ instance.changedFilesList = true; | ||
// See https://github.com/TypeStrong/ts-loader/issues/943 | ||
if (!utils_1.isReferencedFile(instance, filePath) && | ||
if (!(0, utils_1.isReferencedFile)(instance, filePath) && | ||
!instance.rootFileNames.has(filePath) && | ||
@@ -269,3 +270,3 @@ // however, be careful not to add files from node_modules unless | ||
var _a; | ||
const outputFiles = instances_1.getEmitOutput(instance, filePath); | ||
const outputFiles = (0, instances_1.getEmitOutput)(instance, filePath); | ||
loaderContext.clearDependencies(); | ||
@@ -280,3 +281,3 @@ loaderContext.addDependency(rawFilePath); | ||
// Make this file dependent on *all* definition files in the program | ||
if (!utils_1.isReferencedFile(instance, filePath)) { | ||
if (!(0, utils_1.isReferencedFile)(instance, filePath)) { | ||
for (const { fileName: defFilePath } of instance.files.values()) { | ||
@@ -297,3 +298,3 @@ if (defFilePath.match(constants.dtsDtsxOrDtsDtsxMapRegex) && | ||
// the real dependency that webpack should watch is the JS output file. | ||
addDependency(instances_1.getInputFileNameFromOutput(instance, path.resolve(resolvedFileName)) || | ||
addDependency((0, instances_1.getInputFileNameFromOutput)(instance, path.resolve(resolvedFileName)) || | ||
originalFileName); | ||
@@ -305,3 +306,3 @@ } | ||
'@' + | ||
(utils_1.isReferencedFile(instance, defFilePath) | ||
((0, utils_1.isReferencedFile)(instance, defFilePath) | ||
? instance | ||
@@ -333,3 +334,3 @@ .solutionBuilderHost.getInputFileStamp(defFilePath) | ||
const resolvedFilePath = instance.filePathKeyMapper(filePath); | ||
if (!utils_1.isReferencedFile(instance, filePath)) { | ||
if (!(0, utils_1.isReferencedFile)(instance, filePath)) { | ||
if (instance.configParseResult.fileNames.some(f => instance.filePathKeyMapper(f) === resolvedFilePath)) { | ||
@@ -398,3 +399,3 @@ addDependenciesFromProjectReferences(instance, instance.configFilePath, instance.configParseResult.projectReferences, addDependency); | ||
function getTranspilationEmit(fileName, contents, instance, loaderContext) { | ||
if (utils_1.isReferencedFile(instance, fileName)) { | ||
if ((0, utils_1.isReferencedFile)(instance, fileName)) { | ||
const outputFiles = instance.solutionBuilderHost.getOutputFilesFromReferencedProjectInput(fileName); | ||
@@ -414,3 +415,3 @@ addDependenciesFromSolutionBuilder(instance, fileName, file => loaderContext.addDependency(path.resolve(file))); | ||
if (!instance.loaderOptions.happyPackMode) { | ||
const errors = utils_1.formatErrors(diagnostics, instance.loaderOptions, instance.colors, instance.compiler, { module }, loaderContext.context); | ||
const errors = (0, utils_1.formatErrors)(diagnostics, instance.loaderOptions, instance.colors, instance.compiler, { module }, loaderContext.context); | ||
/** | ||
@@ -417,0 +418,0 @@ * Since webpack 5, the `errors` property is deprecated, |
@@ -1,2 +0,2 @@ | ||
import * as typescript from 'typescript'; | ||
import type * as typescript from 'typescript'; | ||
import * as webpack from 'webpack'; | ||
@@ -3,0 +3,0 @@ import { LoaderOptions, TSInstance, WebpackError } from './interfaces'; |
@@ -26,6 +26,6 @@ "use strict"; | ||
function getTypeScriptInstance(loaderOptions, loader) { | ||
const existing = instance_cache_1.getTSInstanceFromCache(loader._compiler, loaderOptions.instance); | ||
const existing = (0, instance_cache_1.getTSInstanceFromCache)(loader._compiler, loaderOptions.instance); | ||
if (existing) { | ||
if (!existing.initialSetupPending) { | ||
utils_1.ensureProgram(existing); | ||
(0, utils_1.ensureProgram)(existing); | ||
} | ||
@@ -37,6 +37,6 @@ return { instance: existing }; | ||
const log = logger.makeLogger(loaderOptions, colors); | ||
const compiler = compilerSetup_1.getCompiler(loaderOptions, log); | ||
const compiler = (0, compilerSetup_1.getCompiler)(loaderOptions, log); | ||
if (compiler.errorMessage !== undefined) { | ||
return { | ||
error: utils_1.makeError(loaderOptions, colors.red(compiler.errorMessage), undefined), | ||
error: (0, utils_1.makeError)(loaderOptions, colors.red(compiler.errorMessage), undefined), | ||
}; | ||
@@ -52,3 +52,3 @@ } | ||
const fileNameLowerCaseRegExp = /[^\u0130\u0131\u00DFa-z0-9\\/:\-_\. ]+/g; | ||
return utils_1.useCaseSensitiveFileNames(compiler, loaderOptions) | ||
return (0, utils_1.useCaseSensitiveFileNames)(compiler, loaderOptions) | ||
? pathResolve | ||
@@ -77,7 +77,7 @@ : toFileNameLowerCase; | ||
function successfulTypeScriptInstance(loaderOptions, loader, log, colors, compiler, compilerCompatible, compilerDetailsLogMessage) { | ||
const configFileAndPath = config_1.getConfigFile(compiler, colors, loader, loaderOptions, compilerCompatible, log, compilerDetailsLogMessage); | ||
const configFileAndPath = (0, config_1.getConfigFile)(compiler, colors, loader, loaderOptions, compilerCompatible, log, compilerDetailsLogMessage); | ||
if (configFileAndPath.configFileError !== undefined) { | ||
const { message, file } = configFileAndPath.configFileError; | ||
return { | ||
error: utils_1.makeError(loaderOptions, colors.red('error while reading tsconfig.json:' + constants_1.EOL + message), file), | ||
error: (0, utils_1.makeError)(loaderOptions, colors.red('error while reading tsconfig.json:' + constants_1.EOL + message), file), | ||
}; | ||
@@ -92,3 +92,3 @@ } | ||
// Reuse the instance if config file for project references is shared. | ||
instance_cache_1.setTSInstanceInCache(loader._compiler, loaderOptions.instance, existing); | ||
(0, instance_cache_1.setTSInstanceInCache)(loader._compiler, loaderOptions.instance, existing); | ||
return { instance: existing }; | ||
@@ -99,5 +99,5 @@ } | ||
const basePath = loaderOptions.context || path.dirname(configFilePath || ''); | ||
const configParseResult = config_1.getConfigParseResult(compiler, configFile, basePath, configFilePath, loaderOptions); | ||
const configParseResult = (0, config_1.getConfigParseResult)(compiler, configFile, basePath, configFilePath, loaderOptions); | ||
if (configParseResult.errors.length > 0 && !loaderOptions.happyPackMode) { | ||
const errors = utils_1.formatErrors(configParseResult.errors, loaderOptions, colors, compiler, { file: configFilePath }, loader.context); | ||
const errors = (0, utils_1.formatErrors)(configParseResult.errors, loaderOptions, colors, compiler, { file: configFilePath }, loader.context); | ||
/** | ||
@@ -114,6 +114,6 @@ * Since webpack 5, the `errors` property is deprecated, | ||
return { | ||
error: utils_1.makeError(loaderOptions, colors.red('error while parsing tsconfig.json'), configFilePath), | ||
error: (0, utils_1.makeError)(loaderOptions, colors.red('error while parsing tsconfig.json'), configFilePath), | ||
}; | ||
} | ||
const compilerOptions = compilerSetup_1.getCompilerOptions(configParseResult); | ||
const compilerOptions = (0, compilerSetup_1.getCompilerOptions)(configParseResult, compiler); | ||
const rootFileNames = new Set(); | ||
@@ -124,3 +124,3 @@ const files = new Map(); | ||
loaderOptions.appendTsxSuffixTo.length > 0 | ||
? (filePath) => utils_1.appendSuffixesIfMatch({ | ||
? (filePath) => (0, utils_1.appendSuffixesIfMatch)({ | ||
'.ts': loaderOptions.appendTsSuffixTo, | ||
@@ -153,3 +153,3 @@ '.tsx': loaderOptions.appendTsxSuffixTo, | ||
}; | ||
instance_cache_1.setTSInstanceInCache(loader._compiler, loaderOptions.instance, transpileInstance); | ||
(0, instance_cache_1.setTSInstanceInCache)(loader._compiler, loaderOptions.instance, transpileInstance); | ||
return { instance: transpileInstance }; | ||
@@ -175,3 +175,3 @@ } | ||
return { | ||
error: utils_1.makeError(loaderOptions, colors.red(`A file specified in tsconfig.json could not be found: ${normalizedFilePath}`), normalizedFilePath), | ||
error: (0, utils_1.makeError)(loaderOptions, colors.red(`A file specified in tsconfig.json could not be found: ${normalizedFilePath}`), normalizedFilePath), | ||
}; | ||
@@ -198,3 +198,3 @@ } | ||
}; | ||
instance_cache_1.setTSInstanceInCache(loader._compiler, loaderOptions.instance, instance); | ||
(0, instance_cache_1.setTSInstanceInCache)(loader._compiler, loaderOptions.instance, instance); | ||
return { instance }; | ||
@@ -218,15 +218,22 @@ } | ||
// add makeAfterCompile with addAssets = true to emit assets and report errors | ||
loader._compiler.hooks.afterCompile.tapAsync('ts-loader', after_compile_1.makeAfterCompile(instance, true, true, instance.configFilePath)); | ||
loader._compiler.hooks.afterCompile.tapAsync('ts-loader', (0, after_compile_1.makeAfterCompile)(instance, instance.configFilePath)); | ||
} | ||
: (loader, instance) => { | ||
// We must be running under webpack 5+ | ||
// Add makeAfterCompile with addAssets = false to suppress emitting assets | ||
// during the afterCompile stage. Errors will be still be reported to webpack | ||
loader._compiler.hooks.afterCompile.tapAsync('ts-loader', after_compile_1.makeAfterCompile(instance, false, true, instance.configFilePath)); | ||
// Emit the assets at the afterProcessAssets stage | ||
loader._compilation.hooks.afterProcessAssets.tap('ts-loader', (_) => { | ||
after_compile_1.makeAfterCompile(instance, true, false, instance.configFilePath)(loader._compilation, () => { | ||
// makeAfterCompile is a closure. It returns a function which closes over the variable checkAllFilesForErrors | ||
// We need to get the function once and then reuse it, otherwise it will be recreated each time | ||
// and all files will always be checked. | ||
const cachedMakeAfterCompile = (0, after_compile_1.makeAfterCompile)(instance, instance.configFilePath); | ||
// compilation is actually of type webpack.compilation.Compilation, but afterProcessAssets | ||
// only exists in webpack5 and at the time of writing ts-loader is built using webpack4 | ||
const makeAssetsCallback = (compilation) => { | ||
compilation.hooks.afterProcessAssets.tap('ts-loader', () => cachedMakeAfterCompile(compilation, () => { | ||
return null; | ||
}); | ||
}); | ||
})); | ||
}; | ||
// We need to add the hook above for each run. | ||
// For the first run, we just need to add the hook to loader._compilation | ||
makeAssetsCallback(loader._compilation); | ||
// For future calls in watch mode we need to watch for a new compilation and add the hook | ||
loader._compiler.hooks.compilation.tap('ts-loader', makeAssetsCallback); | ||
// It may be better to add assets at the processAssets stage (https://webpack.js.org/api/compilation-hooks/#processassets) | ||
@@ -252,3 +259,3 @@ // This requires Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL, which does not exist in webpack4 | ||
catch (err) { | ||
throw new Error(`Failed to load customTransformers from "${instance.loaderOptions.getCustomTransformers}": ${err.message}`); | ||
throw new Error(`Failed to load customTransformers from "${instance.loaderOptions.getCustomTransformers}": ${err instanceof Error ? err.message : ''}`); | ||
} | ||
@@ -273,3 +280,3 @@ if (typeof customerTransformers !== 'function') { | ||
addAssetHooks(loader, instance); | ||
loader._compiler.hooks.watchRun.tapAsync('ts-loader', watch_run_1.makeWatchRun(instance, loader)); | ||
loader._compiler.hooks.watchRun.tapAsync('ts-loader', (0, watch_run_1.makeWatchRun)(instance, loader)); | ||
} | ||
@@ -284,3 +291,3 @@ } | ||
// If there is api available for watch, use it instead of language service | ||
instance.watchHost = servicesHost_1.makeWatchHost(getScriptRegexp(instance), loader, instance, instance.configParseResult.projectReferences); | ||
instance.watchHost = (0, servicesHost_1.makeWatchHost)(getScriptRegexp(instance), loader, instance, instance.configParseResult.projectReferences); | ||
instance.watchOfFilesAndCompilerOptions = instance.compiler.createWatchProgram(instance.watchHost); | ||
@@ -292,3 +299,3 @@ instance.builderProgram = instance.watchOfFilesAndCompilerOptions.getProgram(); | ||
else { | ||
instance.servicesHost = servicesHost_1.makeServicesHost(getScriptRegexp(instance), loader, instance, instance.configParseResult.projectReferences); | ||
instance.servicesHost = (0, servicesHost_1.makeServicesHost)(getScriptRegexp(instance), loader, instance, instance.configParseResult.projectReferences); | ||
instance.languageService = instance.compiler.createLanguageService(instance.servicesHost, instance.compiler.createDocumentRegistry()); | ||
@@ -298,3 +305,3 @@ instance.transformers = getCustomTransformers(instance.languageService.getProgram()); | ||
addAssetHooks(loader, instance); | ||
loader._compiler.hooks.watchRun.tapAsync('ts-loader', watch_run_1.makeWatchRun(instance, loader)); | ||
loader._compiler.hooks.watchRun.tapAsync('ts-loader', (0, watch_run_1.makeWatchRun)(instance, loader)); | ||
} | ||
@@ -324,5 +331,5 @@ } | ||
if (!instance.loaderOptions.happyPackMode) { | ||
const solutionErrors = servicesHost_1.getSolutionErrors(instance, loader.context); | ||
const solutionErrors = (0, servicesHost_1.getSolutionErrors)(instance, loader.context); | ||
const diagnostics = instance.program.getOptionsDiagnostics(); | ||
const errors = utils_1.formatErrors(diagnostics, instance.loaderOptions, instance.colors, instance.compiler, { file: instance.configFilePath || 'tsconfig.json' }, loader.context); | ||
const errors = (0, utils_1.formatErrors)(diagnostics, instance.loaderOptions, instance.colors, instance.compiler, { file: instance.configFilePath || 'tsconfig.json' }, loader.context); | ||
/** | ||
@@ -342,3 +349,3 @@ * Since webpack 5, the `errors` property is deprecated, | ||
function buildSolutionReferences(instance, loader) { | ||
if (!utils_1.supportsSolutionBuild(instance)) { | ||
if (!(0, utils_1.supportsSolutionBuild)(instance)) { | ||
return; | ||
@@ -350,3 +357,3 @@ } | ||
const scriptRegex = getScriptRegexp(instance); | ||
instance.solutionBuilderHost = servicesHost_1.makeSolutionBuilderHost(scriptRegex, loader, instance); | ||
instance.solutionBuilderHost = (0, servicesHost_1.makeSolutionBuilderHost)(scriptRegex, loader, instance); | ||
const solutionBuilder = instance.compiler.createSolutionBuilderWithWatch(instance.solutionBuilderHost, instance.configParseResult.projectReferences.map(ref => ref.path), { verbose: true }); | ||
@@ -417,3 +424,3 @@ solutionBuilder.build(); | ||
function getOutputFileNames(instance, configFile, inputFileName) { | ||
const ignoreCase = !utils_1.useCaseSensitiveFileNames(instance.compiler, instance.loaderOptions); | ||
const ignoreCase = !(0, utils_1.useCaseSensitiveFileNames)(instance.compiler, instance.loaderOptions); | ||
if (instance.compiler.getOutputFileNames) { | ||
@@ -449,3 +456,3 @@ return instance.compiler.getOutputFileNames(configFile, inputFileName, ignoreCase); | ||
} | ||
const program = utils_1.ensureProgram(instance); | ||
const program = (0, utils_1.ensureProgram)(instance); | ||
return (program && | ||
@@ -464,3 +471,3 @@ program.getResolvedProjectReferences && | ||
function getEmitFromWatchHost(instance, filePath) { | ||
const program = utils_1.ensureProgram(instance); | ||
const program = (0, utils_1.ensureProgram)(instance); | ||
const builderProgram = instance.builderProgram; | ||
@@ -511,6 +518,6 @@ if (builderProgram && program) { | ||
} | ||
if (utils_1.isReferencedFile(instance, filePath)) { | ||
if ((0, utils_1.isReferencedFile)(instance, filePath)) { | ||
return instance.solutionBuilderHost.getOutputFilesFromReferencedProjectInput(filePath); | ||
} | ||
const program = utils_1.ensureProgram(instance); | ||
const program = (0, utils_1.ensureProgram)(instance); | ||
if (program !== undefined) { | ||
@@ -517,0 +524,0 @@ const sourceFile = program.getSourceFile(filePath); |
@@ -1,2 +0,2 @@ | ||
import * as typescript from 'typescript'; | ||
import type * as typescript from 'typescript'; | ||
import { Chalk } from 'chalk'; | ||
@@ -14,5 +14,13 @@ import * as logger from './logger'; | ||
export declare type FileLocation = { | ||
/** 1-based */ | ||
line: number; | ||
/** 1-based */ | ||
character: number; | ||
}; | ||
export declare type WebpackSourcePosition = { | ||
/** 1-based */ | ||
line: number; | ||
/** 0-based */ | ||
column?: number; | ||
}; | ||
export interface WebpackError { | ||
@@ -22,2 +30,6 @@ module?: any; | ||
message: string; | ||
loc?: { | ||
start: WebpackSourcePosition; | ||
end?: WebpackSourcePosition; | ||
}; | ||
location?: FileLocation; | ||
@@ -64,3 +76,3 @@ loaderSource: string; | ||
} | ||
export interface WatchHost extends typescript.WatchCompilerHostOfFilesAndCompilerOptions<typescript.EmitAndSemanticDiagnosticsBuilderProgram> { | ||
export interface WatchHost extends typescript.WatchCompilerHostOfFilesAndCompilerOptions<typescript.EmitAndSemanticDiagnosticsBuilderProgram>, HostMayBeCacheable { | ||
invokeFileWatcher: WatchFactory['invokeFileWatcher']; | ||
@@ -125,2 +137,20 @@ updateRootFileNames(): void; | ||
} | ||
interface CacheWithRedirects<T> { | ||
ownMap: Map<string, T>; | ||
redirectsMap: Map<typescript.Path, Map<string, T>>; | ||
getOrCreateMapOfCacheRedirects(redirectedReference: typescript.ResolvedProjectReference | undefined): Map<string, T>; | ||
clear(): void; | ||
setOwnOptions(newOptions: typescript.CompilerOptions): void; | ||
setOwnMap(newOwnMap: Map<string, T>): void; | ||
} | ||
interface PerModuleNameCache { | ||
get(directory: string): typescript.ResolvedModuleWithFailedLookupLocations | undefined; | ||
set(directory: string, result: typescript.ResolvedModuleWithFailedLookupLocations): void; | ||
} | ||
export interface ModuleResolutionCache extends typescript.ModuleResolutionCache { | ||
directoryToModuleNameMap: CacheWithRedirects<Map<string, typescript.ResolvedModuleWithFailedLookupLocations>>; | ||
moduleNameToDirectoryMap: CacheWithRedirects<PerModuleNameCache>; | ||
clear(): void; | ||
update(compilerOptions: typescript.CompilerOptions): void; | ||
} | ||
export interface TSInstance { | ||
@@ -133,2 +163,4 @@ compiler: typeof typescript; | ||
rootFileNames: Set<string>; | ||
moduleResolutionCache?: ModuleResolutionCache; | ||
typeReferenceResolutionCache?: typescript.TypeReferenceDirectiveResolutionCache; | ||
/** | ||
@@ -228,3 +260,14 @@ * a cache of all the files | ||
} | ||
export interface TSCommon { | ||
resolveTypeReferenceDirective(typeReferenceDirectiveName: string, containingFile: string | undefined, options: typescript.CompilerOptions, host: typescript.ModuleResolutionHost, redirectedReference?: typescript.ResolvedProjectReference, cache?: typescript.TypeReferenceDirectiveResolutionCache, resolutionMode?: typescript.SourceFile['impliedNodeFormat']): typescript.ResolvedTypeReferenceDirectiveWithFailedLookupLocations; | ||
} | ||
/** | ||
* Compiler APIs we use that are marked internal and not included in TypeScript's public API declarations | ||
* @internal | ||
*/ | ||
export interface TSInternal { | ||
getModeForFileReference?: (ref: typescript.FileReference | string, containingFileMode: typescript.SourceFile['impliedNodeFormat']) => typescript.SourceFile['impliedNodeFormat']; | ||
} | ||
export declare type Severity = 'error' | 'warning'; | ||
export {}; | ||
//# sourceMappingURL=interfaces.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import * as typescript from 'typescript'; | ||
import type * as typescript from 'typescript'; | ||
import * as webpack from 'webpack'; | ||
@@ -3,0 +3,0 @@ import { FilePathKey, ServiceHostWhichMayBeCacheable, SolutionBuilderWithWatchHost, TSInstance, WatchHost, WebpackError } from './interfaces'; |
@@ -20,3 +20,3 @@ "use strict"; | ||
// make a (sync) resolver that follows webpack's rules | ||
const resolveSync = resolver_1.makeResolver(loader._compiler.options); | ||
const resolveSync = (0, resolver_1.makeResolver)(loader._compiler.options); | ||
const moduleResolutionHost = { | ||
@@ -31,3 +31,3 @@ trace: logData => instance.log.log(logData), | ||
readDirectory, | ||
useCaseSensitiveFileNames: () => utils_1.useCaseSensitiveFileNames(compiler, instance.loaderOptions), | ||
useCaseSensitiveFileNames: () => (0, utils_1.useCaseSensitiveFileNames)(compiler, instance.loaderOptions), | ||
getNewLine: () => newLine, | ||
@@ -42,3 +42,3 @@ getDefaultLibFileName: options => compiler.getDefaultLibFilePath(options), | ||
return (instance.compiler.sys.readFile(filePath, encoding) || | ||
utils_1.fsReadFile(filePath, encoding)); | ||
(0, utils_1.fsReadFile)(filePath, encoding)); | ||
} | ||
@@ -64,3 +64,3 @@ function directoryExists(directoryName) { | ||
const { moduleResolutionHost, resolveModuleNames, resolveTypeReferenceDirectives, } = makeResolversAndModuleResolutionHost(scriptRegex, loader, instance, filePathToCheck => compiler.sys.fileExists(filePathToCheck) || | ||
utils_1.fsReadFile(filePathToCheck) !== undefined, instance.loaderOptions.experimentalFileCaching); | ||
(0, utils_1.fsReadFile)(filePathToCheck) !== undefined, instance.loaderOptions.experimentalFileCaching); | ||
const servicesHost = Object.assign(Object.assign({ getProjectVersion: () => `${instance.version}`, getProjectReferences: () => projectReferences, getScriptFileNames: () => [...files.values()] | ||
@@ -115,10 +115,11 @@ .map(({ fileName }) => fileName) | ||
function makeResolvers(compiler, compilerOptions, moduleResolutionHost, customResolveTypeReferenceDirective, customResolveModuleName, resolveSync, appendTsTsxSuffixesIfRequired, scriptRegex, instance) { | ||
const resolveTypeReferenceDirective = makeResolveTypeReferenceDirective(compiler, compilerOptions, moduleResolutionHost, customResolveTypeReferenceDirective); | ||
const resolveTypeReferenceDirectives = (typeDirectiveNames, containingFile, _redirectedReference) => typeDirectiveNames.map(directive => resolveTypeReferenceDirective(directive, containingFile, _redirectedReference).resolvedTypeReferenceDirective); | ||
const resolveModuleName = makeResolveModuleName(compiler, compilerOptions, moduleResolutionHost, customResolveModuleName); | ||
const resolveModuleNames = (moduleNames, containingFile, _reusedNames, _redirectedReference) => { | ||
const resolvedModules = moduleNames.map(moduleName => resolveModule(resolveSync, resolveModuleName, appendTsTsxSuffixesIfRequired, scriptRegex, moduleName, containingFile)); | ||
utils_1.populateDependencyGraph(resolvedModules, instance, containingFile); | ||
const resolveModuleName = makeResolveModuleName(compiler, compilerOptions, moduleResolutionHost, customResolveModuleName, instance); | ||
const resolveModuleNames = (moduleNames, containingFile, _reusedNames, redirectedReference) => { | ||
const resolvedModules = moduleNames.map(moduleName => resolveModule(resolveSync, resolveModuleName, appendTsTsxSuffixesIfRequired, scriptRegex, moduleName, containingFile, redirectedReference)); | ||
(0, utils_1.populateDependencyGraph)(resolvedModules, instance, containingFile); | ||
return resolvedModules; | ||
}; | ||
const resolveTypeReferenceDirective = makeResolveTypeReferenceDirective(compiler, compilerOptions, moduleResolutionHost, customResolveTypeReferenceDirective, instance); | ||
const resolveTypeReferenceDirectives = (typeDirectiveNames, containingFile, redirectedReference, options, containingFileMode // new impliedNodeFormat is accepted by compilerHost | ||
) => typeDirectiveNames.map(directive => resolveTypeReferenceDirective(directive, containingFile, options, redirectedReference, containingFileMode).resolvedTypeReferenceDirective); | ||
return { | ||
@@ -195,3 +196,3 @@ resolveTypeReferenceDirectives, | ||
if (existing !== undefined) { | ||
utils_1.unorderedRemoveItem(existing.callbacks, callback); | ||
(0, utils_1.unorderedRemoveItem)(existing.callbacks, callback); | ||
if (!existing.callbacks.length) { | ||
@@ -239,4 +240,3 @@ callbacks.delete(key); | ||
const { moduleResolutionHost, resolveModuleNames, resolveTypeReferenceDirectives, } = makeResolversAndModuleResolutionHost(scriptRegex, loader, instance, fileName => files.has(filePathKeyMapper(fileName)) || | ||
compiler.sys.fileExists(fileName), | ||
/*enabledCaching*/ false); | ||
compiler.sys.fileExists(fileName), instance.loaderOptions.experimentalFileCaching); | ||
const watchHost = Object.assign(Object.assign({ rootFiles: getRootFileNames(), options: compilerOptions }, moduleResolutionHost), { readFile: readFileWithCachingText, watchFile: (fileName, callback, pollingInterval, options) => { | ||
@@ -301,2 +301,44 @@ var _a; | ||
const missingFileModifiedTime = new Date(0); | ||
function identity(x) { | ||
return x; | ||
} | ||
function toLowerCase(x) { | ||
return x.toLowerCase(); | ||
} | ||
const fileNameLowerCaseRegExp = /[^\u0130\u0131\u00DFa-z0-9\\/:\-_\. ]+/g; | ||
function toFileNameLowerCase(x) { | ||
return fileNameLowerCaseRegExp.test(x) | ||
? x.replace(fileNameLowerCaseRegExp, toLowerCase) | ||
: x; | ||
} | ||
function createGetCanonicalFileName(instance) { | ||
return (0, utils_1.useCaseSensitiveFileNames)(instance.compiler, instance.loaderOptions) | ||
? identity | ||
: toFileNameLowerCase; | ||
} | ||
function createModuleResolutionCache(instance, moduleResolutionHost) { | ||
const cache = instance.compiler.createModuleResolutionCache(moduleResolutionHost.getCurrentDirectory(), createGetCanonicalFileName(instance), instance.compilerOptions); | ||
// Add new API optional methods | ||
if (!cache.clear) { | ||
cache.clear = () => { | ||
cache.directoryToModuleNameMap.clear(); | ||
cache.moduleNameToDirectoryMap.clear(); | ||
}; | ||
} | ||
if (!cache.update) { | ||
cache.update = options => { | ||
if (!options.configFile) | ||
return; | ||
const ref = { | ||
sourceFile: options.configFile, | ||
commandLine: { options }, | ||
}; | ||
cache.directoryToModuleNameMap.setOwnMap(cache.directoryToModuleNameMap.getOrCreateMapOfCacheRedirects(ref)); | ||
cache.moduleNameToDirectoryMap.setOwnMap(cache.moduleNameToDirectoryMap.getOrCreateMapOfCacheRedirects(ref)); | ||
cache.directoryToModuleNameMap.setOwnOptions(options); | ||
cache.moduleNameToDirectoryMap.setOwnOptions(options); | ||
}; | ||
} | ||
return cache; | ||
} | ||
/** | ||
@@ -310,5 +352,3 @@ * Create the TypeScript Watch host | ||
getCurrentDirectory: compiler.sys.getCurrentDirectory, | ||
getCanonicalFileName: utils_1.useCaseSensitiveFileNames(compiler, instance.loaderOptions) | ||
? s => s | ||
: s => s.toLowerCase(), | ||
getCanonicalFileName: createGetCanonicalFileName(instance), | ||
getNewLine: () => compiler.sys.newLine, | ||
@@ -354,5 +394,8 @@ }; | ||
let timeoutId; | ||
const { resolveModuleNames, resolveTypeReferenceDirectives, moduleResolutionHost, } = makeResolversAndModuleResolutionHost(scriptRegex, loader, instance, fileName => !!instance.files.has(filePathKeyMapper(fileName)) || | ||
!!instance.otherFiles.get(filePathKeyMapper(fileName)) || | ||
compiler.sys.fileExists(fileName), | ||
const { resolveModuleNames, resolveTypeReferenceDirectives, moduleResolutionHost, } = makeResolversAndModuleResolutionHost(scriptRegex, loader, instance, fileName => { | ||
const filePathKey = filePathKeyMapper(fileName); | ||
return (instance.files.has(filePathKey) || | ||
instance.otherFiles.has(filePathKey) || | ||
compiler.sys.fileExists(fileName)); | ||
}, | ||
/*enableFileCaching*/ true); | ||
@@ -362,3 +405,11 @@ const configFileInfo = new Map(); | ||
const sysHost = compiler.createSolutionBuilderWithWatchHost(compiler.sys, compiler.createEmitAndSemanticDiagnosticsBuilderProgram, reportDiagnostic, reportSolutionBuilderStatus, reportWatchStatus); | ||
const solutionBuilderHost = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, sysHost), moduleResolutionHost), { resolveModuleNames, | ||
const solutionBuilderHost = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, sysHost), moduleResolutionHost), { createProgram: (rootNames, options, host, oldProgram, configFileParsingDiagnostics, projectReferences) => { | ||
var _a, _b, _c, _d; | ||
(_a = instance.moduleResolutionCache) === null || _a === void 0 ? void 0 : _a.update(options || {}); | ||
(_b = instance.typeReferenceResolutionCache) === null || _b === void 0 ? void 0 : _b.update(options || {}); | ||
const result = sysHost.createProgram(rootNames, options, host, oldProgram, configFileParsingDiagnostics, projectReferences); | ||
(_c = instance.typeReferenceResolutionCache) === null || _c === void 0 ? void 0 : _c.update(instance.compilerOptions); | ||
(_d = instance.moduleResolutionCache) === null || _d === void 0 ? void 0 : _d.update(instance.compilerOptions); | ||
return result; | ||
}, resolveModuleNames, | ||
resolveTypeReferenceDirectives, | ||
@@ -409,3 +460,3 @@ diagnostics }), createWatchFactory(filePathKeyMapper, compiler)), { | ||
}, getParsedCommandLine: file => { | ||
const config = config_1.getParsedCommandLine(compiler, instance.loaderOptions, file); | ||
const config = (0, config_1.getParsedCommandLine)(compiler, instance.loaderOptions, file); | ||
configFileInfo.set(filePathKeyMapper(file), { config }); | ||
@@ -503,3 +554,3 @@ return config; | ||
inputFileName: path.resolve(inputFile), | ||
outputNames: instances_1.getOutputFileNames(instance, configInfo.config, inputFile), | ||
outputNames: (0, instances_1.getOutputFileNames)(instance, configInfo.config, inputFile), | ||
})); | ||
@@ -627,3 +678,3 @@ configInfo.tsbuildInfoFile = instance.compiler | ||
instance.solutionBuilderHost.diagnostics.transpileErrors.length) { | ||
instance.solutionBuilderHost.diagnostics.transpileErrors.forEach(([filePath, errors]) => solutionErrors.push(...utils_1.formatErrors(errors, instance.loaderOptions, instance.colors, instance.compiler, { file: filePath ? undefined : 'tsconfig.json' }, context))); | ||
instance.solutionBuilderHost.diagnostics.transpileErrors.forEach(([filePath, errors]) => solutionErrors.push(...(0, utils_1.formatErrors)(errors, instance.loaderOptions, instance.colors, instance.compiler, { file: filePath ? undefined : 'tsconfig.json' }, context))); | ||
} | ||
@@ -633,7 +684,24 @@ return solutionErrors; | ||
exports.getSolutionErrors = getSolutionErrors; | ||
function makeResolveTypeReferenceDirective(compiler, compilerOptions, moduleResolutionHost, customResolveTypeReferenceDirective) { | ||
function makeResolveTypeReferenceDirective(compiler, compilerOptions, moduleResolutionHost, customResolveTypeReferenceDirective, instance) { | ||
var _a, _b; | ||
if (customResolveTypeReferenceDirective === undefined) { | ||
return (directive, containingFile, redirectedReference) => compiler.resolveTypeReferenceDirective(directive, containingFile, compilerOptions, moduleResolutionHost, redirectedReference); | ||
// Until the api is published | ||
if (compiler.createTypeReferenceDirectiveResolutionCache !== undefined && | ||
!instance.typeReferenceResolutionCache) { | ||
instance.typeReferenceResolutionCache = compiler.createTypeReferenceDirectiveResolutionCache(moduleResolutionHost.getCurrentDirectory(), createGetCanonicalFileName(instance), instance.compilerOptions, (_b = (_a = instance.moduleResolutionCache) === null || _a === void 0 ? void 0 : _a.getPackageJsonInfoCache) === null || _b === void 0 ? void 0 : _b.call(_a)); | ||
} | ||
return (typeDirectiveName, containingFile, options, redirectedReference, containingFileMode) => { | ||
// Copy-pasted from https://github.com/TypeStrong/ts-node/blob/9f789d0d91c6eba30ac7f7aad45194a23b44f159/src/resolver-functions.ts#L139 | ||
const nameIsString = typeof typeDirectiveName === 'string'; | ||
const mode = nameIsString | ||
? undefined | ||
: compiler.getModeForFileReference(typeDirectiveName, containingFileMode); | ||
const strName = nameIsString | ||
? typeDirectiveName | ||
: typeDirectiveName.fileName.toLowerCase(); | ||
return compiler.resolveTypeReferenceDirective(strName, containingFile, options, moduleResolutionHost, redirectedReference, undefined, mode); | ||
}; | ||
} | ||
return (directive, containingFile) => customResolveTypeReferenceDirective(directive, containingFile, compilerOptions, moduleResolutionHost, compiler.resolveTypeReferenceDirective); | ||
return (directive, containingFile) => customResolveTypeReferenceDirective(directive, // unsure whether we should evolve this further | ||
containingFile, compilerOptions, moduleResolutionHost, compiler.resolveTypeReferenceDirective); | ||
} | ||
@@ -644,3 +712,3 @@ function isJsImplementationOfTypings(resolvedModule, tsResolution) { | ||
} | ||
function resolveModule(resolveSync, resolveModuleName, appendTsTsxSuffixesIfRequired, scriptRegex, moduleName, containingFile) { | ||
function resolveModule(resolveSync, resolveModuleName, appendTsTsxSuffixesIfRequired, scriptRegex, moduleName, containingFile, redirectedReference) { | ||
let resolutionResult; | ||
@@ -655,10 +723,6 @@ try { | ||
catch (e) { } | ||
const tsResolution = resolveModuleName(moduleName, containingFile); | ||
const tsResolution = resolveModuleName(moduleName, containingFile, redirectedReference); | ||
if (tsResolution.resolvedModule !== undefined) { | ||
const resolvedFileName = path.normalize(tsResolution.resolvedModule.resolvedFileName); | ||
const tsResolutionResult = { | ||
originalFileName: resolvedFileName, | ||
resolvedFileName, | ||
isExternalLibraryImport: tsResolution.resolvedModule.isExternalLibraryImport, | ||
}; | ||
const tsResolutionResult = Object.assign(Object.assign({}, tsResolution.resolvedModule), { originalFileName: resolvedFileName, resolvedFileName }); | ||
return resolutionResult === undefined || | ||
@@ -673,5 +737,8 @@ resolutionResult.resolvedFileName === | ||
} | ||
function makeResolveModuleName(compiler, compilerOptions, moduleResolutionHost, customResolveModuleName) { | ||
function makeResolveModuleName(compiler, compilerOptions, moduleResolutionHost, customResolveModuleName, instance) { | ||
if (customResolveModuleName === undefined) { | ||
return (moduleName, containingFile) => compiler.resolveModuleName(moduleName, containingFile, compilerOptions, moduleResolutionHost); | ||
if (!instance.moduleResolutionCache) { | ||
instance.moduleResolutionCache = createModuleResolutionCache(instance, moduleResolutionHost); | ||
} | ||
return (moduleName, containingFile, redirectedReference) => compiler.resolveModuleName(moduleName, containingFile, compilerOptions, moduleResolutionHost, instance.moduleResolutionCache, redirectedReference); | ||
} | ||
@@ -678,0 +745,0 @@ return (moduleName, containingFile) => customResolveModuleName(moduleName, containingFile, compilerOptions, moduleResolutionHost, compiler.resolveModuleName); |
import { Chalk } from 'chalk'; | ||
import * as typescript from 'typescript'; | ||
import { FilePathKey, LoaderOptions, ResolvedModule, ReverseDependencyGraph, TSInstance, WebpackError, WebpackModule } from './interfaces'; | ||
import type * as typescript from 'typescript'; | ||
import { FileLocation, FilePathKey, LoaderOptions, ResolvedModule, ReverseDependencyGraph, TSInstance, WebpackError, WebpackModule } from './interfaces'; | ||
/** | ||
@@ -13,6 +13,3 @@ * Take TypeScript errors, parse them and format to webpack errors | ||
export declare function fsReadFile(fileName: string, encoding?: string | undefined): string | undefined; | ||
export declare function makeError(loaderOptions: LoaderOptions, message: string, file: string | undefined, location?: { | ||
line: number; | ||
character: number; | ||
}): WebpackError; | ||
export declare function makeError(loaderOptions: LoaderOptions, message: string, file: string | undefined, location?: FileLocation, endLocation?: FileLocation): WebpackError; | ||
export declare function tsLoaderSource(loaderOptions: LoaderOptions): string; | ||
@@ -19,0 +16,0 @@ export declare function appendSuffixIfMatch(patterns: (RegExp | string)[], filePath: string, suffix: string): string; |
@@ -47,5 +47,5 @@ "use strict"; | ||
const file = diagnostic.file; | ||
const position = file === undefined | ||
? undefined | ||
: file.getLineAndCharacterOfPosition(diagnostic.start); | ||
const { start, end } = file === undefined || diagnostic.start === undefined | ||
? { start: undefined, end: undefined } | ||
: getFileLocations(file, diagnostic.start, diagnostic.length); | ||
const errorInfo = { | ||
@@ -56,4 +56,4 @@ code: diagnostic.code, | ||
file: file === undefined ? '' : path.normalize(file.fileName), | ||
line: position === undefined ? 0 : position.line + 1, | ||
character: position === undefined ? 0 : position.character + 1, | ||
line: start === undefined ? 0 : start.line, | ||
character: start === undefined ? 0 : start.character, | ||
context, | ||
@@ -64,5 +64,3 @@ }; | ||
: loaderOptions.errorFormatter(errorInfo, colors); | ||
const error = makeError(loaderOptions, message, merge.file === undefined ? errorInfo.file : merge.file, position === undefined | ||
? undefined | ||
: { line: errorInfo.line, character: errorInfo.character }); | ||
const error = makeError(loaderOptions, message, merge.file === undefined ? errorInfo.file : merge.file, start, end); | ||
return Object.assign(error, merge); | ||
@@ -72,2 +70,16 @@ }); | ||
exports.formatErrors = formatErrors; | ||
function getFileLocations(file, position, length = 0) { | ||
const startLC = file.getLineAndCharacterOfPosition(position); | ||
const start = { | ||
line: startLC.line + 1, | ||
character: startLC.character + 1, | ||
}; | ||
const endLC = length > 0 | ||
? file.getLineAndCharacterOfPosition(position + length) | ||
: undefined; | ||
const end = endLC === undefined | ||
? undefined | ||
: { line: endLC.line + 1, character: endLC.character + 1 }; | ||
return { start, end }; | ||
} | ||
function fsReadFile(fileName, encoding = 'utf8') { | ||
@@ -83,7 +95,10 @@ fileName = path.normalize(fileName); | ||
exports.fsReadFile = fsReadFile; | ||
function makeError(loaderOptions, message, file, location) { | ||
function makeError(loaderOptions, message, file, location, endLocation) { | ||
return { | ||
message, | ||
file, | ||
loc: location === undefined | ||
? undefined | ||
: makeWebpackLocation(location, endLocation), | ||
location, | ||
file, | ||
loaderSource: tsLoaderSource(loaderOptions), | ||
@@ -93,2 +108,12 @@ }; | ||
exports.makeError = makeError; | ||
function makeWebpackLocation(location, endLocation) { | ||
const start = { | ||
line: location.line, | ||
column: location.character - 1, | ||
}; | ||
const end = endLocation === undefined | ||
? undefined | ||
: { line: endLocation.line, column: endLocation.character - 1 }; | ||
return { start, end }; | ||
} | ||
function tsLoaderSource(loaderOptions) { | ||
@@ -141,3 +166,3 @@ return `ts-loader-${loaderOptions.instance}`; | ||
const inputFileName = instance.solutionBuilderHost && | ||
instances_1.getInputFileNameFromOutput(instance, fileKey); | ||
(0, instances_1.getInputFileNameFromOutput)(instance, fileKey); | ||
const containingFileKey = inputFileName | ||
@@ -148,3 +173,3 @@ ? instance.filePathKeyMapper(inputFileName) | ||
const key = instance.filePathKeyMapper(instance.solutionBuilderHost | ||
? instances_1.getInputFileNameFromOutput(instance, resolvedFileName) || | ||
? (0, instances_1.getInputFileNameFromOutput)(instance, resolvedFileName) || | ||
resolvedFileName | ||
@@ -151,0 +176,0 @@ : resolvedFileName); |
@@ -18,4 +18,7 @@ "use strict"; | ||
return (compiler, callback) => { | ||
var _a, _b; | ||
var _a, _b, _c, _d, _e, _f; | ||
(_b = (_a = instance.servicesHost) === null || _a === void 0 ? void 0 : _a.clearCache) === null || _b === void 0 ? void 0 : _b.call(_a); | ||
(_d = (_c = instance.watchHost) === null || _c === void 0 ? void 0 : _c.clearCache) === null || _d === void 0 ? void 0 : _d.call(_c); | ||
(_e = instance.moduleResolutionCache) === null || _e === void 0 ? void 0 : _e.clear(); | ||
(_f = instance.typeReferenceResolutionCache) === null || _f === void 0 ? void 0 : _f.clear(); | ||
const promises = []; | ||
@@ -77,3 +80,3 @@ if (instance.loaderOptions.transpileOnly) { | ||
const text = JSON.parse(source); | ||
servicesHost_1.updateFileWithText(instance, key, filePath, () => text); | ||
(0, servicesHost_1.updateFileWithText)(instance, key, filePath, () => text); | ||
resolve(); | ||
@@ -84,3 +87,3 @@ } | ||
else { | ||
servicesHost_1.updateFileWithText(instance, key, filePath, nFilePath => utils_1.fsReadFile(nFilePath) || ''); | ||
(0, servicesHost_1.updateFileWithText)(instance, key, filePath, nFilePath => (0, utils_1.fsReadFile)(nFilePath) || ''); | ||
resolve(); | ||
@@ -87,0 +90,0 @@ } |
{ | ||
"name": "ts-loader", | ||
"version": "8.0.14", | ||
"version": "8.4.0", | ||
"description": "TypeScript loader for webpack", | ||
@@ -99,3 +99,3 @@ "main": "index.js", | ||
"rimraf": "^2.6.2", | ||
"typescript": "^4.0.0", | ||
"typescript": "^4.6.3", | ||
"webpack": "^4.5.0", | ||
@@ -102,0 +102,0 @@ "webpack-cli": "^3.1.1" |
# TypeScript loader for webpack | ||
[![npm version](https://img.shields.io/npm/v/ts-loader.svg)](https://www.npmjs.com/package/ts-loader) | ||
![GitHub build)](https://github.com/TypeStrong/ts-loader/workflows/Continuous%20Integration%20(build%20and%20test)/badge.svg) | ||
[![Travis build](https://travis-ci.org/TypeStrong/ts-loader.svg?branch=master)](https://travis-ci.org/TypeStrong/ts-loader) | ||
[![build and test](https://github.com/TypeStrong/ts-loader/actions/workflows/push.yml/badge.svg)](https://github.com/TypeStrong/ts-loader/actions/workflows/push.yml) | ||
[![Downloads](http://img.shields.io/npm/dm/ts-loader.svg)](https://npmjs.org/package/ts-loader) | ||
@@ -131,3 +130,3 @@ [![node version](https://img.shields.io/node/v/ts-loader.svg)](https://www.npmjs.com/package/ts-loader) | ||
`ts-loader` works very well in combination with [babel](https://babeljs.io/) and [babel-loader](https://github.com/babel/babel-loader). There is an [example](https://github.com/Microsoft/TypeScriptSamples/tree/master/react-flux-babel-karma) of this in the official [TypeScript Samples](https://github.com/Microsoft/TypeScriptSamples). Alternatively take a look at our own [example](examples/react-babel-karma-gulp). | ||
`ts-loader` works very well in combination with [babel](https://babeljs.io/) and [babel-loader](https://github.com/babel/babel-loader). There is an [example](https://github.com/Microsoft/TypeScriptSamples/tree/master/react-flux-babel-karma) of this in the official [TypeScript Samples](https://github.com/Microsoft/TypeScriptSamples). | ||
@@ -348,3 +347,3 @@ ### Parallelising Builds | ||
|------| | ||
| ` (program: Program) => { before?: TransformerFactory<SourceFile>[]; after?: TransformerFactory<SourceFile>[]; } ` | | ||
| ` (program: Program) => { before?: TransformerFactory<SourceFile>[]; after?: TransformerFactory<SourceFile>[]; afterDeclarations?: TransformerFactory<SourceFile>[]; } ` | | ||
@@ -734,2 +733,3 @@ Provide custom transformers - only compatible with TypeScript 2.3+ (and 2.4 if using `transpileOnly` mode). For example usage take a look at [typescript-plugin-styled-components](https://github.com/Igorbek/typescript-plugin-styled-components) or our [test](test/comparison-tests/customTransformer). | ||
```javascript | ||
// for webpack 4 | ||
plugins: [ | ||
@@ -741,2 +741,11 @@ new webpack.WatchIgnorePlugin([ | ||
], | ||
// for webpack 5 | ||
plugins: [ | ||
new webpack.WatchIgnorePlugin({ | ||
paths:[ | ||
/\.js$/, | ||
/\.d\.ts$/ | ||
]}) | ||
], | ||
``` | ||
@@ -743,0 +752,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
251588
3061
767