Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent the cache of files using Babel Macros #5078

Merged
merged 7 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"es6": true
},
"parserOptions": {
"ecmaVersion": 6
"ecmaVersion": 2018
},
"rules": {
"no-console": "off",
Expand Down
12 changes: 12 additions & 0 deletions packages/babel-preset-react-app/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

const loader = require('babel-loader');
const overrides = require('./overrides');

module.exports = loader.custom(() => overrides);
32 changes: 32 additions & 0 deletions packages/babel-preset-react-app/overrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

const crypto = require('crypto');

module.exports = {
// This function transforms the Babel configuration on a per-file basis
config(config, { source }) {
// Babel Macros are notoriously hard to cache, so they shouldn't be
// https://github.com/babel/babel/issues/8497
// We naively detect macros using their package suffix and insert a random
// caller name, a valid option accepted by Babel, to compose a one-time
// cacheIdentifier for the file. We cannot tune the loader options on a per
// file basis.
if (source.indexOf('.macro') !== -1 || source.indexOf('/macro') !== -1) {
return {
...config.options,
caller: {
name: `babel-preset-react-app:${crypto
.randomBytes(32)
.toString('hex')}`,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why you're using a random value, but there's no need to append it to the name. You can just make a new property like craInvalidationToken or something on caller.

},
};
}
return config.options;
},
};
5 changes: 4 additions & 1 deletion packages/babel-preset-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
"url": "https://github.com/facebook/create-react-app/issues"
},
"files": [
"index.js",
"create.js",
"dependencies.js",
"dev.js",
"index.js",
"loader.js",
"overrides.js",
"prod.js",
"test.js"
],
Expand All @@ -29,6 +31,7 @@
"@babel/preset-env": "7.1.0",
"@babel/preset-flow": "7.0.0",
"@babel/preset-react": "7.0.0",
"babel-loader": "8.0.2",
"babel-plugin-macros": "2.4.2",
"babel-plugin-transform-dynamic-import": "2.1.0",
"babel-plugin-transform-react-remove-prop-types": "0.4.18"
Expand Down
5 changes: 4 additions & 1 deletion packages/react-scripts/config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ module.exports = {
},
},
{
loader: require.resolve('babel-loader'),
// We need to use our own loader until `babel-loader` supports
// customization
// https://github.com/babel/babel-loader/pull/687
loader: require.resolve('babel-preset-react-app/loader'),
options: {
// @remove-on-eject-begin
babelrc: false,
Expand Down
5 changes: 4 additions & 1 deletion packages/react-scripts/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,10 @@ module.exports = {
// improves compile time on larger projects
require.resolve('thread-loader'),
{
loader: require.resolve('babel-loader'),
// We need to use our own loader until `babel-loader` supports
// customization
// https://github.com/babel/babel-loader/pull/687
loader: require.resolve('babel-preset-react-app/loader'),
options: {
// @remove-on-eject-begin
babelrc: false,
Expand Down