Skip to content

Commit 44d0f7f

Browse files
committed
simplify implementation and let user manage included dependencies
1 parent 5e55538 commit 44d0f7f

File tree

11 files changed

+82
-90
lines changed

11 files changed

+82
-90
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
1717
var WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
1818
var getClientEnvironment = require('./env');
1919
var paths = require('./paths');
20-
var vendorManifestId = require('../utils/vendorManifestId')
20+
var vendorHash = require('../utils/vendorHash')
2121
// @remove-on-eject-begin
2222
// `path` is not used after eject - see https://github.com/facebookincubator/create-react-app/issues/1174
2323
var path = require('path');
@@ -211,7 +211,7 @@ module.exports = {
211211
plugins: [
212212
new webpack.DllReferencePlugin({
213213
context: '.',
214-
manifest: require(path.join(paths.vendorPath, 'manifest.' + vendorManifestId + '.json'))
214+
manifest: require(path.join(paths.vendorPath, vendorHash + '.json'))
215215
}),
216216
// Makes some environment variables available in index.html.
217217
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var HtmlWebpackPlugin = require('html-webpack-plugin');
1515
var ExtractTextPlugin = require('extract-text-webpack-plugin');
1616
var ManifestPlugin = require('webpack-manifest-plugin');
1717
var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
18-
var vendorManifestId = require('../utils/vendorManifestId');
18+
var vendorHash = require('../utils/vendorHash');
1919
var paths = require('./paths');
2020
var getClientEnvironment = require('./env');
2121

@@ -225,7 +225,7 @@ module.exports = {
225225
plugins: [
226226
new webpack.DllReferencePlugin({
227227
context: '.',
228-
manifest: require(path.join(paths.vendorPath, 'manifest.' + vendorManifestId + '.json'))
228+
manifest: require(path.join(paths.vendorPath, vendorHash + '.json'))
229229
}),
230230
// Makes some environment variables available in index.html.
231231
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:

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

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,23 @@ var paths = require('../config/paths');
33
var path = require('path');
44
var packageJson = require(paths.appPackageJson);
55
var environment = process.env.NODE_ENV;
6-
var vendorManifestId = require('../utils/vendorManifestId');
6+
var vendorHash = require('../utils/vendorHash');
77
var dependencies = packageJson.dependencies;
88
var getClientEnvironment = require('./env');
99
var publicPath = paths.servedPath;
1010
var publicUrl = publicPath.slice(0, -1);
1111
var env = getClientEnvironment(publicUrl);
1212
var fs = require('fs');
13-
var reactScriptsPath = path.resolve('node_modules/react-scripts');
14-
var reactScriptsLinked = fs.existsSync(reactScriptsPath) &&
15-
fs.lstatSync(reactScriptsPath).isSymbolicLink();
16-
17-
// config before publish: we're in ./packages/react-scripts/config/
18-
if (
19-
!reactScriptsLinked &&
20-
__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
21-
) {
22-
dependencies = packageJson.devDependencies;
23-
}
24-
25-
console.log('Current dependencies :');
26-
Object.keys(dependencies).forEach(dependency => console.log(dependency));
27-
13+
var vendorGlobalName = '[name]' + vendorHash.replace(/\./g,'')
2814
module.exports = {
15+
cache: true,
2916
entry: {
30-
vendor: Object.keys(dependencies)
17+
vendor: [paths.vendorPath]
3118
},
3219
output: {
33-
filename: '[name].' + vendorManifestId + '.js',
20+
filename: vendorHash + '.js',
3421
path: paths.vendorPath,
35-
library: '[name]_' + vendorManifestId
22+
library: vendorGlobalName
3623
},
3724
resolve: {
3825
// This allows you to set a fallback for where Webpack should look for modules.
@@ -70,12 +57,12 @@ module.exports = {
7057
// within that bundle
7158
path: path.join(
7259
paths.vendorPath,
73-
'manifest.' + vendorManifestId + '.json'
60+
vendorHash + '.json'
7461
),
7562
// The name of the global variable which the library's
7663
// require function has been assigned to. This must match the
7764
// output.library option above
78-
name: '[name]_' + vendorManifestId
65+
name: vendorGlobalName
7966
}),
8067
environment === 'production'
8168
? new webpack.DefinePlugin(env.stringified)

packages/react-scripts/scripts/eject.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ prompt(
6363
path.join('utils', 'bundleVendorIfStale.js'),
6464
path.join('utils', 'packageJsonMd5.js'),
6565
path.join('utils', 'printErrors.js'),
66-
path.join('utils', 'vendorManifestId.js'),
66+
path.join('utils', 'vendorHash.js'),
6767
];
6868

6969
// Ensure that the app folder is clean and we won't override any files

packages/react-scripts/scripts/start.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ var getProcessForPort = require('react-dev-utils/getProcessForPort');
3030
var openBrowser = require('react-dev-utils/openBrowser');
3131
var prompt = require('react-dev-utils/prompt');
3232
var fs = require('fs');
33-
var config = require('../config/webpack.config.dev');
3433
var paths = require('../config/paths');
34+
var bundleVendorIfStale = require('../utils/bundleVendorIfStale')
3535

3636
var useYarn = fs.existsSync(paths.yarnLockFile);
3737
var cli = useYarn ? 'yarn' : 'npm';
@@ -41,7 +41,8 @@ var isInteractive = process.stdout.isTTY;
4141
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
4242
process.exit(1);
4343
}
44-
44+
bundleVendorIfStale(()=>{
45+
var config = require('../config/webpack.config.dev');
4546
// Tools like Cloud9 rely on this.
4647
var DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
4748
var compiler;
@@ -334,3 +335,4 @@ detect(DEFAULT_PORT).then(port => {
334335
console.log(chalk.red('Something is already running on port ' + DEFAULT_PORT + '.'));
335336
}
336337
});
338+
})

packages/react-scripts/template/public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
To begin the development, run `npm start`.
2828
To create a production bundle, use `npm run build`.
2929
-->
30-
<script src="vendor/vendor.%REACT_APP_VENDOR_MANIFEST_ID%.js"></script>
30+
<script src="vendor/%REACT_APP_VENDOR_HASH%.js"></script>
3131
</body>
3232
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require("react");
2+
require("react-dom");
3+
require("lodash");

packages/react-scripts/utils/bundleVendorIfStale.js

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,77 @@
1-
var fs = require('fs-extra');
2-
var path = require('path');
3-
var webpack = require('webpack');
4-
var paths = require('../config/paths');
5-
var config = require('../config/webpack.config.vendor');
6-
var clearConsole = require('react-dev-utils/clearConsole');
7-
var os = require('os');
8-
var chalk = require('chalk');
9-
var printErrors = require('../utils/printErrors');
1+
var fs = require("fs-extra");
2+
var path = require("path");
3+
var webpack = require("webpack");
4+
var paths = require("../config/paths");
5+
var config = require("../config/webpack.config.vendor");
6+
var clearConsole = require("react-dev-utils/clearConsole");
7+
var os = require("os");
8+
var chalk = require("chalk");
9+
var printErrors = require("../utils/printErrors");
1010
var environment = process.env.NODE_ENV;
11-
var hashFilePrefix = 'REACT_APP_PACKAGE_JSON_MD5_';
12-
var vendorManifestId = require('../utils/vendorManifestId');
11+
var vendorHash = require("../utils/vendorHash");
1312

1413
module.exports = (callback, args) => {
1514
if (shouldManifestUpdate()) {
16-
fs.emptyDirSync(paths.vendorPath);
17-
var compiler = webpack(config);
18-
console.log('Bundling vendor files for faster rebuilds...');
19-
return compiler.run((err, stats) => {
20-
if (err) {
21-
printErrors('Failed to compile.', [err]);
22-
process.exit(1);
15+
// fs.emptyDirSync(paths.vendorPath);
16+
return fs.readdir(paths.vendorPath, (err, files) => {
17+
try {
18+
files.filter(file => !file.indexOf(environment)).forEach(file => {
19+
fs.unlinkSync(path.join(paths.vendorPath, file));
20+
});
21+
} catch (e) {
2322
}
23+
var compiler = webpack(config);
24+
console.log("Bundling vendor files for faster rebuilds...");
25+
compiler.run((err, stats) => {
26+
if (err) {
27+
printErrors("Failed to compile.", [err]);
28+
process.exit(1);
29+
}
2430

25-
if (stats.compilation.errors.length) {
26-
printErrors('Failed to compile.', stats.compilation.errors);
27-
process.exit(1);
28-
}
31+
if (stats.compilation.errors.length) {
32+
printErrors("Failed to compile.", stats.compilation.errors);
33+
process.exit(1);
34+
}
2935

30-
if (process.env.CI && stats.compilation.warnings.length) {
31-
printErrors(
32-
'Failed to compile. When process.env.CI = true, warnings are treated as failures. Most CI servers set this automatically.',
33-
stats.compilation.warnings
34-
);
35-
process.exit(1);
36-
}
36+
if (process.env.CI && stats.compilation.warnings.length) {
37+
printErrors(
38+
"Failed to compile. When process.env.CI = true, warnings are treated as failures. Most CI servers set this automatically.",
39+
stats.compilation.warnings
40+
);
41+
process.exit(1);
42+
}
3743

38-
console.log(chalk.green('Vendor compiled successfully.'));
39-
console.log();
40-
fs.writeFileSync(
41-
path.join(os.tmpdir(), hashFilePrefix + vendorManifestId)
42-
);
43-
callback();
44+
console.log(chalk.green("Vendor compiled successfully."));
45+
console.log();
46+
callback();
47+
});
4448
});
4549
}
46-
console.log('Vendor file is up to date! No need to rebuild it');
50+
console.log("Vendor file is up to date! No need to rebuild it");
4751
return callback();
4852
};
4953

5054
function manifestExists() {
51-
return fs.existsSync(
52-
path.join(paths.vendorPath, 'manifest.' + vendorManifestId + '.json')
53-
);
55+
return fs.existsSync(path.join(paths.vendorPath, vendorHash + ".json"));
5456
}
5557

5658
function manifestStale() {
57-
if (md5Exists(vendorManifestId)) {
59+
if (vendorHash) {
5860
return false;
5961
}
6062
return true;
6163
}
6264

63-
function md5Exists(md5) {
64-
return fs.existsSync(path.join(os.tmpdir(), hashFilePrefix + md5));
65-
}
66-
6765
function shouldManifestUpdate() {
66+
var isExists = manifestExists();
6867
clearConsole();
69-
console.log('Using ' + vendorManifestId + ' vendor build');
70-
if (!manifestExists()) {
71-
console.log('Vendor file needs to be created...');
68+
console.log("Using " + vendorHash + " vendor build");
69+
if (!isExists) {
70+
console.log("Vendor file needs to be created...");
7271
return true;
7372
}
74-
if (manifestExists() && manifestStale()) {
75-
console.log('Vendor file needs to be updated...');
73+
if (isExists && manifestStale()) {
74+
console.log("Vendor file needs to be updated...");
7675
return true;
7776
}
7877
return false;

packages/react-scripts/utils/packageJsonMd5.js

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var crypto = require('crypto');
2+
var fs = require('fs');
3+
var path = require('path')
4+
var paths = require('../config/paths');
5+
var hash = crypto.createHash('md5');
6+
var input = fs.readFileSync(path.join(paths.vendorPath, 'index.js'));
7+
hash.update(input);
8+
9+
var hashed = hash.digest('hex');
10+
module.exports =
11+
process.env.REACT_APP_VENDOR_HASH =
12+
[process.env.NODE_ENV, hashed].join('.');
13+

0 commit comments

Comments
 (0)