Skip to content

Commit 11737bc

Browse files
authored
Prevent the cache of files using Babel Macros (facebook#5078)
* Add new overrides option * Add file to package.json * Create our own loader * Remove overrides * We have to use a real babel option * Add comments
1 parent 9cff39e commit 11737bc

File tree

6 files changed

+57
-4
lines changed

6 files changed

+57
-4
lines changed

.eslintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"es6": true
88
},
99
"parserOptions": {
10-
"ecmaVersion": 6
10+
"ecmaVersion": 2018
1111
},
1212
"rules": {
1313
"no-console": "off",
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
'use strict';
8+
9+
const loader = require('babel-loader');
10+
const overrides = require('./overrides');
11+
12+
module.exports = loader.custom(() => overrides);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
'use strict';
8+
9+
const crypto = require('crypto');
10+
11+
module.exports = {
12+
// This function transforms the Babel configuration on a per-file basis
13+
config(config, { source }) {
14+
// Babel Macros are notoriously hard to cache, so they shouldn't be
15+
// https://github.com/babel/babel/issues/8497
16+
// We naively detect macros using their package suffix and insert a random
17+
// caller name, a valid option accepted by Babel, to compose a one-time
18+
// cacheIdentifier for the file. We cannot tune the loader options on a per
19+
// file basis.
20+
if (source.indexOf('.macro') !== -1 || source.indexOf('/macro') !== -1) {
21+
return {
22+
...config.options,
23+
caller: {
24+
name: `babel-preset-react-app:${crypto
25+
.randomBytes(32)
26+
.toString('hex')}`,
27+
},
28+
};
29+
}
30+
return config.options;
31+
},
32+
};

packages/babel-preset-react-app/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
"url": "https://github.com/facebook/create-react-app/issues"
99
},
1010
"files": [
11-
"index.js",
1211
"create.js",
1312
"dependencies.js",
1413
"dev.js",
14+
"index.js",
15+
"loader.js",
16+
"overrides.js",
1517
"prod.js",
1618
"test.js"
1719
],
@@ -29,6 +31,7 @@
2931
"@babel/preset-env": "7.1.0",
3032
"@babel/preset-flow": "7.0.0",
3133
"@babel/preset-react": "7.0.0",
34+
"babel-loader": "8.0.2",
3235
"babel-plugin-macros": "2.4.2",
3336
"babel-plugin-transform-dynamic-import": "2.1.0",
3437
"babel-plugin-transform-react-remove-prop-types": "0.4.18"

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,10 @@ module.exports = {
228228
},
229229
},
230230
{
231-
loader: require.resolve('babel-loader'),
231+
// We need to use our own loader until `babel-loader` supports
232+
// customization
233+
// https://github.com/babel/babel-loader/pull/687
234+
loader: require.resolve('babel-preset-react-app/loader'),
232235
options: {
233236
// @remove-on-eject-begin
234237
babelrc: false,

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,10 @@ module.exports = {
266266
// improves compile time on larger projects
267267
require.resolve('thread-loader'),
268268
{
269-
loader: require.resolve('babel-loader'),
269+
// We need to use our own loader until `babel-loader` supports
270+
// customization
271+
// https://github.com/babel/babel-loader/pull/687
272+
loader: require.resolve('babel-preset-react-app/loader'),
270273
options: {
271274
// @remove-on-eject-begin
272275
babelrc: false,

0 commit comments

Comments
 (0)