From bb14761051a2433cf08307e997429aea0f4d7bb3 Mon Sep 17 00:00:00 2001 From: Enoah Netzach Date: Thu, 20 Oct 2016 17:28:14 +0200 Subject: [PATCH] Add `PUBLIC_URL` env variable for advanced use (#937) * Add support for `PUBLIC_URL` env variable * Remove unnecessary duplications * Simplify served path choice logic * Honor PUBLIC_URL in development * Add e2e tests Enables serving static assets from specified host. --- packages/react-scripts/config/paths.js | 35 +++++++++++++++++-- .../react-scripts/config/utils/ensureSlash.js | 10 ++++++ .../config/webpack.config.dev.js | 3 +- .../config/webpack.config.prod.js | 25 +++---------- .../kitchensink/integration/env.test.js | 20 +++++++---- .../kitchensink/integration/initDOM.js | 6 ++-- .../fixtures/kitchensink/src/App.js | 10 ++++-- .../kitchensink/src/features/env/PublicUrl.js | 5 +++ .../src/features/env/PublicUrl.test.js | 10 ++++++ packages/react-scripts/scripts/build.js | 16 +++++---- packages/react-scripts/scripts/eject.js | 17 +++++---- packages/react-scripts/scripts/start.js | 2 +- tasks/e2e-kitchensink.sh | 16 +++++++-- 13 files changed, 122 insertions(+), 53 deletions(-) create mode 100644 packages/react-scripts/config/utils/ensureSlash.js create mode 100644 packages/react-scripts/fixtures/kitchensink/src/features/env/PublicUrl.js create mode 100644 packages/react-scripts/fixtures/kitchensink/src/features/env/PublicUrl.test.js diff --git a/packages/react-scripts/config/paths.js b/packages/react-scripts/config/paths.js index 2f10ea2fb8a..abd9f72865a 100644 --- a/packages/react-scripts/config/paths.js +++ b/packages/react-scripts/config/paths.js @@ -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 @@ -40,6 +41,28 @@ var nodePaths = (process.env.NODE_PATH || '') .filter(folder => !path.isAbsolute(folder)) .map(resolveApp); +var envPublicUrl = process.env.PUBLIC_URL; + +function getPublicUrl(appPackageJson) { + return 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