Skip to content

Commit 0cfe758

Browse files
authored
Ensure files get purged when they should (#5054)
1 parent 74c4bae commit 0cfe758

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
module.exports = function getCacheIdentifier(environment, packages) {
11+
let cacheIdentifier = `${environment}`;
12+
for (const packageName of packages) {
13+
cacheIdentifier += `:${packageName}@`;
14+
try {
15+
cacheIdentifier += require(`${packageName}/package.json`).version;
16+
} catch (_) {
17+
// ignored
18+
}
19+
}
20+
return cacheIdentifier;
21+
};

packages/react-dev-utils/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"evalSourceMapMiddleware.js",
2121
"FileSizeReporter.js",
2222
"formatWebpackMessages.js",
23+
"getCacheIdentifier.js",
2324
"getCSSModuleLocalIdent.js",
2425
"getProcessForPort.js",
2526
"ignoredFiles.js",

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

+20
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent')
2020
const getClientEnvironment = require('./env');
2121
const paths = require('./paths');
2222
const ManifestPlugin = require('webpack-manifest-plugin');
23+
const getCacheIdentifier = require('react-dev-utils/getCacheIdentifier');
2324

2425
// Webpack uses `publicPath` to determine where the app is being served from.
2526
// In development, we always serve from the root. This makes config easier.
@@ -232,6 +233,17 @@ module.exports = {
232233
// @remove-on-eject-begin
233234
babelrc: false,
234235
presets: [require.resolve('babel-preset-react-app')],
236+
// Make sure we have a unique cache identifier, erring on the
237+
// side of caution.
238+
// We remove this when the user ejects because the default
239+
// is sane and uses Babel options. Instead of options, we use
240+
// the react-scripts and babel-preset-react-app versions.
241+
cacheIdentifier: getCacheIdentifier('development', [
242+
'babel-plugin-named-asset-import',
243+
'babel-preset-react-app',
244+
'react-dev-utils',
245+
'react-scripts',
246+
]),
235247
// @remove-on-eject-end
236248
plugins: [
237249
[
@@ -280,6 +292,14 @@ module.exports = {
280292
cacheDirectory: true,
281293
// Don't waste time on Gzipping the cache
282294
cacheCompression: false,
295+
// @remove-on-eject-begin
296+
cacheIdentifier: getCacheIdentifier('development', [
297+
'babel-plugin-named-asset-import',
298+
'babel-preset-react-app',
299+
'react-dev-utils',
300+
'react-scripts',
301+
]),
302+
// @remove-on-eject-end
283303
highlightCode: true,
284304
},
285305
},

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

+20
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
2323
const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');
2424
const paths = require('./paths');
2525
const getClientEnvironment = require('./env');
26+
const getCacheIdentifier = require('react-dev-utils/getCacheIdentifier');
2627

2728
// Webpack uses `publicPath` to determine where the app is being served from.
2829
// It requires a trailing slash, or the file assets will get an incorrect path.
@@ -269,6 +270,17 @@ module.exports = {
269270
// @remove-on-eject-begin
270271
babelrc: false,
271272
presets: [require.resolve('babel-preset-react-app')],
273+
// Make sure we have a unique cache identifier, erring on the
274+
// side of caution.
275+
// We remove this when the user ejects because the default
276+
// is sane and uses Babel options. Instead of options, we use
277+
// the react-scripts and babel-preset-react-app versions.
278+
cacheIdentifier: getCacheIdentifier('production', [
279+
'babel-plugin-named-asset-import',
280+
'babel-preset-react-app',
281+
'react-dev-utils',
282+
'react-scripts',
283+
]),
272284
// @remove-on-eject-end
273285
plugins: [
274286
[
@@ -310,6 +322,14 @@ module.exports = {
310322
cacheDirectory: true,
311323
// Save disk space when time isn't as important
312324
cacheCompression: true,
325+
// @remove-on-eject-begin
326+
cacheIdentifier: getCacheIdentifier('production', [
327+
'babel-plugin-named-asset-import',
328+
'babel-preset-react-app',
329+
'react-dev-utils',
330+
'react-scripts',
331+
]),
332+
// @remove-on-eject-end
313333
highlightCode: true,
314334
},
315335
},

0 commit comments

Comments
 (0)