Commit d2baa3c4 authored by Dallon Feldner's avatar Dallon Feldner Committed by Dan Abramov
Browse files

Symlink-friendly path resolution (#277)

* Symlink-friendly path resolution

I was having difficulties using a local copy of `react-scripts` and `npm link`ing it into a real world project. This change resolves paths relative to the current working directory (that is, most likely the directory of the app) rather than assuming a certain directory structure.

* Fix relative paths in post-eject case

because I'm an idiot

* Renamed resolveLib to resolveOwn
parent 19580a8b
Showing with 31 additions and 27 deletions
+31 -27
...@@ -12,10 +12,10 @@ ...@@ -12,10 +12,10 @@
var path = require('path'); var path = require('path');
// True when used as a dependency, false after ejecting // True after ejecting, false when used as a dependency
var isInNodeModules = ( var isEjected = (
'node_modules' === path.resolve(path.join(__dirname, '..')) ===
path.basename(path.resolve(path.join(__dirname, '..', '..'))) path.resolve(process.cwd())
); );
// Are we developing create-react-app locally? // Are we developing create-react-app locally?
...@@ -23,42 +23,46 @@ var isInCreateReactAppSource = ( ...@@ -23,42 +23,46 @@ var isInCreateReactAppSource = (
process.argv.some(arg => arg.indexOf('--debug-template') > -1) process.argv.some(arg => arg.indexOf('--debug-template') > -1)
); );
function resolve(relativePath) { function resolveOwn(relativePath) {
return path.resolve(__dirname, relativePath); return path.resolve(__dirname, relativePath);
} }
function resolveApp(relativePath) {
return path.resolve(relativePath);
}
if (isInCreateReactAppSource) { if (isInCreateReactAppSource) {
// create-react-app development: we're in ./config/ // create-react-app development: we're in ./config/
module.exports = { module.exports = {
appBuild: resolve('../build'), appBuild: resolveOwn('../build'),
appHtml: resolve('../template/index.html'), appHtml: resolveOwn('../template/index.html'),
appFavicon: resolve('../template/favicon.ico'), appFavicon: resolveOwn('../template/favicon.ico'),
appPackageJson: resolve('../package.json'), appPackageJson: resolveOwn('../package.json'),
appSrc: resolve('../template/src'), appSrc: resolveOwn('../template/src'),
appNodeModules: resolve('../node_modules'), appNodeModules: resolveOwn('../node_modules'),
ownNodeModules: resolve('../node_modules') ownNodeModules: resolveOwn('../node_modules')
}; };
} else if (isInNodeModules) { } else if (!isEjected) {
// before eject: we're in ./node_modules/react-scripts/config/ // before eject: we're in ./node_modules/react-scripts/config/
module.exports = { module.exports = {
appBuild: resolve('../../../build'), appBuild: resolveApp('build'),
appHtml: resolve('../../../index.html'), appHtml: resolveApp('index.html'),
appFavicon: resolve('../../../favicon.ico'), appFavicon: resolveApp('favicon.ico'),
appPackageJson: resolve('../../../package.json'), appPackageJson: resolveApp('package.json'),
appSrc: resolve('../../../src'), appSrc: resolveApp('src'),
appNodeModules: resolve('../..'), appNodeModules: resolveApp('node_modules'),
// this is empty with npm3 but node resolution searches higher anyway: // this is empty with npm3 but node resolution searches higher anyway:
ownNodeModules: resolve('../node_modules') ownNodeModules: resolveOwn('../node_modules')
}; };
} else { } else {
// after eject: we're in ./config/ // after eject: we're in ./config/
module.exports = { module.exports = {
appBuild: resolve('../build'), appBuild: resolveApp('build'),
appHtml: resolve('../index.html'), appHtml: resolveApp('index.html'),
appFavicon: resolve('../favicon.ico'), appFavicon: resolveApp('favicon.ico'),
appPackageJson: resolve('../package.json'), appPackageJson: resolveApp('package.json'),
appSrc: resolve('../src'), appSrc: resolveApp('src'),
appNodeModules: resolve('../node_modules'), appNodeModules: resolveApp('node_modules'),
ownNodeModules: resolve('../node_modules') ownNodeModules: resolveApp('node_modules')
}; };
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment