From 1ed362d38acf6d94f99c936432a137ba36dacf92 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Thu, 9 Feb 2017 23:15:15 -0500 Subject: [PATCH] Add `PUBLIC_URL` env variable for advanced use (#937) (#1504) * 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 | 44 +++++++++++++++++-- .../config/webpack.config.dev.js | 2 +- .../config/webpack.config.prod.js | 26 ++--------- .../kitchensink/integration/env.test.js | 21 ++++++--- .../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/start.js | 2 +- tasks/e2e-kitchensink.sh | 14 +++++- 11 files changed, 109 insertions(+), 47 deletions(-) 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 263941dfe7d..88d35c1386c 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'); // ZEAL: Allow custom build paths via .env config. var buildPath = process.env.BUILD_PATH || 'build' @@ -43,6 +44,37 @@ var nodePaths = (process.env.NODE_PATH || '') .filter(folder => !path.isAbsolute(folder)) .map(resolveApp); +var envPublicUrl = process.env.PUBLIC_URL; + +function ensureSlash(path, needsSlash) { + var hasSlash = path.endsWith('/'); + if (hasSlash && !needsSlash) { + return path.substr(path, path.length - 1); + } else if (!hasSlash && needsSlash) { + return path + '/'; + } else { + return path; + } +} + +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