Skip to content

Commit

Permalink
add cached loaders and plugins
Browse files Browse the repository at this point in the history
tweak settings

tweak variable names
  • Loading branch information
viankakrisna committed Jul 14, 2017
1 parent 1091876 commit 655a479
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea/
.vscode/
.cache-loader/
node_modules/
build
.DS_Store
Expand Down
69 changes: 69 additions & 0 deletions packages/react-dev-utils/createHashFromPaths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

const crypto = require('crypto');
const path = require('path');
const fs = require('fs');

const getSources = (paths = [], sourceMethod = function() {}, exclude = []) => {
const getSource = (somePath = '') => {
try {
if (fs.lstatSync(somePath).isDirectory()) {
if (
exclude.includes(somePath) ||
exclude.find(excluded => somePath.startsWith(excluded))
) {
return somePath;
}
const fileList = fs.readdirSync(somePath);
return getSources(
fileList.map(file => path.join(somePath, file)),
sourceMethod
);
} else {
return sourceMethod(somePath);
}
} catch (ignored) {
return somePath;
}
};
let result = '';
for (let i = paths.length - 1; i >= 0; i--) {
result += getSource(paths[i]);
}
return result;
};

const getSourceMethod = key => {
const cases = {
content: filePath => fs.readFileSync(filePath, 'utf-8'),
mtime: filePath => `${filePath}_${fs.statSync(filePath).mtime}`,
};

const found = cases[key];

if (found) {
return found;
}

console.error(
'Error from createHashFromPaths:\n\n',
'Source method is not recognized, possible values are:\n\n',
Object.keys(cases).map(e => '`' + e + '`').join(', '),
'\n'
);
process.exit(1);
};

const createHashFromPaths = ({ paths, exclude, method = 'mtime' }) => {
const hash = crypto.createHash('md5');
hash.update(paths.join(''));
if (Array.isArray(paths)) {
const sourceMethod = getSourceMethod(method);
const sources = getSources(paths, sourceMethod, exclude);
hash.update(sources);
}

return hash.digest('hex');
};

module.exports = createHashFromPaths;
1 change: 1 addition & 0 deletions packages/react-dev-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"checkRequiredFiles.js",
"clearConsole.js",
"crashOverlay.js",
"createHashFromPaths.js",
"crossSpawn.js",
"eslintFormatter.js",
"FileSizeReporter.js",
Expand Down
42 changes: 31 additions & 11 deletions packages/react-scripts/config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
const eslintFormatter = require('react-dev-utils/eslintFormatter');
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
const createHashFromPaths = require('react-dev-utils/createHashFromPaths');
const getClientEnvironment = require('./env');
const paths = require('./paths');
const AutoDllWebpackPlugin = require('autodll-webpack-plugin');

