Skip to content

Commit a71f633

Browse files
gaearonfeiqitian
authored andcommitted
Eject .babelrc instead of separate configs (facebook#705)
Fixes facebook#410, facebook#674
1 parent c1053b8 commit a71f633

10 files changed

+39
-58
lines changed

packages/react-scripts/.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["react-app"]
3+
}

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

-27
This file was deleted.

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

-18
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
// @remove-on-eject-begin
21
/**
32
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
43
*
54
* This source code is licensed under the BSD-style license found in the
65
* LICENSE file in the root directory of this source tree. An additional grant
76
* of patent rights can be found in the PATENTS file in the same directory.
87
*/
9-
// @remove-on-eject-end
108

11-
const babelDev = require('../babel.dev');
129
const babelJest = require('babel-jest');
1310

14-
module.exports = babelJest.createTransformer(babelDev);
11+
module.exports = babelJest.createTransformer({
12+
presets: [require.resolve('babel-preset-react-app')]
13+
});

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var path = require('path');
1313
var autoprefixer = require('autoprefixer');
1414
var webpack = require('webpack');
15+
var findCacheDir = require('find-cache-dir');
1516
var HtmlWebpackPlugin = require('html-webpack-plugin');
1617
var CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
1718
var InterpolateHtmlPlugin = require('../scripts/utils/InterpolateHtmlPlugin');
@@ -118,7 +119,19 @@ module.exports = {
118119
test: /\.(js|jsx)$/,
119120
include: paths.appSrc,
120121
loader: 'babel',
121-
query: require('./babel.dev')
122+
query: {
123+
// @remove-on-eject-begin
124+
babelrc: false,
125+
presets: [require.resolve('babel-preset-react-app')],
126+
// @remove-on-eject-end
127+
// This is a feature of `babel-loader` for webpack (not Babel itself).
128+
// It enables caching results in ./node_modules/.cache/react-scripts/
129+
// directory for faster rebuilds. We use findCacheDir() because of:
130+
// https://github.com/facebookincubator/create-react-app/issues/483
131+
cacheDirectory: findCacheDir({
132+
name: 'react-scripts'
133+
})
134+
}
122135
},
123136
// "postcss" loader applies autoprefixer to our CSS.
124137
// "css" loader resolves paths in CSS and adds assets as dependencies.

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ module.exports = {
119119
test: /\.(js|jsx)$/,
120120
include: paths.appSrc,
121121
loader: 'babel',
122-
query: require('./babel.prod')
122+
// @remove-on-eject-begin
123+
query: {
124+
babelrc: false,
125+
presets: [require.resolve('babel-preset-react-app')],
126+
},
127+
// @remove-on-eject-end
123128
},
124129
// The notation here is somewhat confusing.
125130
// "postcss" loader applies autoprefixer to our CSS.

packages/react-scripts/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"url": "https://github.com/facebookincubator/create-react-app/issues"
1212
},
1313
"files": [
14+
".babelrc",
1415
".eslintrc",
1516
"bin",
1617
"config",

packages/react-scripts/scripts/eject.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ prompt(
2929
var ownPath = path.join(__dirname, '..');
3030
var appPath = path.join(ownPath, '..', '..');
3131
var files = [
32+
'.babelrc',
3233
'.eslintrc',
33-
path.join('config', 'babel.dev.js'),
34-
path.join('config', 'babel.prod.js'),
3534
path.join('config', 'flow', 'css.js.flow'),
3635
path.join('config', 'flow', 'file.js.flow'),
3736
path.join('config', 'paths.js'),
@@ -40,7 +39,6 @@ prompt(
4039
path.join('config', 'webpack.config.prod.js'),
4140
path.join('config', 'jest', 'CSSStub.js'),
4241
path.join('config', 'jest', 'FileStub.js'),
43-
path.join('config', 'jest', 'transform.js'),
4442
path.join('scripts', 'build.js'),
4543
path.join('scripts', 'start.js'),
4644
path.join('scripts', 'utils', 'checkRequiredFiles.js'),
@@ -109,7 +107,9 @@ prompt(
109107

110108
// Add Jest config
111109
appPackage.jest = createJestConfig(
112-
filePath => path.join('<rootDir>', filePath)
110+
filePath => path.join('<rootDir>', filePath),
111+
null,
112+
true
113113
);
114114

115115
console.log('Writing package.json');

packages/react-scripts/scripts/test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ if (!process.env.CI) {
2424

2525
argv.push('--config', JSON.stringify(createJestConfig(
2626
relativePath => path.resolve(__dirname, '..', relativePath),
27-
path.resolve(paths.appSrc, '..')
27+
path.resolve(paths.appSrc, '..'),
28+
false
2829
)));
2930

3031
jest.run(argv);

packages/react-scripts/scripts/utils/createJestConfig.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
const pathExists = require('path-exists');
1313
const paths = require('../../config/paths');
1414

15-
module.exports = (resolve, rootDir) => {
15+
module.exports = (resolve, rootDir, isEjecting) => {
1616
const setupFiles = [resolve('config/polyfills.js')];
1717
if (pathExists.sync(paths.testsSetup)) {
1818
// Use this instead of `paths.testsSetup` to avoid putting
@@ -26,7 +26,6 @@ module.exports = (resolve, rootDir) => {
2626
'^.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': resolve('config/jest/FileStub.js'),
2727
'^.+\\.css$': resolve('config/jest/CSSStub.js')
2828
},
29-
scriptPreprocessor: resolve('config/jest/transform.js'),
3029
setupFiles: setupFiles,
3130
testPathIgnorePatterns: ['<rootDir>/(build|docs|node_modules)/'],
3231
testEnvironment: 'node',
@@ -35,5 +34,10 @@ module.exports = (resolve, rootDir) => {
3534
if (rootDir) {
3635
config.rootDir = rootDir;
3736
}
37+
if (!isEjecting) {
38+
// This is unnecessary after ejecting because Jest
39+
// will just use .babelrc in the project folder.
40+
config.scriptPreprocessor = resolve('config/jest/transform.js');
41+
}
3842
return config;
3943
};

0 commit comments

Comments
 (0)