Skip to content

Commit 768c9e8

Browse files
committed
Reads CDN_URL to populate webpack publicPath
closes #647 Previously we only allowed settings the pathname via the "homepage" key in package.json Allows user to configure the full base URL via a $CDN_URL env variable Development has the same behaviour.
1 parent a2d0469 commit 768c9e8

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

packages/react-scripts/config/webpack.config.prod.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,21 @@ if (env['process.env.NODE_ENV'] !== '"production"') {
2424
throw new Error('Production builds must have NODE_ENV=production.');
2525
}
2626

27-
// We use "homepage" field to infer "public path" at which the app is served.
28-
// Webpack needs to know it to put the right <script> hrefs into HTML even in
29-
// single-page apps that may serve index.html for nested URLs like /todos/42.
30-
// We can't use a relative path in HTML because we don't want to load something
31-
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
32-
var homepagePath = require(paths.appPackageJson).homepage;
33-
var publicPath = homepagePath ? url.parse(homepagePath).pathname : '/';
27+
// We use $CDN_URL environment variable to set the "public path" at which the
28+
// app is served, if no $CDN_URL is set we then look at the "homepage" field
29+
// in package.json. Webpack needs to know it to put the right <script> hrefs
30+
// into HTML even in single-page apps that may serve index.html for nested URLs
31+
// like /todos/42. We can't use a relative path in HTML because we don't want to
32+
// load something like /todos/42/static/js/bundle.7289d.js. We have to know the root.
33+
34+
var publicPath;
35+
if (process.env.CDN_URL) {
36+
publicPath = process.env.CDN_URL;
37+
} else {
38+
var homepagePath = require(paths.appPackageJson).homepage;
39+
publicPath = homepagePath ? url.parse(homepagePath).pathname : '';
40+
}
3441
if (!publicPath.endsWith('/')) {
35-
// If we don't do this, file assets will get incorrect paths.
3642
publicPath += '/';
3743
}
3844

@@ -58,7 +64,7 @@ module.exports = {
5864
// We don't currently advertise code splitting but Webpack supports it.
5965
filename: 'static/js/[name].[chunkhash:8].js',
6066
chunkFilename: 'static/js/[name].[chunkhash:8].chunk.js',
61-
// We inferred the "public path" (such as / or /my-project) from homepage.
67+
// We use $CDN_URL or infer the "public path" (such as / or /my-project) from homepage.
6268
publicPath: publicPath
6369
},
6470
resolve: {

packages/react-scripts/scripts/build.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,14 @@ function build(previousSizeMap) {
127127
var openCommand = process.platform === 'win32' ? 'start' : 'open';
128128
var homepagePath = require(paths.appPackageJson).homepage;
129129
var publicPath = config.output.publicPath;
130-
if (homepagePath && homepagePath.indexOf('.github.io/') !== -1) {
130+
if (process.env.CDN_URL) {
131+
// CDN_URL: "http://cdn.com/project"
132+
console.log('The project was built assuming it is hosted at ' + chalk.green(publicPath) + '.');
133+
console.log('You can control this with the ' + chalk.green('homepage') + ' field in your ' + chalk.cyan('package.json') + '.');
134+
console.log();
135+
console.log('The ' + chalk.cyan('build') + ' folder is ready to be deployed.');
136+
console.log();
137+
} else if (homepagePath && homepagePath.indexOf('.github.io/') !== -1) {
131138
// "homepage": "http://user.github.io/project"
132139
console.log('The project was built assuming it is hosted at ' + chalk.green(publicPath) + '.');
133140
console.log('You can control this with the ' + chalk.green('homepage') + ' field in your ' + chalk.cyan('package.json') + '.');

0 commit comments

Comments
 (0)