Skip to content
This repository was archived by the owner on Jan 26, 2019. It is now read-only.

Commit 7abc0bb

Browse files
author
Koen Van Geert
committed
Add support for happypack to get faster builds on large projects
1 parent bdc024d commit 7abc0bb

File tree

4 files changed

+71
-22
lines changed

4 files changed

+71
-22
lines changed

packages/react-scripts/config/paths.js

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ module.exports = {
5959
testsSetup: resolveApp('src/setupTests.ts'),
6060
appNodeModules: resolveApp('node_modules'),
6161
appTsConfig: resolveApp('tsconfig.json'),
62+
appTsLint: resolveApp('tslint.json'),
6263
publicUrl: getPublicUrl(resolveApp('package.json')),
6364
servedPath: getServedPath(resolveApp('package.json')),
6465
};
@@ -80,6 +81,7 @@ module.exports = {
8081
testsSetup: resolveApp('src/setupTests.ts'),
8182
appNodeModules: resolveApp('node_modules'),
8283
appTsConfig: resolveApp('tsconfig.json'),
84+
appTsLint: resolveApp('tslint.json'),
8385
appTsTestConfig: resolveApp('tsconfig.test.json'),
8486
publicUrl: getPublicUrl(resolveApp('package.json')),
8587
servedPath: getServedPath(resolveApp('package.json')),
@@ -112,6 +114,7 @@ if (
112114
testsSetup: resolveOwn('template/src/setupTests.ts'),
113115
appNodeModules: resolveOwn('node_modules'),
114116
appTsConfig: resolveOwn('template/tsconfig.json'),
117+
appTsLint: resolveOwn('template/tslint.json'),
115118
appTsTestConfig: resolveOwn('template/tsconfig.test.json'),
116119
publicUrl: getPublicUrl(resolveOwn('package.json')),
117120
servedPath: getServedPath(resolveOwn('package.json')),

packages/react-scripts/config/webpack.config.dev.js

+32-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
1616
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
1717
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
1818
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
19+
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
20+
const HappyPack = require('happypack');
21+
const os = require('os');
1922
const getClientEnvironment = require('./env');
2023
const paths = require('./paths');
2124

@@ -119,15 +122,6 @@ module.exports = {
119122
// TODO: Disable require.ensure as it's not a standard language feature.
120123
// We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
121124
// { parser: { requireEnsure: false } },
122-
123-
// First, run the linter.
124-
// It's important to do this before Babel processes the JS.
125-
{
126-
test: /\.(ts|tsx)$/,
127-
loader: require.resolve('tslint-loader'),
128-
enforce: 'pre',
129-
include: paths.appSrc,
130-
},
131125
{
132126
test: /\.js$/,
133127
loader: require.resolve('source-map-loader'),
@@ -154,7 +148,14 @@ module.exports = {
154148
{
155149
test: /\.(ts|tsx)$/,
156150
include: paths.appSrc,
157-
loader: require.resolve('ts-loader'),
151+
use: [
152+
{
153+
loader: require.resolve('happypack/loader'),
154+
options: {
155+
id: 'ts',
156+
},
157+
},
158+
],
158159
},
159160
// Process JS with Babel.
160161
{
@@ -264,6 +265,27 @@ module.exports = {
264265
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
265266
// You can remove this if you don't use Moment.js:
266267
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
268+
269+
// Perform type checking and linting in a separate process to speed up compilation
270+
new ForkTsCheckerWebpackPlugin({
271+
async: false,
272+
tsconfig: paths.appTsConfig,
273+
tslint: paths.appTsLint,
274+
}),
275+
276+
// Makes initial webpack builds faster by transforming files in parallel.
277+
new HappyPack({
278+
id: 'ts',
279+
threads: Math.max(Math.min(os.cpus().length - 1, 4), 1),
280+
loaders: [
281+
{
282+
loader: require.resolve('ts-loader'),
283+
options: {
284+
happyPackMode: true,
285+
},
286+
},
287+
],
288+
}),
267289
],
268290
// Some libraries import Node modules but don't use them in the browser.
269291
// Tell Webpack to provide empty mocks for them so importing them works.

packages/react-scripts/config/webpack.config.prod.js

+33-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const ManifestPlugin = require('webpack-manifest-plugin');
1717
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
1818
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin');
1919
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
20+
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
21+
const HappyPack = require('happypack');
22+
const os = require('os');
2023
const paths = require('./paths');
2124
const getClientEnvironment = require('./env');
2225

@@ -50,7 +53,7 @@ const cssFilename = 'static/css/[name].[contenthash:8].css';
5053
// To have this structure working with relative paths, we have to use custom options.
5154
const extractTextPluginOptions = shouldUseRelativeAssetPaths
5255
? // Making sure that the publicPath goes back to to build folder.
53-
{ publicPath: Array(cssFilename.split('/').length).join('../') }
56+
{ publicPath: Array(cssFilename.split('/').length).join('../') }
5457
: {};
5558

5659
// This is the production configuration.
@@ -126,14 +129,6 @@ module.exports = {
126129
// We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
127130
// { parser: { requireEnsure: false } },
128131

129-
// First, run the linter.
130-
// It's important to do this before Babel processes the JS.
131-
{
132-
test: /\.(ts|tsx)$/,
133-
loader: require.resolve('tslint-loader'),
134-
enforce: 'pre',
135-
include: paths.appSrc,
136-
},
137132
{
138133
test: /\.js$/,
139134
loader: require.resolve('source-map-loader'),
@@ -159,7 +154,14 @@ module.exports = {
159154
{
160155
test: /\.(ts|tsx)$/,
161156
include: paths.appSrc,
162-
loader: require.resolve('ts-loader')
157+
use: [
158+
{
159+
loader: require.resolve('happypack/loader'),
160+
options: {
161+
id: 'ts',
162+
},
163+
},
164+
],
163165
},
164166
// Process JS with Babel.
165167
{
@@ -350,6 +352,27 @@ module.exports = {
350352
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
351353
// You can remove this if you don't use Moment.js:
352354
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
355+
356+
// Perform type checking and linting in a separate process to speed up compilation
357+
new ForkTsCheckerWebpackPlugin({
358+
async: false,
359+
tsconfig: paths.appTsConfig,
360+
tslint: paths.appTsLint,
361+
}),
362+
363+
// Makes initial webpack builds faster by transforming files in parallel.
364+
new HappyPack({
365+
id: 'ts',
366+
threads: Math.max(Math.min(os.cpus().length - 1, 4), 1),
367+
loaders: [
368+
{
369+
loader: require.resolve('ts-loader'),
370+
options: {
371+
happyPackMode: true,
372+
},
373+
},
374+
],
375+
}),
353376
],
354377
// Some libraries import Node modules but don't use them in the browser.
355378
// Tell Webpack to provide empty mocks for them so importing them works.

packages/react-scripts/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
"dotenv": "4.0.0",
3535
"extract-text-webpack-plugin": "3.0.2",
3636
"file-loader": "1.1.5",
37+
"fork-ts-checker-webpack-plugin": "^0.2.8",
3738
"fs-extra": "3.0.1",
39+
"happypack": "^4.0.0",
3840
"html-webpack-plugin": "2.29.0",
3941
"jest": "20.0.4",
4042
"object-assign": "4.1.1",
@@ -53,7 +55,6 @@
5355
"ts-jest": "^21.1.4",
5456
"ts-loader": "^3.1.1",
5557
"tslint": "^5.8.0",
56-
"tslint-loader": "^3.5.3",
5758
"tslint-react": "^3.2.0",
5859
"typescript": "~2.6.1",
5960
"source-map-loader": "^0.2.3"
@@ -65,4 +66,4 @@
6566
"optionalDependencies": {
6667
"fsevents": "1.1.2"
6768
}
68-
}
69+
}

0 commit comments

Comments
 (0)