Skip to content

Commit 4ff112b

Browse files
committed
separate dev config and prod config, cleanup code on eject, ship prod config only on release and dev-pack
1 parent d071aa0 commit 4ff112b

16 files changed

+170
-84
lines changed

config/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
module.exports = function(name, stage) {
11+
if(stage){
12+
return require('./'+name+'.'+stage);
13+
}
14+
15+
var isInCreateReactAppSource = (
16+
process.argv.some(arg => arg.indexOf('--debug-template') > -1)
17+
);
18+
if (isInCreateReactAppSource) {
19+
return require('./'+name+'.dev');
20+
}else {
21+
return require('./'+name+'.prod');
22+
}
23+
}

config/index.prod.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
module.exports = function(name) {
11+
return require('./'+name+'.prod');
12+
}

config/paths.dev.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
var path = require('path');
11+
12+
function resolve(relativePath) {
13+
return path.resolve(__dirname, relativePath);
14+
}
15+
16+
// create-react-app development: we're in ./config/
17+
module.exports = {
18+
appBuild: resolve('../build'),
19+
appHtml: resolve('../template/index.html'),
20+
appFavicon: resolve('../template/favicon.ico'),
21+
appPackageJson: resolve('../package.json'),
22+
appSrc: resolve('../template/src'),
23+
appNodeModules: resolve('../node_modules'),
24+
ownNodeModules: resolve('../node_modules')
25+
};

config/paths.js

Lines changed: 0 additions & 64 deletions
This file was deleted.

config/paths.prod.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
var path = require('path');
11+
12+
function resolve(relativePath) {
13+
return path.resolve(__dirname, relativePath);
14+
}
15+
16+
module.exports = {
17+
appBuild: resolve('../build'),
18+
appHtml: resolve('../index.html'),
19+
appFavicon: resolve('../favicon.ico'),
20+
appPackageJson: resolve('../package.json'),
21+
appSrc: resolve('../src'),
22+
appNodeModules: resolve('../node_modules'),
23+
ownNodeModules: resolve('../node_modules')
24+
};
25+
26+
// Dead code on eject: start
27+
// before eject: we're in ./node_modules/react-scripts/config/
28+
module.exports = {
29+
appBuild: resolve('../../../build'),
30+
appHtml: resolve('../../../index.html'),
31+
appFavicon: resolve('../../../favicon.ico'),
32+
appPackageJson: resolve('../../../package.json'),
33+
appSrc: resolve('../../../src'),
34+
appNodeModules: resolve('../..'),
35+
// this is empty with npm3 but node resolution searches higher anyway:
36+
ownNodeModules: resolve('../node_modules')
37+
};
38+
// Dead code on eject: end

config/webpack.config.dev.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var path = require('path');
1111
var autoprefixer = require('autoprefixer');
1212
var webpack = require('webpack');
1313
var HtmlWebpackPlugin = require('html-webpack-plugin');
14-
var paths = require('./paths');
14+
var paths = require('./')('paths');
1515

