From 5887372e7e3ab19db4211f7e75346c642c2b78ca 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. --- config/paths.js | 44 +++++++++++++++++-- config/webpack.config.dev.js | 2 +- config/webpack.config.prod.js | 26 ++--------- fixtures/kitchensink/integration/env.test.js | 21 ++++++--- fixtures/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 +++++ scripts/build.js | 16 ++++--- scripts/start.js | 2 +- 10 files changed, 97 insertions(+), 45 deletions(-) create mode 100644 fixtures/kitchensink/src/features/env/PublicUrl.js create mode 100644 fixtures/kitchensink/src/features/env/PublicUrl.test.js diff --git a/config/paths.js b/config/paths.js index 2f10ea2fb8a..7d114080f7c 100644 --- a/config/paths.js +++ b/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,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