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

Commit 0cb6250

Browse files
author
Stanislav Panferov
committed
feat(*): add glob warning
1 parent 225da92 commit 0cb6250

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ Use internal file cache. Useful with Babel, when processing takes a long time to
141141

142142
Directory when cache is stored.
143143

144+
### resolveGlobs *(string) (default=true)*
145+
146+
Invoke glob resolver using 'filesGlob' and 'exclude' sections of `tsconfig`.
147+
144148
## Compiler options
145149

146150
You can pass compiler options inside loader query string or in tsconfig file.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
"loader-utils": "^0.2.6",
3333
"lodash": "^3.10.0",
3434
"object-assign": "^2.1.1",
35+
"parse-json": "^2.2.0",
36+
"strip-bom": "^2.0.0",
37+
"strip-json-comments": "^2.0.0",
3538
"tsconfig": "^2.1.1"
3639
},
3740
"devDependencies": {

src/host.ts

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export interface ICompilerOptions extends ts.CompilerOptions {
4646
useCache?: boolean;
4747
cacheDirectory?: string;
4848
files?: any;
49+
resolveGlobs?: boolean;
4950
}
5051

5152
export interface IOutputFile extends ts.OutputFile {

src/instance.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { loadLib, formatErrors } from './helpers';
88
import { ICompilerInfo } from './host';
99
import { createResolver } from './deps';
1010
import { createChecker } from './checker';
11-
import { rawToTsCompilerOptions } from './tsconfig-utils';
11+
import { rawToTsCompilerOptions, parseContent, tsconfigSuggestions } from './tsconfig-utils';
1212
import makeResolver from './resolver';
1313

1414
let pkg = require('../package.json');
@@ -103,7 +103,7 @@ export function ensureInstance(webpack: IWebPack, options: ICompilerOptions, ins
103103

104104
let tsImpl: typeof ts;
105105
try {
106-
tsImpl = require(compilerName);
106+
tsImpl = require(compilerPath);
107107
} catch (e) {
108108
console.error(e)
109109
console.error(COMPILER_ERROR);
@@ -128,12 +128,21 @@ export function ensureInstance(webpack: IWebPack, options: ICompilerOptions, ins
128128
lib6: loadLib(lib6Path)
129129
};
130130

131+
_.defaults(options, {
132+
resolveGlobs: true
133+
});
134+
131135
let configFilePath: string;
132136
let configFile: tsconfig.TSConfig;
133137
let folder = options.tsconfig || process.cwd();
134138
configFilePath = tsconfig.resolveSync(folder);
135139
if (configFilePath) {
136-
configFile = tsconfig.readFileSync(configFilePath);
140+
configFile = parseContent(fs.readFileSync(configFilePath).toString(), configFilePath);
141+
142+
if (options.resolveGlobs) {
143+
tsconfigSuggestions(configFile);
144+
configFile = tsconfig.readFileSync(configFilePath);
145+
}
137146
}
138147

139148
let tsFiles: string[] = [];

src/tsconfig-utils.ts

+45
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,49 @@
11
import * as path from 'path';
2+
import * as colors from 'colors';
3+
import { TSConfig } from 'tsconfig';
4+
5+
let parseJson = require('parse-json');
6+
let stripBom = require('strip-bom');
7+
let stripComments = require('strip-json-comments');
8+
9+
const TSCONFIG_ERROR = colors.red(`\n\n[awesome-typescript-loader] You have \`resolveGlobs\` enabled and don't have an \`exclude\` directive in your tsconfig file. This WILL slow down your compilation. Please add:
10+
{
11+
// ...
12+
"exclude": [
13+
"node_modules",
14+
"bower_components"
15+
]
16+
}
17+
`);
18+
19+
export function tsconfigSuggestions(config: TSConfig) {
20+
let hasExclude = config.exclude && (
21+
config.exclude.indexOf('node_modules') !== -1
22+
|| config.exclude.indexOf('./node_modules') !== -1
23+
);
24+
25+
let hasGlobIgnore = config.filesGlob && (
26+
config.filesGlob.some(item => item.indexOf('!node_modules') !== -1)
27+
|| !config.filesGlob.some(item => item.indexOf('!./node_modules') !== -1))
28+
29+
if (!hasExclude && !hasGlobIgnore) {
30+
console.warn(TSCONFIG_ERROR);
31+
}
32+
}
33+
34+
/**
35+
* Parse `tsconfig.json` file.
36+
*/
37+
export function parseContent(contents: string, filename: string): TSConfig {
38+
const data = stripComments(stripBom(contents))
39+
40+
// A tsconfig.json file is permitted to be completely empty.
41+
if (/^\s*$/.test(data)) {
42+
return {}
43+
}
44+
45+
return parseJson(data, null, filename)
46+
}
247

348
function buildEnumMap(tsImpl: typeof ts) {
449
let typescriptEnumMap = {

0 commit comments

Comments
 (0)