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

Commit

Permalink
fix: warn when .d.ts files are required, fixes #372, #320
Browse files Browse the repository at this point in the history
  • Loading branch information
s-panferov committed Feb 16, 2017
1 parent 4e043b8 commit 0058cc5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/__test__/import-empty-declaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
src, webpackConfig, tsconfig,
compile, expectErrors, expectWarnings, spec
} from './utils';

spec(__filename, async function() {
src('index.ts', `
import './empty.d.ts'
`);

src('empty.d.ts', ``);
tsconfig();

const stats = await compile(webpackConfig());
expectErrors(stats, 0);
expectWarnings(stats, 1, [
'TypeScript declaration files should never be required'
]);
});
9 changes: 9 additions & 0 deletions src/__test__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ export function expectErrors(stats: any, count: number, errors: string[] = []) {
expect(stats.compilation.errors.length).eq(count);
}

export function expectWarnings(stats: any, count: number, warnings: string[] = []) {
stats.compilation.warnings.every(warn => {
const str = warn.toString();
expect(warnings.some(e => str.indexOf(e) !== -1), 'Warning is not covered: \n' + str).true;
});

expect(stats.compilation.warnings.length).eq(count);
}

export function tsconfig(
compilerOptions?: any,
config?: any
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ interface Transformation {
fresh?: boolean;
}

const DECLARATION = /\.d.ts$/i;

function compiler(loader: Loader, text: string): void {
if (loader.cacheable) {
loader.cacheable();
Expand All @@ -44,9 +46,13 @@ function compiler(loader: Loader, text: string): void {
const callback = loader.async();

let fileName = helpers.toUnix(loader.resourcePath);

instance.compiledFiles[fileName] = true;

if (DECLARATION.test(fileName)) {
loader.emitWarning(`[${instanceName}] TypeScript declaration files should never be required`);
return callback(null, '');
}

let compiledModule;
if (instance.loaderConfig.usePrecompiledFiles) {
compiledModule = findCompiledModule(fileName);
Expand Down
2 changes: 2 additions & 0 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export interface Loader {
addDependency: (dep: string) => void;
clearDependencies: () => void;
emitFile: (fileName: string, text: string) => void;
emitWarning: (msg: string) => void;
emitError: (msg: string) => void;
options: {
ts?: LoaderConfig
};
Expand Down

0 comments on commit 0058cc5

Please sign in to comment.