Skip to content

Commit ef4f8e9

Browse files
dhruskagaearon
authored andcommitted
Don't assume the project is hosted at the root (#94)
* Don't assume the project is hosted at the root * Require package.json in webpack.config.prod.js and use homepage if set; otherwise use '/' * Fix package.json path and add sample package.json for tests * Update publicPath to use relative path portion of URL defined in homepage * Update successful bundle generation message * Show bundle generation success message based on presence of homepage in package.json
1 parent 055b414 commit ef4f8e9

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

config/webpack.config.prod.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var autoprefixer = require('autoprefixer');
1212
var webpack = require('webpack');
1313
var HtmlWebpackPlugin = require('html-webpack-plugin');
1414
var ExtractTextPlugin = require('extract-text-webpack-plugin');
15+
var url = require('url');
1516

1617
// TODO: hide this behind a flag and eliminate dead code on eject.
1718
// This shouldn't be exposed to the user.
@@ -26,6 +27,8 @@ var nodeModulesPath = path.join(__dirname, '..', 'node_modules');
2627
var indexHtmlPath = path.resolve(__dirname, relativePath, 'index.html');
2728
var faviconPath = path.resolve(__dirname, relativePath, 'favicon.ico');
2829
var buildPath = path.join(__dirname, isInNodeModules ? '../../..' : '..', 'build');
30+
var homepagePath = require(path.resolve(__dirname, relativePath, 'package.json')).homepage;
31+
var publicPath = homepagePath ? url.parse(homepagePath).pathname : '/';
2932

3033
module.exports = {
3134
bail: true,
@@ -35,9 +38,7 @@ module.exports = {
3538
path: buildPath,
3639
filename: '[name].[chunkhash].js',
3740
chunkFilename: '[name].[chunkhash].chunk.js',
38-
// TODO: this wouldn't work for e.g. GH Pages.
39-
// Good news: we can infer it from package.json :-)
40-
publicPath: '/'
41+
publicPath: publicPath
4142
},
4243
resolve: {
4344
extensions: ['', '.js'],

scripts/build.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ var config = require('../config/webpack.config.prod');
1717
var isInNodeModules = 'node_modules' ===
1818
path.basename(path.resolve(path.join(__dirname, '..', '..')));
1919
var relative = isInNodeModules ? '../..' : '.';
20+
if (process.argv[2] === '--debug-template') {
21+
relative = './template';
22+
}
2023
rimrafSync(relative + '/build');
2124

2225
webpack(config).run(function(err, stats) {
@@ -27,13 +30,18 @@ webpack(config).run(function(err, stats) {
2730
}
2831

2932
var openCommand = process.platform === 'win32' ? 'start' : 'open';
33+
var homepagePath = require(path.resolve(relative, 'package.json')).homepage;
3034
console.log('Successfully generated a bundle in the build folder!');
3135
console.log();
32-
console.log('You can now serve it with any static server, for example:');
33-
console.log(' cd build');
34-
console.log(' npm install -g http-server');
35-
console.log(' hs');
36-
console.log(' ' + openCommand + ' http://localhost:8080');
36+
if (homepagePath) {
37+
console.log('You can now deploy and serve it from ' + homepagePath + '.');
38+
} else {
39+
console.log('You can now serve it with any static server, for example:');
40+
console.log(' cd build');
41+
console.log(' npm install -g http-server');
42+
console.log(' hs');
43+
console.log(' ' + openCommand + ' http://localhost:8080');
44+
}
3745
console.log();
3846
console.log('The bundle is optimized and ready to be deployed to production.');
3947
});

template/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "my-app",
3+
"version": "0.0.1",
4+
"private": true,
5+
"devDependencies": {
6+
"react-scripts": "0.1.0"
7+
},
8+
"dependencies": {
9+
"react": "^15.2.1",
10+
"react-dom": "^15.2.1"
11+
},
12+
"scripts": {
13+
"start": "react-scripts start",
14+
"build": "react-scripts build",
15+
"eject": "react-scripts eject"
16+
}
17+
}

0 commit comments

Comments
 (0)