diff --git a/.npmignore b/.npmignore index f900b6d..f721ab5 100644 --- a/.npmignore +++ b/.npmignore @@ -1,6 +1,3 @@ .tscache node_modules -dist -!/dist -!/dist.babel tscommand diff --git a/README.md b/README.md index 6deb4be..7bce0ab 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,11 @@ Array of paths to .d.ts files that must be included in program. Useful with `rew Use this setting to disable type checking. +### ignoreDiagnostics *(number[]) (default=[])* + +You can squelch certain TypeScript errors by specifying an array of [diagnostic codes](https://github.com/Microsoft/TypeScript/blob/master/src/compiler/diagnosticMessages.json) to ignore. +For example, you can transpile [stage 1 properties](https://github.com/jeffmo/es-class-fields-and-static-properties) from `*.js` using `"ignoreDiagnostics": [8014]`. + ### forkChecker *(boolean) (default=false)* Do type checking in a separate process, so webpack doesn't need to wait. **Significantly** improves development workflow with tools like [react-hot-loader](https://github.com/gaearon/react-hot-loader). @@ -169,7 +174,7 @@ Directory when cache is stored. Invoke glob resolver using 'filesGlob' and 'exclude' sections of `tsconfig`. -### skipDeclarationFilesCheck *(string) (default=fase)* +### skipDeclarationFilesCheck *(string) (default=false)* Skip declaration files typechecking. Use this only if you understand consequences. diff --git a/package.json b/package.json index 4525f28..fd74a53 100644 --- a/package.json +++ b/package.json @@ -4,14 +4,17 @@ "description": "Awesome TS loader for webpack", "main": "dist.babel/entry.js", "scripts": { - "prepublish": "npm run ts-build && npm run babel-build && grunt", - "ts": "./node_modules/.bin/tsc --project src --watch --pretty --diagnostics", - "ts-build": "./node_modules/.bin/tsc --project src --pretty", - "babel": "babel dist --presets es2015 --out-dir dist.babel --watch", - "babel-build": "babel dist --presets es2015 --out-dir dist.babel", + "prepublish": "npm run test && grunt", + "pretest": "npm run build", "test": "mocha dist.babel/test", - "build": "npm run grunt && npm run babel-compile", - "lint": "./node_modules/.bin/tslint src/*.ts" + "watch": "npm run watch:ts && npm run watch:babel", + "watch:ts": "npm run build:ts -- --watch --diagnostics", + "watch:babel": "npm run build:babel -- --watch", + "prebuild": "npm run lint", + "build": "npm run build:ts && npm run build:babel", + "build:ts": "tsc -p src --pretty", + "build:babel": "babel dist -d dist.babel", + "lint": "tslint src/*.ts" }, "author": "Stanislav Panferov (http://panferov.me/)", "repository": { @@ -59,7 +62,6 @@ "grunt-contrib-copy": "^0.7.0", "grunt-conventional-changelog": "^1.2.1", "grunt-shell": "^1.1.2", - "grunt-ts": "^3.0.0", "load-grunt-tasks": "^0.6.0", "mkdirp": "^0.5.1", "mocha": "^2.3.4", diff --git a/src/helpers.ts b/src/helpers.ts index 9be6bf2..5bdbee1 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -58,20 +58,18 @@ export function codegenErrorReport(errors) { .join('\n'); } -export function formatErrors(instanceName: string, errors: ts.Diagnostic[]) { - return errors.map(function (diagnostic) { - let lineChar; - if (diagnostic.file) { - lineChar = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); - } - return ( - `[${ instanceName }] ` + (diagnostic.file ? diagnostic.file.fileName : '') - + (lineChar ? formatLineChar(lineChar) + ' ' : '') + "\n" - + (typeof diagnostic.messageText == "string" ? - diagnostic.messageText : - formatMessageChain(diagnostic.messageText)) - ); - }); +export function formatError(diagnostic) { + let lineChar; + if (diagnostic.file) { + lineChar = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); + } + return ( + (diagnostic.file ? diagnostic.file.fileName : '') + + (lineChar ? formatLineChar(lineChar) + ' ' : '') + "\n" + + (typeof diagnostic.messageText == "string" ? + diagnostic.messageText : + formatMessageChain(diagnostic.messageText)) + ); } export function formatMessageChain(chain: ts.DiagnosticMessageChain) { diff --git a/src/host.ts b/src/host.ts index 9f012c6..d22ce0f 100644 --- a/src/host.ts +++ b/src/host.ts @@ -41,6 +41,7 @@ export interface ICompilerOptions extends ts.CompilerOptions { exclude?: string[]; externals?: any; doTypeCheck?: boolean; + ignoreDiagnostics?: number[]; forkChecker?: boolean; forkCheckerSilent?: boolean; useBabel?: boolean; diff --git a/src/instance.ts b/src/instance.ts index 187d4cf..6e92900 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -3,7 +3,7 @@ import * as fs from 'fs'; import * as path from 'path'; import * as _ from 'lodash'; import * as tsconfig from 'tsconfig'; -import { loadLib, formatErrors } from './helpers'; +import { loadLib, formatError } from './helpers'; import { ICompilerInfo } from './host'; import { createResolver } from './deps'; import { createChecker } from './checker'; @@ -358,16 +358,19 @@ function setupAfterCompile(compiler, instanceName, forkChecker = false) { } let diagnostics = state.ts.getPreEmitDiagnostics(state.program); - let emitError = (err) => { + let emitError = (msg) => { if (compilation.bail) { - console.error('Error in bail mode:', err); + console.error('Error in bail mode:', msg); process.exit(1); } - compilation.errors.push(new Error(err)); + compilation.errors.push(new Error(msg)); }; - let errors = formatErrors(instanceName, diagnostics); - errors.forEach(emitError); + let {options: {ignoreDiagnostics}} = instance; + diagnostics + .filter(err => !ignoreDiagnostics || ignoreDiagnostics.indexOf(err.code) == -1) + .map(err => `[${ instanceName }] ` + formatError(err)) + .forEach(emitError); instance.initedPlugins.forEach(plugin => { plugin.processProgram(state.program); diff --git a/src/test/index.ts b/src/test/index.ts index 3496bd6..2acfbaf 100644 --- a/src/test/index.ts +++ b/src/test/index.ts @@ -28,6 +28,17 @@ describe('main test', function() { expect(stats.compilation.errors.length).eq(1); }); + it('should ignore diagnostics', async function() { + let config = { + entry: fixturePath(['errors', 'with-type-errors.ts']) + }; + + let loaderQuery = { ignoreDiagnostics: [2345] }; + + let stats = await cleanAndCompile(createConfig(config, { loaderQuery })); + expect(stats.compilation.errors.length).eq(0); + }); + it('should load tsx files and use tsconfig', async function() { let tsconfig = fixturePath(['tsx', 'tsconfig.json']); let config = { diff --git a/src/tsconfig.json b/src/tsconfig.json index c325696..6ad5102 100644 --- a/src/tsconfig.json +++ b/src/tsconfig.json @@ -2,13 +2,11 @@ "compilerOptions": { "target": "es6", "outDir": "../dist", - "declaration": false, "moduleResolution": "node", "skipDefaultLibCheck": true, "allowSyntheticDefaultImports": true, "noImplicitAny": false, "removeComments": true, - "noLib": false, "preserveConstEnums": true, "suppressImplicitAnyIndexErrors": true },