Skip to content

Commit 9e408c5

Browse files
ianschmitzjohnnyreilly
authored andcommitted
Add compilerOptions option (#173)
* Add compilerOptions option * Add unit tests * Update version and changelog * Update README.md Co-Authored-By: ianschmitz <[email protected]>
1 parent f57ece2 commit 9e408c5

10 files changed

+125
-10
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v0.4.12
2+
3+
* [Add `compilerOptions` option](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/173) (#173)
4+
15
## v0.4.11
26

37
* [Fix os.cpus is not a function](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/172) (#172)

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ It helps to distinguish lints from typescript's diagnostics.
6060
* **tsconfig** `string`:
6161
Path to *tsconfig.json* file. Default: `path.resolve(compiler.options.context, './tsconfig.json')`.
6262

63+
* **compilerOptions** `object`:
64+
Allows overriding TypeScript options. Should be specified in the same format as you would do for the `compilerOptions` property in tsconfig.json. Default: `{}`.
65+
6366
* **tslint** `string | true`:
6467
Path to *tslint.json* file or `true`. If `true`, uses `path.resolve(compiler.options.context, './tslint.json')`. Default: `undefined`.
6568

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fork-ts-checker-webpack-plugin",
3-
"version": "0.4.11",
3+
"version": "0.4.12",
44
"description": "Runs typescript type checker and linter on separate process.",
55
"main": "lib/index.js",
66
"types": "lib/types/index.d.ts",

Diff for: src/IncrementalChecker.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface ConfigurationFile extends Configuration.IConfigurationFile {
2222

2323
export class IncrementalChecker {
2424
programConfigFile: string;
25+
compilerOptions: object;
2526
linterConfigFile: string | false;
2627
watchPaths: string[];
2728
workNumber: number;
@@ -41,6 +42,7 @@ export class IncrementalChecker {
4142

4243
constructor(
4344
programConfigFile: string,
45+
compilerOptions: object,
4446
linterConfigFile: string | false,
4547
watchPaths: string[],
4648
workNumber: number,
@@ -49,6 +51,7 @@ export class IncrementalChecker {
4951
vue: boolean
5052
) {
5153
this.programConfigFile = programConfigFile;
54+
this.compilerOptions = compilerOptions;
5255
this.linterConfigFile = linterConfigFile;
5356
this.watchPaths = watchPaths;
5457
this.workNumber = workNumber || 0;
@@ -68,15 +71,19 @@ export class IncrementalChecker {
6871
}));
6972
}
7073

71-
static loadProgramConfig(configFile: string) {
72-
return ts.parseJsonConfigFileContent(
74+
static loadProgramConfig(configFile: string, compilerOptions: object) {
75+
const parsed = ts.parseJsonConfigFileContent(
7376
// Regardless of the setting in the tsconfig.json we want isolatedModules to be false
7477
Object.assign(ts.readConfigFile(configFile, ts.sys.readFile).config, {
7578
isolatedModules: false
7679
}),
7780
ts.sys,
7881
path.dirname(configFile)
7982
);
83+
84+
parsed.options = { ...parsed.options, ...compilerOptions };
85+
86+
return parsed;
8087
}
8188

8289
static loadLinterConfig(configFile: string): ConfigurationFile {
@@ -189,7 +196,10 @@ export class IncrementalChecker {
189196
loadVueProgram() {
190197
this.programConfig =
191198
this.programConfig ||
192-
VueProgram.loadProgramConfig(this.programConfigFile);
199+
VueProgram.loadProgramConfig(
200+
this.programConfigFile,
201+
this.compilerOptions
202+
);
193203

194204
return VueProgram.createProgram(
195205
this.programConfig,
@@ -203,7 +213,10 @@ export class IncrementalChecker {
203213
loadDefaultProgram() {
204214
this.programConfig =
205215
this.programConfig ||
206-
IncrementalChecker.loadProgramConfig(this.programConfigFile);
216+
IncrementalChecker.loadProgramConfig(
217+
this.programConfigFile,
218+
this.compilerOptions
219+
);
207220

208221
return IncrementalChecker.createProgram(
209222
this.programConfig,

Diff for: src/VueProgram.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface ResolvedScript {
1212
}
1313

1414
export class VueProgram {
15-
static loadProgramConfig(configFile: string) {
15+
static loadProgramConfig(configFile: string, compilerOptions: object) {
1616
const extraExtensions = ['vue'];
1717

1818
const parseConfigHost: ts.ParseConfigHost = {
@@ -40,6 +40,7 @@ export class VueProgram {
4040
);
4141

4242
parsed.options.allowNonTsExtensions = true;
43+
parsed.options = { ...parsed.options, ...compilerOptions };
4344

4445
return parsed;
4546
}

Diff for: src/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ interface Logger {
3939

4040
interface Options {
4141
tsconfig: string;
42+
compilerOptions: object;
4243
tslint: string | true;
4344
watch: string | string[];
4445
async: boolean;
@@ -71,6 +72,7 @@ class ForkTsCheckerWebpackPlugin {
7172

7273
options: Partial<Options>;
7374
tsconfig: string;
75+
compilerOptions: object;
7476
tslint: string | true;
7577
watch: string[];
7678
ignoreDiagnostics: number[];
@@ -114,6 +116,10 @@ class ForkTsCheckerWebpackPlugin {
114116
this.options = Object.assign({}, options);
115117

116118
this.tsconfig = options.tsconfig || './tsconfig.json';
119+
this.compilerOptions =
120+
typeof options.compilerOptions === 'object'
121+
? options.compilerOptions
122+
: {};
117123
this.tslint = options.tslint
118124
? options.tslint === true
119125
? './tslint.json'
@@ -554,6 +560,7 @@ class ForkTsCheckerWebpackPlugin {
554560
: ['--max-old-space-size=' + this.memoryLimit],
555561
env: Object.assign({}, process.env, {
556562
TSCONFIG: this.tsconfigPath,
563+
COMPILER_OPTIONS: JSON.stringify(this.compilerOptions),
557564
TSLINT: this.tslintPath || '',
558565
WATCH: this.isWatching ? this.watchPaths.join('|') : '',
559566
WORK_DIVISION: Math.max(1, this.workersNumber),

Diff for: src/service.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { NormalizedMessage } from './NormalizedMessage';
66

77
const checker = new IncrementalChecker(
88
process.env.TSCONFIG,
9+
JSON.parse(process.env.COMPILER_OPTIONS),
910
process.env.TSLINT === '' ? false : process.env.TSLINT,
1011
process.env.WATCH === '' ? [] : process.env.WATCH.split('|'),
1112
parseInt(process.env.WORK_NUMBER, 10),

Diff for: test/integration/vue.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ describe('[INTEGRATION] vue', function() {
7676

7777
checker = new IncrementalChecker(
7878
plugin.tsconfigPath,
79+
{},
7980
plugin.tslintPath || false,
8081
[compiler.context],
8182
ForkTsCheckerWebpackPlugin.ONE_CPU,

Diff for: test/unit/IncrementalChecker.spec.js

+41-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,36 @@ var it = require('mocha').it;
33
var expect = require('chai').expect;
44
var path = require('path');
55
var minimatch = require('minimatch');
6-
7-
var IncrementalChecker = require('../../lib/IncrementalChecker')
8-
.IncrementalChecker;
6+
var mockRequire = require('mock-require');
7+
var sinon = require('sinon');
98

109
describe('[UNIT] IncrementalChecker', function() {
10+
var IncrementalChecker;
11+
12+
beforeEach(function() {
13+
var parseJsonConfigFileContentStub = sinon.stub().returns({
14+
options: {
15+
foo: true
16+
}
17+
});
18+
var readConfigFileStub = sinon.stub().returns({
19+
config: {}
20+
});
21+
22+
mockRequire('typescript', {
23+
parseJsonConfigFileContent: parseJsonConfigFileContentStub,
24+
readConfigFile: readConfigFileStub,
25+
sys: {}
26+
});
27+
28+
IncrementalChecker = mockRequire.reRequire('../../lib/IncrementalChecker')
29+
.IncrementalChecker;
30+
});
31+
32+
afterEach(function() {
33+
mockRequire.stopAll();
34+
});
35+
1136
describe('isFileExcluded', function() {
1237
it('should properly filter definition files and listed exclusions', function() {
1338
var linterConfig = {
@@ -41,4 +66,17 @@ describe('[UNIT] IncrementalChecker', function() {
4166
expect(pathsAreExcluded[3]).to.be.true;
4267
});
4368
});
69+
70+
describe('loadProgramConfig', function() {
71+
it('merges compilerOptions into returned options', function() {
72+
var result = IncrementalChecker.loadProgramConfig('tsconfig.foo.json', {
73+
bar: false
74+
});
75+
76+
expect(result.options).to.deep.equal({
77+
foo: true,
78+
bar: false
79+
});
80+
});
81+
});
4482
});

Diff for: test/unit/VueProgram.spec.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,36 @@ var ts = require('typescript');
22
var describe = require('mocha').describe;
33
var it = require('mocha').it;
44
var expect = require('chai').expect;
5-
var VueProgram = require('../../lib/VueProgram').VueProgram;
5+
var mockRequire = require('mock-require');
6+
var sinon = require('sinon');
67

78
describe('[UNIT] VueProgram', function() {
9+
var VueProgram;
10+
11+
beforeEach(function() {
12+
var parseJsonConfigFileContentStub = sinon.stub().returns({
13+
options: {
14+
foo: true
15+
}
16+
});
17+
var readConfigFileStub = sinon.stub().returns({
18+
config: {}
19+
});
20+
21+
mockRequire('typescript', {
22+
parseJsonConfigFileContent: parseJsonConfigFileContentStub,
23+
readConfigFile: readConfigFileStub,
24+
sys: {},
25+
ScriptKind: ts.ScriptKind
26+
});
27+
28+
VueProgram = mockRequire.reRequire('../../lib/VueProgram').VueProgram;
29+
});
30+
31+
afterEach(function() {
32+
mockRequire.stopAll();
33+
});
34+
835
it('should determine if file is a Vue file', function() {
936
expect(VueProgram.isVue('./test.vue')).to.be.true;
1037
expect(VueProgram.isVue('../test.vue')).to.be.true;
@@ -130,4 +157,24 @@ describe('[UNIT] VueProgram', function() {
130157
].join('\n')
131158
);
132159
});
160+
161+
describe('loadProgramConfig', function() {
162+
it('sets allowNonTsExtensions to true on returned options', function() {
163+
var result = VueProgram.loadProgramConfig('tsconfig.foo.json', {});
164+
165+
expect(result.options.allowNonTsExtensions).to.equal(true);
166+
});
167+
168+
it('merges compilerOptions into returned options', function() {
169+
var result = VueProgram.loadProgramConfig('tsconfig.foo.json', {
170+
bar: false
171+
});
172+
173+
expect(result.options).to.deep.equal({
174+
foo: true,
175+
bar: false,
176+
allowNonTsExtensions: true
177+
});
178+
});
179+
});
133180
});

0 commit comments

Comments
 (0)