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

Commit

Permalink
feat(*): add glob warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Panferov committed Nov 26, 2015
1 parent 225da92 commit 0cb6250
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ Use internal file cache. Useful with Babel, when processing takes a long time to

Directory when cache is stored.

### resolveGlobs *(string) (default=true)*

Invoke glob resolver using 'filesGlob' and 'exclude' sections of `tsconfig`.

## Compiler options

You can pass compiler options inside loader query string or in tsconfig file.
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"loader-utils": "^0.2.6",
"lodash": "^3.10.0",
"object-assign": "^2.1.1",
"parse-json": "^2.2.0",
"strip-bom": "^2.0.0",
"strip-json-comments": "^2.0.0",
"tsconfig": "^2.1.1"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface ICompilerOptions extends ts.CompilerOptions {
useCache?: boolean;
cacheDirectory?: string;
files?: any;
resolveGlobs?: boolean;
}

export interface IOutputFile extends ts.OutputFile {
Expand Down
15 changes: 12 additions & 3 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { loadLib, formatErrors } from './helpers';
import { ICompilerInfo } from './host';
import { createResolver } from './deps';
import { createChecker } from './checker';
import { rawToTsCompilerOptions } from './tsconfig-utils';
import { rawToTsCompilerOptions, parseContent, tsconfigSuggestions } from './tsconfig-utils';
import makeResolver from './resolver';

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

let tsImpl: typeof ts;
try {
tsImpl = require(compilerName);
tsImpl = require(compilerPath);
} catch (e) {
console.error(e)
console.error(COMPILER_ERROR);
Expand All @@ -128,12 +128,21 @@ export function ensureInstance(webpack: IWebPack, options: ICompilerOptions, ins
lib6: loadLib(lib6Path)
};

_.defaults(options, {
resolveGlobs: true
});

let configFilePath: string;
let configFile: tsconfig.TSConfig;
let folder = options.tsconfig || process.cwd();
configFilePath = tsconfig.resolveSync(folder);
if (configFilePath) {
configFile = tsconfig.readFileSync(configFilePath);
configFile = parseContent(fs.readFileSync(configFilePath).toString(), configFilePath);

if (options.resolveGlobs) {
tsconfigSuggestions(configFile);
configFile = tsconfig.readFileSync(configFilePath);
}
}

let tsFiles: string[] = [];
Expand Down
45 changes: 45 additions & 0 deletions src/tsconfig-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
import * as path from 'path';
import * as colors from 'colors';
import { TSConfig } from 'tsconfig';

let parseJson = require('parse-json');
let stripBom = require('strip-bom');
let stripComments = require('strip-json-comments');

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:
{
// ...
"exclude": [
"node_modules",
"bower_components"
]
}
`);

export function tsconfigSuggestions(config: TSConfig) {
let hasExclude = config.exclude && (
config.exclude.indexOf('node_modules') !== -1
|| config.exclude.indexOf('./node_modules') !== -1
);

let hasGlobIgnore = config.filesGlob && (
config.filesGlob.some(item => item.indexOf('!node_modules') !== -1)
|| !config.filesGlob.some(item => item.indexOf('!./node_modules') !== -1))

if (!hasExclude && !hasGlobIgnore) {
console.warn(TSCONFIG_ERROR);
}
}

/**
* Parse `tsconfig.json` file.
*/
export function parseContent(contents: string, filename: string): TSConfig {
const data = stripComments(stripBom(contents))

// A tsconfig.json file is permitted to be completely empty.
if (/^\s*$/.test(data)) {
return {}
}

return parseJson(data, null, filename)
}

function buildEnumMap(tsImpl: typeof ts) {
let typescriptEnumMap = {
Expand Down

0 comments on commit 0cb6250

Please sign in to comment.