Skip to content

Commit b80a061

Browse files
fix #270, support appendTsSuffixTo config
1 parent 60f6a42 commit b80a061

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ const definitionFileRegex = /\.d\.ts$/;
1414
function loader(this: interfaces.Webpack, contents: string) {
1515
this.cacheable && this.cacheable();
1616
const callback = this.async();
17-
const filePath = path.normalize(this.resourcePath);
18-
1917
const options = makeOptions(this);
18+
const rawFilePath = path.normalize(this.resourcePath);
19+
const filePath = utils.appendTsSuffixIfMatch(options.appendTsSuffixTo, rawFilePath);
2020

2121
const { instance, error } = instances.ensureTypeScriptInstance(options, this);
2222

@@ -58,6 +58,7 @@ function makeOptions(loader: interfaces.Webpack) {
5858
configFileName: 'tsconfig.json',
5959
transpileOnly: false,
6060
compilerOptions: {},
61+
appendTsSuffixTo: [],
6162
}, configFileOptions, queryOptions);
6263
options.ignoreDiagnostics = arrify(options.ignoreDiagnostics).map(Number);
6364
options.logLevel = options.logLevel.toUpperCase();

src/instances.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export function ensureTypeScriptInstance(
105105
modifiedFiles: null,
106106
};
107107

108-
const servicesHost = makeServicesHost(scriptRegex, log, loader, instance);
108+
const servicesHost = makeServicesHost(scriptRegex, log, loader, instance, loaderOptions.appendTsSuffixTo);
109109
instance.languageService = compiler.createLanguageService(servicesHost, compiler.createDocumentRegistry());
110110

111111
loader._compiler.plugin("after-compile", afterCompile(instance, configFilePath));

src/interfaces.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export interface Resolve {
8585
/**
8686
* The directory (absolute path) that contains your modules.
8787
* May also be an array of directories.
88-
* This setting should be used to add individual directories to the search path.
88+
* This setting should be used to add individual directories to the search path.
8989
*/
9090
root?: string | string[];
9191
/**
@@ -153,6 +153,7 @@ export interface LoaderOptions {
153153
transpileOnly: boolean;
154154
ignoreDiagnostics: number[];
155155
compilerOptions: typescript.CompilerOptions;
156+
appendTsSuffixTo: RegExp[];
156157
}
157158

158159
export interface TSFile {

src/servicesHost.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ function makeServicesHost(
1313
scriptRegex: RegExp,
1414
log: logger.Logger,
1515
loader: interfaces.Webpack,
16-
instance: interfaces.TSInstance
16+
instance: interfaces.TSInstance,
17+
appendTsSuffixTo: RegExp[]
1718
) {
1819
const { compiler, compilerOptions, files } = instance;
1920

@@ -77,6 +78,7 @@ function makeServicesHost(
7778

7879
try {
7980
resolvedFileName = resolver.resolveSync(path.normalize(path.dirname(containingFile)), moduleName);
81+
resolvedFileName = utils.appendTsSuffixIfMatch(appendTsSuffixTo, resolvedFileName);
8082

8183
if (!resolvedFileName.match(scriptRegex)) {
8284
resolvedFileName = null;

src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,12 @@ export function makeError({ rawMessage, message, location, file }: MakeError): i
7171

7272
return <interfaces.WebpackError> objectAssign(error, { location, file });
7373
}
74+
75+
export function appendTsSuffixIfMatch(patterns: RegExp[], path: string): string {
76+
for (let regexp of patterns) {
77+
if (regexp.test(path)) {
78+
return path + '.ts';
79+
}
80+
}
81+
return path;
82+
}

0 commit comments

Comments
 (0)