diff --git a/.travis.yml b/.travis.yml index ba5ccb6ac..1ada50d11 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,6 @@ language: node_js node_js: - "8" - "10" - - "11" - "12" sudo: required install: diff --git a/CHANGELOG.md b/CHANGELOG.md index a833638a9..4e03bf2f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v5.4.4 + +* [refactor: add common appendTsTsxSuffixesIfRequired function to instance](https://github.com/TypeStrong/ts-loader/pull/924) - thanks @johnnyreilly! + ## v5.4.3 * [feat: resolveTypeReferenceDirective support for yarn PnP](https://github.com/TypeStrong/ts-loader/pull/921) - thanks @johnnyreilly! diff --git a/package.json b/package.json index 178d3919f..b2116d1c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-loader", - "version": "5.4.3", + "version": "5.4.4", "description": "TypeScript loader for webpack", "main": "index.js", "types": "dist/types/index.d.ts", diff --git a/src/instances.ts b/src/instances.ts index bb9ac05b9..fd1f53d8f 100644 --- a/src/instances.ts +++ b/src/instances.ts @@ -19,6 +19,7 @@ import { import * as logger from './logger'; import { makeServicesHost, makeWatchHost } from './servicesHost'; import { + appendSuffixesIfMatch, ensureProgram, formatErrors, isUsingProjectReferences, @@ -125,6 +126,19 @@ function successfulTypeScriptInstance( const files: TSFiles = new Map(); const otherFiles: TSFiles = new Map(); + const appendTsTsxSuffixesIfRequired = + loaderOptions.appendTsSuffixTo.length > 0 || + loaderOptions.appendTsxSuffixTo.length > 0 + ? (filePath: string) => + appendSuffixesIfMatch( + { + '.ts': loaderOptions.appendTsSuffixTo, + '.tsx': loaderOptions.appendTsxSuffixTo + }, + filePath + ) + : (filePath: string) => filePath; + // same strategy as https://github.com/s-panferov/awesome-typescript-loader/pull/531/files let { getCustomTransformers: customerTransformers } = loaderOptions; let getCustomTransformers = Function.prototype; @@ -182,6 +196,7 @@ function successfulTypeScriptInstance( instances[loaderOptions.instance] = { compiler, compilerOptions, + appendTsTsxSuffixesIfRequired, loaderOptions, files, otherFiles, @@ -230,6 +245,7 @@ function successfulTypeScriptInstance( const instance: TSInstance = (instances[loaderOptions.instance] = { compiler, compilerOptions, + appendTsTsxSuffixesIfRequired, loaderOptions, files, otherFiles, @@ -257,8 +273,6 @@ function successfulTypeScriptInstance( log, loader, instance, - loaderOptions.appendTsSuffixTo, - loaderOptions.appendTsxSuffixTo, configParseResult.projectReferences ); instance.watchOfFilesAndCompilerOptions = compiler.createWatchProgram( @@ -288,7 +302,9 @@ function successfulTypeScriptInstance( loader._compiler.hooks.watchRun.tap('ts-loader', servicesHost.clearCache); } - instance.transformers = getCustomTransformers(instance.languageService!.getProgram()); + instance.transformers = getCustomTransformers( + instance.languageService!.getProgram() + ); } loader._compiler.hooks.afterCompile.tapAsync( diff --git a/src/interfaces.ts b/src/interfaces.ts index 01a12f0c0..d3bd4d4fb 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -240,6 +240,8 @@ export interface WatchHost export interface TSInstance { compiler: typeof typescript; compilerOptions: typescript.CompilerOptions; + /** Used for Vue for the most part */ + appendTsTsxSuffixesIfRequired: (filePath: string) => string; loaderOptions: LoaderOptions; /** * a cache of all the files diff --git a/src/servicesHost.ts b/src/servicesHost.ts index 7cf9da25d..7839a6bdc 100644 --- a/src/servicesHost.ts +++ b/src/servicesHost.ts @@ -14,7 +14,7 @@ import { } from './interfaces'; import * as logger from './logger'; import { makeResolver } from './resolver'; -import { appendSuffixesIfMatch, readFile, unorderedRemoveItem } from './utils'; +import { readFile, unorderedRemoveItem } from './utils'; export type Action = () => void; @@ -37,10 +37,9 @@ export function makeServicesHost( const { compiler, compilerOptions, + appendTsTsxSuffixesIfRequired, files, loaderOptions: { - appendTsSuffixTo, - appendTsxSuffixTo, resolveModuleName: customResolveModuleName, resolveTypeReferenceDirective: customResolveTypeReferenceDirective } @@ -85,8 +84,7 @@ export function makeServicesHost( customResolveTypeReferenceDirective, customResolveModuleName, resolveSync, - appendTsSuffixTo, - appendTsxSuffixTo, + appendTsTsxSuffixesIfRequired, scriptRegex, instance ); @@ -171,8 +169,7 @@ function makeResolvers( customResolveTypeReferenceDirective: CustomResolveTypeReferenceDirective, customResolveModuleName: CustomResolveModuleName, resolveSync: ResolveSync, - appendTsSuffixTo: RegExp[], - appendTsxSuffixTo: RegExp[], + appendTsTsxSuffixesIfRequired: (filePath: string) => string, scriptRegex: RegExp, instance: TSInstance ) { @@ -211,8 +208,7 @@ function makeResolvers( resolveModule( resolveSync, resolveModuleName, - appendTsSuffixTo, - appendTsxSuffixTo, + appendTsTsxSuffixesIfRequired, scriptRegex, moduleName, containingFile @@ -238,13 +234,12 @@ export function makeWatchHost( log: logger.Logger, loader: Webpack, instance: TSInstance, - appendTsSuffixTo: RegExp[], - appendTsxSuffixTo: RegExp[], projectReferences?: ReadonlyArray ) { const { compiler, compilerOptions, + appendTsTsxSuffixesIfRequired, files, otherFiles, loaderOptions: { @@ -294,8 +289,7 @@ export function makeWatchHost( customResolveTypeReferenceDirective, customResolveModuleName, resolveSync, - appendTsSuffixTo, - appendTsxSuffixTo, + appendTsTsxSuffixesIfRequired, scriptRegex, instance ); @@ -550,8 +544,7 @@ function isJsImplementationOfTypings( function resolveModule( resolveSync: ResolveSync, resolveModuleName: ResolveModuleName, - appendTsSuffixTo: RegExp[], - appendTsxSuffixTo: RegExp[], + appendTsTsxSuffixesIfRequired: (filePath: string) => string, scriptRegex: RegExp, moduleName: string, containingFile: string @@ -565,16 +558,7 @@ function resolveModule( moduleName ); - const resolvedFileName = - appendTsSuffixTo.length > 0 || appendTsxSuffixTo.length > 0 - ? appendSuffixesIfMatch( - { - '.ts': appendTsSuffixTo, - '.tsx': appendTsxSuffixTo - }, - originalFileName - ) - : originalFileName; + const resolvedFileName = appendTsTsxSuffixesIfRequired(originalFileName); if (resolvedFileName.match(scriptRegex) !== null) { resolutionResult = { resolvedFileName, originalFileName };