From d2baa3c4ddb0cb4b07b1bea2d9f317a447653457 Mon Sep 17 00:00:00 2001 From: Dallon Feldner Date: Fri, 29 Jul 2016 11:09:40 -0500 Subject: [PATCH] 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 --- config/paths.js | 58 ++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/config/paths.js b/config/paths.js index b0f94588ad..7645917fbc 100644 --- a/config/paths.js +++ b/config/paths.js @@ -12,10 +12,10 @@ var path = require('path'); -// True when used as a dependency, false after ejecting -var isInNodeModules = ( - 'node_modules' === - path.basename(path.resolve(path.join(__dirname, '..', '..'))) +// True after ejecting, false when used as a dependency +var isEjected = ( + path.resolve(path.join(__dirname, '..')) === + path.resolve(process.cwd()) ); // Are we developing create-react-app locally? @@ -23,42 +23,46 @@ var isInCreateReactAppSource = ( process.argv.some(arg => arg.indexOf('--debug-template') > -1) ); -function resolve(relativePath) { +function resolveOwn(relativePath) { return path.resolve(__dirname, relativePath); } +function resolveApp(relativePath) { + return path.resolve(relativePath); +} + if (isInCreateReactAppSource) { // create-react-app development: we're in ./config/ module.exports = { - appBuild: resolve('../build'), - appHtml: resolve('../template/index.html'), - appFavicon: resolve('../template/favicon.ico'), - appPackageJson: resolve('../package.json'), - appSrc: resolve('../template/src'), - appNodeModules: resolve('../node_modules'), - ownNodeModules: resolve('../node_modules') + appBuild: resolveOwn('../build'), + appHtml: resolveOwn('../template/index.html'), + appFavicon: resolveOwn('../template/favicon.ico'), + appPackageJson: resolveOwn('../package.json'), + appSrc: resolveOwn('../template/src'), + appNodeModules: resolveOwn('../node_modules'), + ownNodeModules: resolveOwn('../node_modules') }; -} else if (isInNodeModules) { +} else if (!isEjected) { // before eject: we're in ./node_modules/react-scripts/config/ module.exports = { - appBuild: resolve('../../../build'), - appHtml: resolve('../../../index.html'), - appFavicon: resolve('../../../favicon.ico'), - appPackageJson: resolve('../../../package.json'), - appSrc: resolve('../../../src'), - appNodeModules: resolve('../..'), + appBuild: resolveApp('build'), + appHtml: resolveApp('index.html'), + appFavicon: resolveApp('favicon.ico'), + appPackageJson: resolveApp('package.json'), + appSrc: resolveApp('src'), + appNodeModules: resolveApp('node_modules'), // this is empty with npm3 but node resolution searches higher anyway: - ownNodeModules: resolve('../node_modules') + ownNodeModules: resolveOwn('../node_modules') }; } else { // after eject: we're in ./config/ module.exports = { - appBuild: resolve('../build'), - appHtml: resolve('../index.html'), - appFavicon: resolve('../favicon.ico'), - appPackageJson: resolve('../package.json'), - appSrc: resolve('../src'), - appNodeModules: resolve('../node_modules'), - ownNodeModules: resolve('../node_modules') + appBuild: resolveApp('build'), + appHtml: resolveApp('index.html'), + appFavicon: resolveApp('favicon.ico'), + appPackageJson: resolveApp('package.json'), + appSrc: resolveApp('src'), + appNodeModules: resolveApp('node_modules'), + ownNodeModules: resolveApp('node_modules') }; }