Skip to content

Commit

Permalink
Remove unnecessary duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
Enoah Netzach committed Nov 25, 2016
1 parent e8b51e5 commit c403dd5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
41 changes: 31 additions & 10 deletions packages/react-scripts/config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

var path = require('path');
var fs = require('fs');
var url = require('url');

// Make sure any symlinks in the project folder are resolved:
// https://github.com/facebookincubator/create-react-app/issues/637
Expand All @@ -35,8 +36,26 @@ var nodePaths = (process.env.NODE_PATH || '')
.filter(Boolean)
.map(resolveApp);

var envPublicUrl = process.env.PUBLIC_URL;

function getPublicUrl(appPackageJson) {
return envPublicUrl ? envPublicUrl : require(appPackageJson).homepage;
}

// We use `PUBLIC_URL` environment variable or "homepage" field to infer
// "public path" at which the app is served.
// Webpack needs to know it to put the right <script> hrefs into HTML even in
// single-page apps that may serve index.html for nested URLs like /todos/42.
// We can't use a relative path in HTML because we don't want to load something
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
function getServedPath(appPackageJson) {
var homepagePath = getPublicUrl(appPackageJson);
var homepagePathname = homepagePath ? url.parse(homepagePath).pathname : '/';
return envPublicUrl ? homepagePath : homepagePathname;
}

// config after eject: we're in ./config/
var configs = {
module.exports = {
appBuild: resolveApp('build'),
appPublic: resolveApp('public'),
appHtml: resolveApp('public/index.html'),
Expand All @@ -47,7 +66,9 @@ var configs = {
testsSetup: resolveApp('src/setupTests.js'),
appNodeModules: resolveApp('node_modules'),
ownNodeModules: resolveApp('node_modules'),
nodePaths: nodePaths
nodePaths: nodePaths,
publicUrl: getPublicUrl(resolveApp('package.json')),
servedPath: getServedPath(resolveApp('package.json'))
};

// @remove-on-eject-begin
Expand All @@ -56,7 +77,7 @@ function resolveOwn(relativePath) {
}

// config before eject: we're in ./node_modules/react-scripts/config/
configs = {
module.exports = {
appBuild: resolveApp('build'),
appPublic: resolveApp('public'),
appHtml: resolveApp('public/index.html'),
Expand All @@ -68,12 +89,14 @@ configs = {
appNodeModules: resolveApp('node_modules'),
// this is empty with npm3 but node resolution searches higher anyway:
ownNodeModules: resolveOwn('../node_modules'),
nodePaths: nodePaths
nodePaths: nodePaths,
publicUrl: getPublicUrl(resolveApp('package.json')),
servedPath: getServedPath(resolveApp('package.json'))
};

// config before publish: we're in ./packages/react-scripts/config/
if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1) {
configs = {
module.exports = {
appBuild: resolveOwn('../../../build'),
appPublic: resolveOwn('../template/public'),
appHtml: resolveOwn('../template/public/index.html'),
Expand All @@ -84,11 +107,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
testsSetup: resolveOwn('../template/src/setupTests.js'),
appNodeModules: resolveOwn('../node_modules'),
ownNodeModules: resolveOwn('../node_modules'),
nodePaths: nodePaths
nodePaths: nodePaths,
publicUrl: getPublicUrl(resolveOwn('../package.json')),
servedPath: getServedPath(resolveOwn('../package.json'))
};
}
// @remove-on-eject-end

configs.publicUrl = process.env.PUBLIC_URL ? process.env.PUBLIC_URL : require(configs.appPackageJson).homepage;

module.exports = configs;
14 changes: 2 additions & 12 deletions packages/react-scripts/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var ManifestPlugin = require('webpack-manifest-plugin');
var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
var url = require('url');
var paths = require('./paths');
var getClientEnvironment = require('./env');

Expand All @@ -31,22 +30,13 @@ function ensureSlash(path, needsSlash) {
}
}

// We use `PUBLIC_URL` environment variable or "homepage" field to infer
// "public path" at which the app is served.
// Webpack needs to know it to put the right <script> hrefs into HTML even in
// single-page apps that may serve index.html for nested URLs like /todos/42.
// We can't use a relative path in HTML because we don't want to load something
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
var homepagePath = paths.publicUrl;
var homepagePathname = homepagePath ? url.parse(homepagePath).pathname : '/';
var servedPath = process.env.PUBLIC_URL ? homepagePath : homepagePathname;
// Webpack uses `publicPath` to determine where the app is being served from.
// It requires a trailing slash, or the file assets will get an incorrect path.
var publicPath = ensureSlash(servedPath, true);
var publicPath = ensureSlash(paths.servedPath, true);
// `publicUrl` is just like `publicPath`, but we will provide it to our app
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
// Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
var publicUrl = ensureSlash(servedPath, false);
var publicUrl = ensureSlash(paths.servedPath, false);
// Get environment variables to inject into our app.
var env = getClientEnvironment(publicUrl);

Expand Down

0 comments on commit c403dd5

Please sign in to comment.