// Webpack uses `publicPath` to determine where the app is being served from.
// In development, we always serve from the root. This makes config easier.
Expand Down Expand Up @@ -170,17 +172,22 @@ module.exports = {
{
test: /\.(js|jsx)$/,
include: paths.appSrc,
loader: require.resolve('babel-loader'),
options: {
// @remove-on-eject-begin
babelrc: false,
presets: [require.resolve('babel-preset-react-app')],
// @remove-on-eject-end
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in ./node_modules/.cache/babel-loader/
// directory for faster rebuilds.
cacheDirectory: true,
},
use: [
require.resolve('cache-loader'),
{
loader: require.resolve('babel-loader'),
options: {
// @remove-on-eject-begin
babelrc: false,
presets: [require.resolve('babel-preset-react-app')],
// @remove-on-eject-end
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in ./node_modules/.cache/babel-loader/
// directory for faster rebuilds.
cacheDirectory: true,
},
},
],
},
// "postcss" loader applies autoprefixer to our CSS.
// "css" loader resolves paths in CSS and adds assets as dependencies.
Expand All @@ -190,6 +197,7 @@ module.exports = {
{
test: /\.css$/,
use: [
require.resolve('cache-loader'),
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
Expand Down Expand Up @@ -252,6 +260,18 @@ module.exports = {
inject: true,
template: paths.appHtml,
}),
new AutoDllWebpackPlugin({
env: process.env.NODE_ENV,
additionalHash: createHashFromPaths({
paths: [paths.appNodeModules],
exclude: [path.join(paths.appNodeModules, '.cache')],
}),
inject: true,
filename: '[name].[hash].js',
entry: {
vendor: ['react', 'react-dom'],
},
}),
// Add module names to factory functions so they appear in browser profiler.
new webpack.NamedModulesPlugin(),
// Makes some environment variables available to the JS code, for example:
Expand Down
39 changes: 30 additions & 9 deletions packages/react-scripts/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin');
const eslintFormatter = require('react-dev-utils/eslintFormatter');
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
const createHashFromPaths = require('react-dev-utils/createHashFromPaths');
const AutoDllWebpackPlugin = require('autodll-webpack-plugin');
const WebpackUglifyParallel = require('webpack-uglify-parallel');
const paths = require('./paths');
const getClientEnvironment = require('./env');
const os = require('os');

// Webpack uses `publicPath` to determine where the app is being served from.
// It requires a trailing slash, or the file assets will get an incorrect path.
Expand Down Expand Up @@ -171,14 +175,19 @@ module.exports = {
{
test: /\.(js|jsx)$/,
include: paths.appSrc,
loader: require.resolve('babel-loader'),
options: {
// @remove-on-eject-begin
babelrc: false,
presets: [require.resolve('babel-preset-react-app')],
// @remove-on-eject-end
compact: true,
},
use: [
require.resolve('cache-loader'),
{
loader: require.resolve('babel-loader'),
options: {
// @remove-on-eject-begin
babelrc: false,
presets: [require.resolve('babel-preset-react-app')],
// @remove-on-eject-end
compact: true,
},
},
],
},
// The notation here is somewhat confusing.
// "postcss" loader applies autoprefixer to our CSS.
Expand All @@ -199,6 +208,8 @@ module.exports = {
{
fallback: require.resolve('style-loader'),
use: [
require.resolve('cache-loader'),

{
loader: require.resolve('css-loader'),
options: {
Expand Down Expand Up @@ -279,13 +290,23 @@ module.exports = {
minifyURLs: true,
},
}),
new AutoDllWebpackPlugin({
env: process.env.NODE_ENV,
additionalHash: createHashFromPaths({
paths: [paths.appNodeModules],
exclude: [path.join(paths.appNodeModules, '.cache')],
}),
inject: true,
filename: '[name].[hash].js',
}),
// Makes some environment variables available to the JS code, for example:
// if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
// It is absolutely essential that NODE_ENV was set to production here.
// Otherwise React will be compiled in the very slow development mode.
new webpack.DefinePlugin(env.stringified),
// Minify the code.
new webpack.optimize.UglifyJsPlugin({
new WebpackUglifyParallel({
workers: os.cpus().length,
compress: {
warnings: false,
// Disabled because of an issue with Uglify breaking seemingly valid code:
Expand Down
3 changes: 3 additions & 0 deletions packages/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
"react-scripts": "./bin/react-scripts.js"
},
"dependencies": {
"autodll-webpack-plugin": "^0.2.1",
"autoprefixer": "7.1.1",
"babel-core": "6.25.0",
"babel-eslint": "7.2.3",
"babel-jest": "20.0.3",
"babel-loader": "7.0.0",
"babel-preset-react-app": "^3.0.1",
"babel-runtime": "6.23.0",
"cache-loader": "^1.0.3",
"case-sensitive-paths-webpack-plugin": "2.1.1",
"chalk": "1.1.3",
"css-loader": "0.28.4",
Expand Down Expand Up @@ -56,6 +58,7 @@
"webpack": "2.6.1",
"webpack-dev-server": "2.5.0",
"webpack-manifest-plugin": "1.1.0",
"webpack-uglify-parallel": "^0.1.3",
"whatwg-fetch": "2.0.3"
},
"devDependencies": {
Expand Down

0 comments on commit 655a479

Please sign in to comment.