1616
module.exports = {
1717
devtool: 'eval',
@@ -48,7 +48,7 @@ module.exports = {
4848
test: /\.js$/,
4949
include: paths.appSrc,
5050
loader: 'babel',
51-
query: require('./babel.dev')
51+
query: require('./')('babel')
5252
},
5353
{
5454
test: /\.css$/,

config/webpack.config.prod.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var webpack = require('webpack');
1313
var HtmlWebpackPlugin = require('html-webpack-plugin');
1414
var ExtractTextPlugin = require('extract-text-webpack-plugin');
1515
var url = require('url');
16-
var paths = require('./paths');
16+
var paths = require('./')('paths');
1717

1818
var homepagePath = require(paths.appPackageJson).homepage;
1919
var publicPath = homepagePath ? url.parse(homepagePath).pathname : '/';
@@ -55,7 +55,7 @@ module.exports = {
5555
test: /\.js$/,
5656
include: paths.appSrc,
5757
loader: 'babel',
58-
query: require('./babel.prod')
58+
query: require('./')('babel')
5959
},
6060
{
6161
test: /\.css$/,

global-cli/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var argv = require('minimist')(process.argv.slice(2));
5050
* Example of valid values:
5151
* - a specific npm version: "0.22.0-rc1"
5252
* - a .tgz archive from any npm repo: "https://registry.npmjs.org/react-scripts/-/react-scripts-0.20.0.tgz"
53-
* - a package prepared with `npm pack`: "/Users/home/vjeux/create-react-app/react-scripts-0.22.0.tgz"
53+
* - a package prepared with `tasks/pack.sh`: "/Users/home/vjeux/create-react-app/react-scripts-0.22.0.tgz"
5454
*/
5555
var commands = argv._;
5656
if (commands.length === 0) {

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@
1313
"scripts": {
1414
"start": "node scripts/start.js --debug-template",
1515
"build": "node scripts/build.js --debug-template",
16-
"create-react-app": "node global-cli/index.js --scripts-version \"$PWD/`npm pack`\"",
16+
"create-react-app": "node global-cli/index.js --scripts-version \"$PWD/`tasks/pack.sh`\"",
1717
"test": "tasks/e2e.sh"
1818
},
1919
"files": [
2020
"LICENSE",
2121
"PATENTS",
2222
"bin",
23-
"config",
23+
"config/flow",
24+
"config/*.prod.js",
25+
"!config/index.prod.js",
26+
"config/eslint.js",
27+
"config/index.js",
28+
"config/polyfills.js",
2429
"scripts",
2530
"template"
2631
],

scripts/build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ var filesize = require('filesize');
1414
var gzipSize = require('gzip-size');
1515
var rimrafSync = require('rimraf').sync;
1616
var webpack = require('webpack');
17-
var config = require('../config/webpack.config.prod');
18-
var paths = require('../config/paths');
17+
var config = require('../config')('webpack.config', 'prod');
18+
var paths = require('../config')('paths');
1919

2020
// Remove all content but keep the directory so that
2121
// if you're in it, you don't end up in Trash

scripts/eject.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@ prompt(
2828
var ownPath = path.join(__dirname, '..');
2929
var appPath = path.join(ownPath, '..', '..');
3030
var files = [
31-
path.join('config', 'babel.dev.js'),
3231
path.join('config', 'babel.prod.js'),
3332
path.join('config', 'flow', 'css.js.flow'),
3433
path.join('config', 'flow', 'file.js.flow'),
3534
path.join('config', 'eslint.js'),
3635
path.join('config', 'paths.js'),
3736
path.join('config', 'polyfills.js'),
38-
path.join('config', 'webpack.config.dev.js'),
3937
path.join('config', 'webpack.config.prod.js'),
4038
path.join('scripts', 'build.js'),
4139
path.join('scripts', 'start.js'),
@@ -70,6 +68,8 @@ prompt(
7068
.replace(/^\/\*\*(\*(?!\/)|[^*])*\*\//, '')
7169
// Remove license header from AppleScript
7270
.replace(/^--.*\n/gm, '')
71+
// Remove dead code on eject
72+
.replace(/\/\/ Dead code on eject: start([\s\S]*?)\/\/ Dead code on eject: end/g, '')
7373
.trim() + '\n';
7474
fs.writeFileSync(path.join(appPath, file), content);
7575
});

scripts/start.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var execSync = require('child_process').execSync;
1717
var opn = require('opn');
1818
var detect = require('detect-port');
1919
var prompt = require('./utils/prompt');
20-
var config = require('../config/webpack.config.dev');
20+
var config = require('../config/')('webpack.config');
2121

2222
// Tools like Cloud9 rely on this
2323
var DEFAULT_PORT = process.env.PORT || 3000;

tasks/clean.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright (c) 2015-present, Facebook, Inc.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree. An additional grant
6+
# of patent rights can be found in the PATENTS file in the same directory.
7+
8+
# Start in tests/ even if run from root directory
9+
cd "$(dirname "$0")"
10+
11+
# Exit the script on any command with non 0 return code
12+
# We assume that all the commands in the pipeline set their return code
13+
# properly and that we do not need to validate that the output is correct
14+
set -e
15+
16+
# Echo every command being executed
17+
set -x
18+
19+
# Go to root
20+
cd ..
21+
22+
rm -rf build
23+
rm -rf global-cli/node_modules
24+
rm -rf global-cli/create-react-app*.tgz
25+
rm -rf node_modules
26+
rm -rf react-scripts*.tgz

tasks/e2e.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ perl -i -p0e 's/bundledDependencies.*?]/bundledDependencies": []/s' package.json
4747

4848
# Pack react-scripts
4949
npm install
50-
scripts_path=$PWD/`npm pack`
50+
scripts_path=$PWD/`tasks/pack.sh`
5151

5252
# lint
5353
./node_modules/.bin/eslint --ignore-path .gitignore ./
@@ -72,31 +72,31 @@ temp_cli_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_cli_path'`
7272
cd $temp_cli_path
7373
npm install $cli_path
7474

75-
# Install the app in a temporary location
75+
echo "Install the app in a temporary location"
7676
temp_app_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_app_path'`
7777
cd $temp_app_path
7878
node "$temp_cli_path"/node_modules/create-react-app/index.js --scripts-version=$scripts_path test-app
7979
cd test-app
8080

81-
# Test the build
81+
echo "Test the build"
8282
npm run build
8383

84-
# Check for expected output
84+
echo "Check for expected output"
8585
test -e build/*.html
8686
test -e build/*.js
8787

88-
# Test the server
88+
echo "Test the server"
8989
npm start -- --smoke-test
9090

91-
# Eject and test the build
91+
echo "Eject and test the build"
9292
echo yes | npm run eject
9393
npm run build
9494

95-
# Check for expected output
95+
echo "After Eject: Check for expected output"
9696
test -e build/*.html
9797
test -e build/*.js
9898

99-
# Test the server
99+
echo "After Eject: Test the server"
100100
npm start -- --smoke-test
101101

102102
# Cleanup

tasks/pack.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
# Create a local package some as the released one,
3+
# which only contains prod-only configurations
4+
5+
# TODO: in the future, we can create stage specific package for e2e test
6+
# i.e. prod dev test alpha etc.
7+
mv ./config/index.prod.js ./config/index.js
8+
9+
cp $PWD/config/index.prod.js $PWD/config/index.js 2>/dev/null
10+
packname=`npm pack`
11+
12+
# Discard changes to config
13+
git checkout -- .
14+
15+
echo $packname

tasks/release.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ rm -rf node_modules/fsevents
4141
# We will revert package.json back after release to avoid doing it every time
4242
node ./node_modules/.bin/bundle-deps
4343

44+
# This modifies ./config/index.js to
45+
# -- hide the --debug-template
46+
# -- only expose production configuration to users
47+
# We will revert ./config/index.js in this development repository after release
48+
cp ./config/index.prod.js ./config/index.js
49+
4450
# Go!
4551
npm publish "$@"
4652

0 commit comments

Comments
 (0)