-
-
Notifications
You must be signed in to change notification settings - Fork 26.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PUBLIC_URL
env variable for advanced use (#937)
#1504
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <script> hrefs into HTML even in | ||
// single-page apps that may serve index.html for nested URLs like /todos/42. | ||
// We can't use a relative path in HTML because we don't want to load something | ||
// like /todos/42/static/js/bundle.7289d.js. We have to know the root. | ||
function getServedPath(appPackageJson) { | ||
var publicUrl = getPublicUrl(appPackageJson); | ||
var servedUrl = envPublicUrl || ( | ||
publicUrl ? url.parse(publicUrl).pathname : '/' | ||
); | ||
return ensureSlash(servedUrl, true); | ||
} | ||
|
||
// config after eject: we're in ./config/ | ||
module.exports = { | ||
appBuild: resolveApp('build'), | ||
|
@@ -52,7 +84,9 @@ module.exports = { | |
testsSetup: resolveApp('src/setupTests.js'), | ||
appNodeModules: resolveApp('node_modules'), | ||
ownNodeModules: resolveApp('node_modules'), | ||
nodePaths: nodePaths | ||
nodePaths: nodePaths, | ||
publicUrl: getPublicUrl(resolveApp('package.json')), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's fine to give these more verbose names if that would make it easier to understand what they represent. Right now it's not super obvious to me what the difference is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went ahead and just removed the third variable as it was unnecessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you prefer different names still? |
||
servedPath: getServedPath(resolveApp('package.json')) | ||
}; | ||
|
||
// @remove-on-eject-begin | ||
|
@@ -73,7 +107,9 @@ module.exports = { | |
appNodeModules: resolveApp('node_modules'), | ||
// this is empty with npm3 but node resolution searches higher anyway: | ||
ownNodeModules: resolveOwn('../node_modules'), | ||
nodePaths: nodePaths | ||
nodePaths: nodePaths, | ||
publicUrl: getPublicUrl(resolveApp('package.json')), | ||
servedPath: getServedPath(resolveApp('package.json')) | ||
}; | ||
|
||
// config before publish: we're in ./packages/react-scripts/config/ | ||
|
@@ -89,7 +125,9 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1) | |
testsSetup: resolveOwn('../template/src/setupTests.js'), | ||
appNodeModules: resolveOwn('../node_modules'), | ||
ownNodeModules: resolveOwn('../node_modules'), | ||
nodePaths: nodePaths | ||
nodePaths: nodePaths, | ||
publicUrl: getPublicUrl(resolveOwn('../package.json')), | ||
servedPath: getServedPath(resolveOwn('../package.json')) | ||
}; | ||
} | ||
// @remove-on-eject-end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from 'react' | ||
|
||
export default () => ( | ||
<span id="feature-public-url">{process.env.PUBLIC_URL}.</span> | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import PublicUrl from './PublicUrl'; | ||
|
||
describe('PUBLIC_URL', () => { | ||
it('renders without crashing', () => { | ||
const div = document.createElement('div'); | ||
ReactDOM.render(<PublicUrl />, div); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this have been
path.length
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed that this is wrong, though I think that it should be
0
, notpath.length
. This code works very accidentally, becauseNumber(path)
isNaN
, which is converted to+0
byToInteger
in the definition ofString.prototype.substr
:substr
ToInteger
(edited to include links to docs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in #4424.