Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compilerOptions option #173

Merged
merged 4 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v0.4.12

* [Add `compilerOptions` option](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/173) (#173)

## v0.4.11

* [Fix os.cpus is not a function](https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/172) (#172)
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ It helps to distinguish lints from typescript's diagnostics.
* **tsconfig** `string`:
Path to *tsconfig.json* file. Default: `path.resolve(compiler.options.context, './tsconfig.json')`.

* **compilerOptions** `object`:
Allows overriding TypeScript options. Should be specified in the same format as you would do for the `compilerOptions` property in tsconfig.json. Default: `{}`.

* **tslint** `string | true`:
Path to *tslint.json* file or `true`. If `true`, uses `path.resolve(compiler.options.context, './tslint.json')`. Default: `undefined`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the documented type of compilerOptions be object instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right. Good catch! Was a late night for me yesterday 😛


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fork-ts-checker-webpack-plugin",
"version": "0.4.11",
"version": "0.4.12",
"description": "Runs typescript type checker and linter on separate process.",
"main": "lib/index.js",
"types": "lib/types/index.d.ts",
Expand Down
21 changes: 17 additions & 4 deletions src/IncrementalChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface ConfigurationFile extends Configuration.IConfigurationFile {

export class IncrementalChecker {
programConfigFile: string;
compilerOptions: object;
linterConfigFile: string | false;
watchPaths: string[];
workNumber: number;
Expand All @@ -41,6 +42,7 @@ export class IncrementalChecker {

constructor(
programConfigFile: string,
compilerOptions: object,
linterConfigFile: string | false,
watchPaths: string[],
workNumber: number,
Expand All @@ -49,6 +51,7 @@ export class IncrementalChecker {
vue: boolean
) {
this.programConfigFile = programConfigFile;
this.compilerOptions = compilerOptions;
this.linterConfigFile = linterConfigFile;
this.watchPaths = watchPaths;
this.workNumber = workNumber || 0;
Expand All @@ -68,15 +71,19 @@ export class IncrementalChecker {
}));
}

static loadProgramConfig(configFile: string) {
return ts.parseJsonConfigFileContent(
static loadProgramConfig(configFile: string, compilerOptions: object) {
const parsed = ts.parseJsonConfigFileContent(
// Regardless of the setting in the tsconfig.json we want isolatedModules to be false
Object.assign(ts.readConfigFile(configFile, ts.sys.readFile).config, {
isolatedModules: false
}),
ts.sys,
path.dirname(configFile)
);

parsed.options = { ...parsed.options, ...compilerOptions };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we can use object spread as it won't work on node 6 (and fork-ts-checker supports node 6). https://node.green/#ES2018-features-object-rest-spread-properties-object-spread-properties

Can we switch to Object.assign please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typescript will compile it down to Object.assign for you as long as your target is correct. It's a little tough for me to check as I'm on my phone right now but will double check tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use assign elsewhere in code base I don't mind changing either way!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typescript will compile it down to Object.assign for you as long as your target is correct. It's a little tough for me to check as I'm on my phone right now but will double check tomorrow.

If that's the case then that's fine with me!


return parsed;
}

static loadLinterConfig(configFile: string): ConfigurationFile {
Expand Down Expand Up @@ -189,7 +196,10 @@ export class IncrementalChecker {
loadVueProgram() {
this.programConfig =
this.programConfig ||
VueProgram.loadProgramConfig(this.programConfigFile);
VueProgram.loadProgramConfig(
this.programConfigFile,
this.compilerOptions
);

return VueProgram.createProgram(
this.programConfig,
Expand All @@ -203,7 +213,10 @@ export class IncrementalChecker {
loadDefaultProgram() {
this.programConfig =
this.programConfig ||
IncrementalChecker.loadProgramConfig(this.programConfigFile);
IncrementalChecker.loadProgramConfig(
this.programConfigFile,
this.compilerOptions
);

return IncrementalChecker.createProgram(
this.programConfig,
Expand Down
3 changes: 2 additions & 1 deletion src/VueProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface ResolvedScript {
}

export class VueProgram {
static loadProgramConfig(configFile: string) {
static loadProgramConfig(configFile: string, compilerOptions: object) {
const extraExtensions = ['vue'];

const parseConfigHost: ts.ParseConfigHost = {
Expand Down Expand Up @@ -40,6 +40,7 @@ export class VueProgram {
);

parsed.options.allowNonTsExtensions = true;
parsed.options = { ...parsed.options, ...compilerOptions };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we can use object spread as it won't work on node 6 (and fork-ts-checker supports node 6). https://node.green/#ES2018-features-object-rest-spread-properties-object-spread-properties

Can we switch to Object.assign please?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again - fine with this staying as is if TypeScript transpiles us to glory! 😄


return parsed;
}
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface Logger {

interface Options {
tsconfig: string;
compilerOptions: object;
tslint: string | true;
watch: string | string[];
async: boolean;
Expand Down Expand Up @@ -71,6 +72,7 @@ class ForkTsCheckerWebpackPlugin {

options: Partial<Options>;
tsconfig: string;
compilerOptions: object;
tslint: string | true;
watch: string[];
ignoreDiagnostics: number[];
Expand Down Expand Up @@ -114,6 +116,10 @@ class ForkTsCheckerWebpackPlugin {
this.options = Object.assign({}, options);

this.tsconfig = options.tsconfig || './tsconfig.json';
this.compilerOptions =
typeof options.compilerOptions === 'object'
? options.compilerOptions
: {};
this.tslint = options.tslint
? options.tslint === true
? './tslint.json'
Expand Down Expand Up @@ -554,6 +560,7 @@ class ForkTsCheckerWebpackPlugin {
: ['--max-old-space-size=' + this.memoryLimit],
env: Object.assign({}, process.env, {
TSCONFIG: this.tsconfigPath,
COMPILER_OPTIONS: JSON.stringify(this.compilerOptions),
TSLINT: this.tslintPath || '',
WATCH: this.isWatching ? this.watchPaths.join('|') : '',
WORK_DIVISION: Math.max(1, this.workersNumber),
Expand Down
1 change: 1 addition & 0 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NormalizedMessage } from './NormalizedMessage';

const checker = new IncrementalChecker(
process.env.TSCONFIG,
JSON.parse(process.env.COMPILER_OPTIONS),
process.env.TSLINT === '' ? false : process.env.TSLINT,
process.env.WATCH === '' ? [] : process.env.WATCH.split('|'),
parseInt(process.env.WORK_NUMBER, 10),
Expand Down
1 change: 1 addition & 0 deletions test/integration/vue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ describe('[INTEGRATION] vue', function() {

checker = new IncrementalChecker(
plugin.tsconfigPath,
{},
plugin.tslintPath || false,
[compiler.context],
ForkTsCheckerWebpackPlugin.ONE_CPU,
Expand Down
44 changes: 41 additions & 3 deletions test/unit/IncrementalChecker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,36 @@ var it = require('mocha').it;
var expect = require('chai').expect;
var path = require('path');
var minimatch = require('minimatch');

var IncrementalChecker = require('../../lib/IncrementalChecker')
.IncrementalChecker;
var mockRequire = require('mock-require');
var sinon = require('sinon');

describe('[UNIT] IncrementalChecker', function() {
var IncrementalChecker;

beforeEach(function() {
var parseJsonConfigFileContentStub = sinon.stub().returns({
options: {
foo: true
}
});
var readConfigFileStub = sinon.stub().returns({
config: {}
});

mockRequire('typescript', {
parseJsonConfigFileContent: parseJsonConfigFileContentStub,
readConfigFile: readConfigFileStub,
sys: {}
});

IncrementalChecker = mockRequire.reRequire('../../lib/IncrementalChecker')
.IncrementalChecker;
});

afterEach(function() {
mockRequire.stopAll();
});

describe('isFileExcluded', function() {
it('should properly filter definition files and listed exclusions', function() {
var linterConfig = {
Expand Down Expand Up @@ -41,4 +66,17 @@ describe('[UNIT] IncrementalChecker', function() {
expect(pathsAreExcluded[3]).to.be.true;
});
});

describe('loadProgramConfig', function() {
it('merges compilerOptions into returned options', function() {
var result = IncrementalChecker.loadProgramConfig('tsconfig.foo.json', {
bar: false
});

expect(result.options).to.deep.equal({
foo: true,
bar: false
});
});
});
});
49 changes: 48 additions & 1 deletion test/unit/VueProgram.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,36 @@ var ts = require('typescript');
var describe = require('mocha').describe;
var it = require('mocha').it;
var expect = require('chai').expect;
var VueProgram = require('../../lib/VueProgram').VueProgram;
var mockRequire = require('mock-require');
var sinon = require('sinon');

describe('[UNIT] VueProgram', function() {
var VueProgram;

beforeEach(function() {
var parseJsonConfigFileContentStub = sinon.stub().returns({
options: {
foo: true
}
});
var readConfigFileStub = sinon.stub().returns({
config: {}
});

mockRequire('typescript', {
parseJsonConfigFileContent: parseJsonConfigFileContentStub,
readConfigFile: readConfigFileStub,
sys: {},
ScriptKind: ts.ScriptKind
});

VueProgram = mockRequire.reRequire('../../lib/VueProgram').VueProgram;
});

afterEach(function() {
mockRequire.stopAll();
});

it('should determine if file is a Vue file', function() {
expect(VueProgram.isVue('./test.vue')).to.be.true;
expect(VueProgram.isVue('../test.vue')).to.be.true;
Expand Down Expand Up @@ -130,4 +157,24 @@ describe('[UNIT] VueProgram', function() {
].join('\n')
);
});

describe('loadProgramConfig', function() {
it('sets allowNonTsExtensions to true on returned options', function() {
var result = VueProgram.loadProgramConfig('tsconfig.foo.json', {});

expect(result.options.allowNonTsExtensions).to.equal(true);
});

it('merges compilerOptions into returned options', function() {
var result = VueProgram.loadProgramConfig('tsconfig.foo.json', {
bar: false
});

expect(result.options).to.deep.equal({
foo: true,
bar: false,
allowNonTsExtensions: true
});
});
});
});