Skip to content

Commit

Permalink
Use fork-ts-checker-webpack-plugin instead of tslint-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasterra committed May 6, 2018
1 parent c7e09cb commit 7ecc57a
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 59 deletions.
6 changes: 3 additions & 3 deletions examples/with-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
"@types/express": "^4.11.1",
"@types/jest": "^22.2.3",
"@types/node": "9.6.6",
"@types/react": "^16.3.12",
"@types/react": "^16.3.13",
"@types/react-dom": "^16.0.5",
"@types/react-router-dom": "^4.2.6",
"@types/webpack-env": "^1.13.6",
"razzle": "^2.0.0-alpha.12",
"razzle-plugin-typescript": "^2.0.0-alpha.12",
"ts-jest": "^22.4.4",
"tslint": "^5.9.1",
"ts-jest": "^22.4.5",
"tslint": "^5.10.0",
"tslint-react": "^3.5.1",
"typescript": "^2.8.3"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* tslint:disable:no-console */
import express from 'express';
import app from './server';

if (module.hot) {
module.hot.accept('./server', function() {
module.hot.accept('./server', () => {
console.log('🔁 HMR Reloading `./server`...');
});
console.info('✅ Server-side HMR Enabled!');
Expand All @@ -11,8 +12,8 @@ if (module.hot) {
const port = process.env.PORT || 3000;

export default express()
.use((req, res) => app.handle(req, res))
.listen(port, function(err) {
.use(app)
.listen(port, (err: Error) => {
if (err) {
console.error(err);
return;
Expand Down
2 changes: 1 addition & 1 deletion examples/with-typescript/src/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import App from './App';
let assets: any;

const syncLoadAssets = () => {
assets = require(process.env.RAZZLE_ASSETS_MANIFEST!);
assets = require(process.env.RAZZLE_ASSETS_MANIFEST!);
};
syncLoadAssets();

Expand Down
2 changes: 1 addition & 1 deletion examples/with-typescript/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"no-arg": true,
"no-bitwise": true,
"no-consecutive-blank-lines": true,
"no-console": false,
"no-console": true,
"no-construct": true,
"no-debugger": true,
"no-duplicate-variable": true,
Expand Down
18 changes: 9 additions & 9 deletions packages/razzle-plugin-typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ npm install --save razzle-plugin-typescript

Using the plugin with the default options


```js
// razzle.config.js

Expand Down Expand Up @@ -38,18 +37,19 @@ module.exports = {
// any babel transforms
useBabel: true,

// Any option you want to pass to tslint-loader: https://github.com/wbuchwalter/tslint-loader
tslintLoader: {
// Any option you want to pass to ts-loader: https://github.com/TypeStrong/ts-loader
tsLoader: {
transpileOnly: true,
experimentalWatchApi: true,
},

// Any option you want to pass to ts-loader: https://github.com/TypeStrong/ts-loader
tsLoader: {
emitErrors: true,
// Any option you want to pass to fork-ts-checker-webpack-plugin: https://github.com/Realytics/fork-ts-checker-webpack-plugin
forkTsChecker: {
tsconfig: './tsconfig.json',
tslint: './tslint.json',
watch: './src',
typeCheck: true,
configFile: './tslint.json',
tsConfigFile: './tsconfig.json',
},
}
},
},
],
Expand Down
25 changes: 13 additions & 12 deletions packages/razzle-plugin-typescript/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ const loaderFinder = loaderName => rule => {
// i.e.: /eslint-loader/
const loaderRegex = new RegExp(`[/\\\\]${loaderName}[/\\\\]`);

// Checks if there's a loader string in rule.loader matching loaderRegex.
const inLoaderString =
typeof rule.loader === 'string' && rule.loader.match(loaderRegex);

// Checks if there is an object inside rule.use with loader matching loaderRegex, OR
// If there's a loader string in rule.loader matching loaderRegex.
return (
(Array.isArray(rule.use) &&
rule.use.find(
loader =>
typeof loader.loader === 'string' && loader.loader.match(loaderRegex)
)) ||
(typeof rule.loader === 'string' && rule.loader.match(loaderRegex))
);
const inUseArray =
Array.isArray(rule.use) &&
rule.use.find(
loader =>
typeof loader.loader === 'string' && loader.loader.match(loaderRegex)
);

return inUseArray || inLoaderString;
};

const eslintLoaderFinder = loaderFinder('eslint-loader');
const babelLoaderFinder = loaderFinder('babel-loader');
const tslintLoaderFinder = loaderFinder('tslint-loader');
const tsLoaderFinder = loaderFinder('ts-loader');
const eslintLoaderFinder = loaderFinder('eslint-loader');

module.exports = {
eslintLoaderFinder,
babelLoaderFinder,
tslintLoaderFinder,
tsLoaderFinder,
};
42 changes: 18 additions & 24 deletions packages/razzle-plugin-typescript/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
'use strict';

const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const { babelLoaderFinder, eslintLoaderFinder } = require('./helpers');

const defaultOptions = {
useBabel: true,
tsLoader: {
transpileOnly: true,
experimentalWatchApi: true,
},
tslintLoader: {
emitErrors: true,
configFile: './tslint.json',
forkTsChecker: {
tsconfig: './tsconfig.json',
tslint: './tslint.json',
watch: ['./src'],
typeCheck: true,
tsConfigFile: './tsconfig.json',
},
};

function modify(baseConfig, configOptions, webpack, userOptions = {}) {
function modify(baseConfig, { target }, webpack, userOptions = {}) {
const options = Object.assign({}, defaultOptions, userOptions);
const config = Object.assign({}, baseConfig);

Expand All @@ -32,32 +34,14 @@ function modify(baseConfig, configOptions, webpack, userOptions = {}) {
const babelLoader = config.module.rules.find(babelLoaderFinder);
if (!babelLoader) {
throw new Error(
`'babel-loader' was erased from config, we need it to define 'include' option`
`'babel-loader' was erased from config, we need it to define 'include' option for 'ts-loader'`
);
}

// Get the correct `include` option, since that hasn't changed.
// This tells Razzle which directories to transform.
const { include } = babelLoader;

// Configure tslint-loader
const tslintLoader = {
include,
enforce: 'pre',
test: /\.tsx?$/,
use: [
{
loader: require.resolve('tslint-loader'),
options: Object.assign(
{},
defaultOptions.tslintLoader,
options.tslintLoader
),
},
],
};
config.module.rules.push(tslintLoader);

// Configure ts-loader
const tsLoader = {
include,
Expand All @@ -83,6 +67,16 @@ function modify(baseConfig, configOptions, webpack, userOptions = {}) {

config.module.rules.push(tsLoader);

// Do typechecking in a separate process,
// We can run it only in client builds.
if (target === 'web') {
config.plugins.push(
new ForkTsCheckerWebpackPlugin(
Object.assign({}, defaultOptions.forkTsChecker, options.forkTsChecker)
)
);
}

return config;
}

Expand Down
3 changes: 1 addition & 2 deletions packages/razzle-plugin-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
},
"dependencies": {
"ts-loader": "^4.2.0",
"tslint-loader": "^3.6.0"
"fork-ts-checker-webpack-plugin": "^0.4.1"
},
"devDependencies": {
"razzle": "^2.0.0-alpha.12",
"jest": "20.0.4"
},
"peerDependencies": {
"tslint": "*",
"typescript": ">=2.4.1"
}
}
28 changes: 24 additions & 4 deletions packages/razzle-plugin-typescript/tests/index.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const createConfig = require('razzle/config/createConfig');
const pluginFunc = require('../index');
const {
eslintLoaderFinder,
tslintLoaderFinder,
babelLoaderFinder,
tsLoaderFinder,
} = require('../helpers');
Expand All @@ -21,9 +21,12 @@ describe('razzle-typescript-plugin', () => {
expect(config.resolve.extensions).toContain('.tsx');
});

it('should add tslint-loader', () => {
const rule = config.module.rules.find(tslintLoaderFinder);
expect(rule).not.toBeUndefined();
it('should add fork-ts-checker-webpack-plugin', () => {
const tsCheckerPlugin = config.plugins.find(
plugin => plugin instanceof ForkTsCheckerWebpackPlugin
);

expect(tsCheckerPlugin).not.toBeUndefined();
});

it('should add ts-loader', () => {
Expand Down Expand Up @@ -55,4 +58,21 @@ describe('razzle-typescript-plugin', () => {
expect(rule).toBeUndefined();
});
});

describe('when creating a server config', () => {
let config;
beforeAll(() => {
config = createConfig('node', 'dev', {
plugins: [{ func: pluginFunc }],
});
});

it('should not add fork-ts-checker-webpack-plugin', () => {
const tsCheckerPlugin = config.plugins.find(
plugin => plugin instanceof ForkTsCheckerWebpackPlugin
);

expect(tsCheckerPlugin).toBeUndefined();
});
});
});

0 comments on commit 7ecc57a

Please sign in to comment.