Skip to content

Commit dfecfea

Browse files
authored
Unroll indirection in paths (#191)
1 parent d69ee27 commit dfecfea

File tree

7 files changed

+134
-102
lines changed

7 files changed

+134
-102
lines changed

config/paths.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
// TODO: we can split this file into several files (pre-eject, post-eject, test)
11+
// and use those instead. This way we don't need to branch here.
12+
13+
var path = require('path');
14+
15+
// True when used as a dependency, false after ejecting
16+
var isInNodeModules = (
17+
'node_modules' ===
18+
path.basename(path.resolve(path.join(__dirname, '..', '..')))
19+
);
20+
21+
// Are we developing create-react-app locally?
22+
var isInCreateReactAppSource = (
23+
process.argv.some(arg => arg.indexOf('--debug-template') > -1)
24+
);
25+
26+
function resolve(relativePath) {
27+
return path.resolve(__dirname, relativePath);
28+
}
29+
30+
if (isInCreateReactAppSource) {
31+
// create-react-app development: we're in ./config/
32+
module.exports = {
33+
appBuild: resolve('../build'),
34+
appHtml: resolve('../template/index.html'),
35+
appFavicon: resolve('../template/favicon.ico'),
36+
appPackageJson: resolve('../package.json'),
37+
appSrc: resolve('../template/src'),
38+
appNodeModules: resolve('../node_modules'),
39+
ownNodeModules: resolve('../node_modules')
40+
};
41+
} else if (isInNodeModules) {
42+
// before eject: we're in ./node_modules/react-scripts/config/
43+
module.exports = {
44+
appBuild: resolve('../../../build'),
45+
appHtml: resolve('../../../index.html'),
46+
appFavicon: resolve('../../../favicon.ico'),
47+
appPackageJson: resolve('../../../package.json'),
48+
appSrc: resolve('../../../src'),
49+
appNodeModules: resolve('../..'),
50+
// this is empty with npm3 but node resolution searches higher anyway:
51+
ownNodeModules: resolve('../node_modules')
52+
};
53+
} else {
54+
// after eject: we're in ./config/
55+
module.exports = {
56+
appBuild: resolve('../build'),
57+
appHtml: resolve('../index.html'),
58+
appFavicon: resolve('../favicon.ico'),
59+
appPackageJson: resolve('../package.json'),
60+
appSrc: resolve('../src'),
61+
appNodeModules: resolve('../node_modules'),
62+
ownNodeModules: resolve('../node_modules')
63+
};
64+
}

config/webpack.config.dev.js

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,18 @@ var path = require('path');
1111
var autoprefixer = require('autoprefixer');
1212
var webpack = require('webpack');
1313
var HtmlWebpackPlugin = require('html-webpack-plugin');
14-
15-
// TODO: hide this behind a flag and eliminate dead code on eject.
16-
// This shouldn't be exposed to the user.
17-
var isInNodeModules = 'node_modules' ===
18-
path.basename(path.resolve(path.join(__dirname, '..', '..')));
19-
var relativePath = isInNodeModules ? '../../..' : '..';
20-
var isInDebugMode = process.argv.some(arg =>
21-
arg.indexOf('--debug-template') > -1
22-
);
23-
if (isInDebugMode) {
24-
relativePath = '../template';
25-
}
26-
var srcPath = path.resolve(__dirname, relativePath, 'src');
27-
var rootNodeModulesPath = path.resolve(__dirname, relativePath, 'node_modules');
28-
var nodeModulesPath = path.join(__dirname, '..', 'node_modules');
29-
var indexHtmlPath = path.resolve(__dirname, relativePath, 'index.html');
30-
var faviconPath = path.resolve(__dirname, relativePath, 'favicon.ico');
31-
var buildPath = path.join(__dirname, isInNodeModules ? '../../..' : '..', 'build');
14+
var paths = require('./paths');
3215

3316
module.exports = {
3417
devtool: 'eval',
3518
entry: [
3619
require.resolve('webpack-dev-server/client') + '?http://localhost:3000',
3720
require.resolve('webpack/hot/dev-server'),
38-
path.join(srcPath, 'index')
21+
path.join(paths.appSrc, 'index')
3922
],
4023
output: {
4124
// Next line is not used in dev but WebpackDevServer crashes without it:
42-
path: buildPath,
25+
path: paths.appBuild,
4326
pathinfo: true,
4427
filename: 'bundle.js',
4528
publicPath: '/'
@@ -48,39 +31,42 @@ module.exports = {
4831
extensions: ['', '.js'],
4932
},
5033
resolveLoader: {
51-
root: nodeModulesPath,
34+
root: paths.ownNodeModules,
5235
moduleTemplates: ['*-loader']
5336
},
5437
module: {
5538
preLoaders: [
5639
{
5740
test: /\.js$/,
5841
loader: 'eslint',
59-
include: srcPath,
42+
include: paths.appSrc,
6043
}
6144
],
6245
loaders: [
6346
{
6447
test: /\.js$/,
65-
include: srcPath,
48+
include: paths.appSrc,
6649
loader: 'babel',
6750
query: require('./babel.dev')
6851
},
6952
{
7053
test: /\.css$/,
71-
include: [srcPath, rootNodeModulesPath],
54+
include: [paths.appSrc, paths.appNodeModules],
7255
loader: 'style!css!postcss'
7356
},
7457
{
7558
test: /\.json$/,
59+
include: [paths.appSrc, paths.appNodeModules],
7660
loader: 'json'
7761
},
7862
{
7963
test: /\.(jpg|png|gif|eot|svg|ttf|woff|woff2)$/,
64+
include: [paths.appSrc, paths.appNodeModules],
8065
loader: 'file',
8166
},
8267
{
8368
test: /\.(mp4|webm)$/,
69+
include: [paths.appSrc, paths.appNodeModules],
8470
loader: 'url?limit=10000'
8571
}
8672
]
@@ -95,8 +81,8 @@ module.exports = {
9581
plugins: [
9682
new HtmlWebpackPlugin({
9783
inject: true,
98-
template: indexHtmlPath,
99-
favicon: faviconPath,
84+
template: paths.appHtml,
85+
favicon: paths.appFavicon,
10086
}),
10187
new webpack.DefinePlugin({ 'process.env.NODE_ENV': '"development"' }),
10288
// Note: only CSS is currently hot reloaded

config/webpack.config.prod.js

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,9 @@ 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');
1617

17-
// TODO: hide this behind a flag and eliminate dead code on eject.
18-
// This shouldn't be exposed to the user.
19-
var isInNodeModules = 'node_modules' ===
20-
path.basename(path.resolve(path.join(__dirname, '..', '..')));
21-
var relativePath = isInNodeModules ? '../../..' : '..';
22-
if (process.argv[2] === '--debug-template') {
23-
relativePath = '../template';
24-
}
25-
var srcPath = path.resolve(__dirname, relativePath, 'src');
26-
var rootNodeModulesPath = path.resolve(__dirname, relativePath, 'node_modules');
27-
var nodeModulesPath = path.join(__dirname, '..', 'node_modules');
28-
var indexHtmlPath = path.resolve(__dirname, relativePath, 'index.html');
29-
var faviconPath = path.resolve(__dirname, relativePath, 'favicon.ico');
30-
var buildPath = path.join(__dirname, isInNodeModules ? '../../..' : '..', 'build');
31-
var homepagePath = require(path.resolve(__dirname, relativePath, 'package.json')).homepage;
18+
var homepagePath = require(paths.appPackageJson).homepage;
3219
var publicPath = homepagePath ? url.parse(homepagePath).pathname : '/';
3320
if (!publicPath.endsWith('/')) {
3421
// Prevents incorrect paths in file-loader
@@ -38,9 +25,9 @@ if (!publicPath.endsWith('/')) {
3825
module.exports = {
3926
bail: true,
4027
devtool: 'source-map',
41-
entry: path.join(srcPath, 'index'),
28+
entry: path.join(paths.appSrc, 'index'),
4229
output: {
43-
path: buildPath,
30+
path: paths.appBuild,
4431
filename: '[name].[chunkhash].js',
4532
chunkFilename: '[name].[chunkhash].chunk.js',
4633
publicPath: publicPath
@@ -49,42 +36,45 @@ module.exports = {
4936
extensions: ['', '.js'],
5037
},
5138
resolveLoader: {
52-
root: nodeModulesPath,
39+
root: paths.ownNodeModules,
5340
moduleTemplates: ['*-loader']
5441
},
5542
module: {
5643
preLoaders: [
5744
{
5845
test: /\.js$/,
5946
loader: 'eslint',
60-
include: srcPath
47+
include: paths.appSrc
6148
}
6249
],
6350
loaders: [
6451
{
6552
test: /\.js$/,
66-
include: srcPath,
53+
include: paths.appSrc,
6754
loader: 'babel',
6855
query: require('./babel.prod')
6956
},
7057
{
7158
test: /\.css$/,
72-
include: [srcPath, rootNodeModulesPath],
59+
include: [paths.appSrc, paths.appNodeModules],
7360
// Disable autoprefixer in css-loader itself:
7461
// https://github.com/webpack/css-loader/issues/281
7562
// We already have it thanks to postcss.
7663
loader: ExtractTextPlugin.extract('style', 'css?-autoprefixer!postcss')
7764
},
7865
{
7966
test: /\.json$/,
67+
include: [paths.appSrc, paths.appNodeModules],
8068
loader: 'json'
8169
},
8270
{
8371
test: /\.(jpg|png|gif|eot|svg|ttf|woff|woff2)$/,
72+
include: [paths.appSrc, paths.appNodeModules],
8473
loader: 'file',
8574
},
8675
{
8776
test: /\.(mp4|webm)$/,
77+
include: [paths.appSrc, paths.appNodeModules],
8878
loader: 'url?limit=10000'
8979
}
9080
]
@@ -101,8 +91,8 @@ module.exports = {
10191
plugins: [
10292
new HtmlWebpackPlugin({
10393
inject: true,
104-
template: indexHtmlPath,
105-
favicon: faviconPath,
94+
template: paths.appHtml,
95+
favicon: paths.appFavicon,
10696
minify: {
10797
removeComments: true,
10898
collapseWhitespace: true,

scripts/build.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,12 @@
99

1010
process.env.NODE_ENV = 'production';
1111

12-
var path = require('path');
1312
var rimrafSync = require('rimraf').sync;
1413
var webpack = require('webpack');
1514
var config = require('../config/webpack.config.prod');
15+
var paths = require('../config/paths');
1616

17-
var isInNodeModules = 'node_modules' ===
18-
path.basename(path.resolve(path.join(__dirname, '..', '..')));
19-
var relative = isInNodeModules ? '../../..' : '..';
20-
if (process.argv[2] === '--debug-template') {
21-
relative = '../template';
22-
}
23-
var packageJsonPath = path.join(__dirname, relative, 'package.json');
24-
var buildPath = path.join(__dirname, relative, 'build');
25-
rimrafSync(buildPath);
17+
rimrafSync(paths.appBuild);
2618

2719
webpack(config).run(function(err, stats) {
2820
if (err) {
@@ -32,7 +24,7 @@ webpack(config).run(function(err, stats) {
3224
}
3325

3426
var openCommand = process.platform === 'win32' ? 'start' : 'open';
35-
var homepagePath = require(packageJsonPath).homepage;
27+
var homepagePath = require(paths.appPackageJson).homepage;
3628
console.log('Successfully generated a bundle in the build folder!');
3729
if (homepagePath) {
3830
console.log('You can now deploy it to ' + homepagePath + '.');

0 commit comments

Comments
 (0)