Skip to content

Ref #76: Pass program for performance gain #78

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.iml
.idea
node_modules
npm-debug.log
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ module.exports = {

// enables type checked rules like 'for-in-array'
// uses tsconfig.json from current working directory
// See the notes on when using this with Webpack watch runs
typeCheck: false,

// automatically fix linting errors
Expand Down Expand Up @@ -115,6 +116,33 @@ module.exports = {
}
```

### typeCheck and Webpack Watch mode
Tslint will use a TypeScript program for typeChecked rules. The TypeScript program will cache file sources
when the program is created and therefore, the program needs to be recreated every watch run to pick
up file changes. This is accomplished using a Webpack plugin.

If you wish to use typeCheck in Webpack watch mode, you'll need to add the `TslintPlugin` to your Webpack config.

```js
var TslintPlugin = require('tslint-loader').TslintPlugin;

module.exports = {
plugins: [
new TslintPlugin()
],
module: {
rules: [
{
test: /\.ts$/,
enforce: 'pre',
loader: 'tslint-loader',
options: { typeCheck: true, /* Loader options go here */ }
}
]
}
}
```

## License

[MIT](http://www.opensource.org/licenses/mit-license.php)
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var objectAssign = require('object-assign');
var semver = require('semver');
var TslintPlugin = require('./lib/TslintPlugin');
var programInstance = require('./lib/programInstance');

function resolveFile(configPath) {
return path.isAbsolute(configPath)
Expand Down Expand Up @@ -63,7 +65,7 @@ function lint(webpackInstance, input, options) {
var program;
if (options.typeCheck) {
var tsconfigPath = resolveFile(options.tsConfigFile);
program = Lint.Linter.createProgram(tsconfigPath);
program = programInstance.getProgram(tsconfigPath);
}

var linter = new Lint.Linter(lintOptions, program);
Expand Down Expand Up @@ -140,4 +142,4 @@ module.exports = function(input, map) {
lint(this, input, options);
callback(null, input, map);
};

module.exports.TslintPlugin = TslintPlugin;
8 changes: 8 additions & 0 deletions lib/TslintPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var programInstance = require('./programInstance');

function TslintPlugin () {}
TslintPlugin.prototype.apply = function (compiler) {
compiler.plugin('done', programInstance.clearProgram);
};

module.exports = TslintPlugin;
20 changes: 20 additions & 0 deletions lib/programInstance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var Linter = require('tslint').Linter;

var program;

function getProgram(configFile,
projectDirectory) {
if (program == null) {
program = Linter.createProgram(configFile, projectDirectory);
}
return program;
}

function clearProgram() {
program = null;
}

module.exports = {
getProgram: getProgram,
clearProgram: clearProgram
};
4 changes: 4 additions & 0 deletions test/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var path = require('path');
var TslintPlugin = require('../index').TslintPlugin;

module.exports = function() {
return {
Expand All @@ -10,6 +11,9 @@ module.exports = function() {
resolve: {
extensions: ['.ts']
},
plugins: [
new TslintPlugin()
],
module: {
rules: [
{
Expand Down