Skip to content
This repository was archived by the owner on Dec 1, 2019. It is now read-only.

Commit 0058cc5

Browse files
committed
fix: warn when .d.ts files are required, fixes #372, #320
1 parent 4e043b8 commit 0058cc5

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {
2+
src, webpackConfig, tsconfig,
3+
compile, expectErrors, expectWarnings, spec
4+
} from './utils';
5+
6+
spec(__filename, async function() {
7+
src('index.ts', `
8+
import './empty.d.ts'
9+
`);
10+
11+
src('empty.d.ts', ``);
12+
tsconfig();
13+
14+
const stats = await compile(webpackConfig());
15+
expectErrors(stats, 0);
16+
expectWarnings(stats, 1, [
17+
'TypeScript declaration files should never be required'
18+
]);
19+
});

src/__test__/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,15 @@ export function expectErrors(stats: any, count: number, errors: string[] = []) {
236236
expect(stats.compilation.errors.length).eq(count);
237237
}
238238

239+
export function expectWarnings(stats: any, count: number, warnings: string[] = []) {
240+
stats.compilation.warnings.every(warn => {
241+
const str = warn.toString();
242+
expect(warnings.some(e => str.indexOf(e) !== -1), 'Warning is not covered: \n' + str).true;
243+
});
244+
245+
expect(stats.compilation.warnings.length).eq(count);
246+
}
247+
239248
export function tsconfig(
240249
compilerOptions?: any,
241250
config?: any

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ interface Transformation {
3030
fresh?: boolean;
3131
}
3232

33+
const DECLARATION = /\.d.ts$/i;
34+
3335
function compiler(loader: Loader, text: string): void {
3436
if (loader.cacheable) {
3537
loader.cacheable();
@@ -44,9 +46,13 @@ function compiler(loader: Loader, text: string): void {
4446
const callback = loader.async();
4547

4648
let fileName = helpers.toUnix(loader.resourcePath);
47-
4849
instance.compiledFiles[fileName] = true;
4950

51+
if (DECLARATION.test(fileName)) {
52+
loader.emitWarning(`[${instanceName}] TypeScript declaration files should never be required`);
53+
return callback(null, '');
54+
}
55+
5056
let compiledModule;
5157
if (instance.loaderConfig.usePrecompiledFiles) {
5258
compiledModule = findCompiledModule(fileName);

src/instance.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export interface Loader {
4141
addDependency: (dep: string) => void;
4242
clearDependencies: () => void;
4343
emitFile: (fileName: string, text: string) => void;
44+
emitWarning: (msg: string) => void;
45+
emitError: (msg: string) => void;
4446
options: {
4547
ts?: LoaderConfig
4648
};

0 commit comments

Comments
 (0)