Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add common appendTsTsxSuffixesIfRequired function to instance #924

Merged
merged 3 commits into from Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -6,7 +6,6 @@ language: node_js
node_js:
- "8"
- "10"
- "11"
- "12"
sudo: required
install:
Expand Down
4 changes: 4 additions & 0 deletions 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!
Expand Down
2 changes: 1 addition & 1 deletion 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",
Expand Down
22 changes: 19 additions & 3 deletions src/instances.ts
Expand Up @@ -19,6 +19,7 @@ import {
import * as logger from './logger';
import { makeServicesHost, makeWatchHost } from './servicesHost';
import {
appendSuffixesIfMatch,
ensureProgram,
formatErrors,
isUsingProjectReferences,
Expand Down Expand Up @@ -125,6 +126,19 @@ function successfulTypeScriptInstance(
const files: TSFiles = new Map<string, TSFile>();
const otherFiles: TSFiles = new Map<string, TSFile>();

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;
Expand Down Expand Up @@ -182,6 +196,7 @@ function successfulTypeScriptInstance(
instances[loaderOptions.instance] = {
compiler,
compilerOptions,
appendTsTsxSuffixesIfRequired,
loaderOptions,
files,
otherFiles,
Expand Down Expand Up @@ -230,6 +245,7 @@ function successfulTypeScriptInstance(
const instance: TSInstance = (instances[loaderOptions.instance] = {
compiler,
compilerOptions,
appendTsTsxSuffixesIfRequired,
loaderOptions,
files,
otherFiles,
Expand Down Expand Up @@ -257,8 +273,6 @@ function successfulTypeScriptInstance(
log,
loader,
instance,
loaderOptions.appendTsSuffixTo,
loaderOptions.appendTsxSuffixTo,
configParseResult.projectReferences
);
instance.watchOfFilesAndCompilerOptions = compiler.createWatchProgram(
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces.ts
Expand Up @@ -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
Expand Down
34 changes: 9 additions & 25 deletions src/servicesHost.ts
Expand Up @@ -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;

Expand All @@ -37,10 +37,9 @@ export function makeServicesHost(
const {
compiler,
compilerOptions,
appendTsTsxSuffixesIfRequired,
files,
loaderOptions: {
appendTsSuffixTo,
appendTsxSuffixTo,
resolveModuleName: customResolveModuleName,
resolveTypeReferenceDirective: customResolveTypeReferenceDirective
}
Expand Down Expand Up @@ -85,8 +84,7 @@ export function makeServicesHost(
customResolveTypeReferenceDirective,
customResolveModuleName,
resolveSync,
appendTsSuffixTo,
appendTsxSuffixTo,
appendTsTsxSuffixesIfRequired,
scriptRegex,
instance
);
Expand Down Expand Up @@ -171,8 +169,7 @@ function makeResolvers(
customResolveTypeReferenceDirective: CustomResolveTypeReferenceDirective,
customResolveModuleName: CustomResolveModuleName,
resolveSync: ResolveSync,
appendTsSuffixTo: RegExp[],
appendTsxSuffixTo: RegExp[],
appendTsTsxSuffixesIfRequired: (filePath: string) => string,
scriptRegex: RegExp,
instance: TSInstance
) {
Expand Down Expand Up @@ -211,8 +208,7 @@ function makeResolvers(
resolveModule(
resolveSync,
resolveModuleName,
appendTsSuffixTo,
appendTsxSuffixTo,
appendTsTsxSuffixesIfRequired,
scriptRegex,
moduleName,
containingFile
Expand All @@ -238,13 +234,12 @@ export function makeWatchHost(
log: logger.Logger,
loader: Webpack,
instance: TSInstance,
appendTsSuffixTo: RegExp[],
appendTsxSuffixTo: RegExp[],
projectReferences?: ReadonlyArray<typescript.ProjectReference>
) {
const {
compiler,
compilerOptions,
appendTsTsxSuffixesIfRequired,
files,
otherFiles,
loaderOptions: {
Expand Down Expand Up @@ -294,8 +289,7 @@ export function makeWatchHost(
customResolveTypeReferenceDirective,
customResolveModuleName,
resolveSync,
appendTsSuffixTo,
appendTsxSuffixTo,
appendTsTsxSuffixesIfRequired,
scriptRegex,
instance
);
Expand Down Expand Up @@ -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
Expand All @@ -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 };
Expand Down