From 82066ac4d66ef177d704034dd38ddd965b87a940 Mon Sep 17 00:00:00 2001 From: Jimmy Miller Date: Tue, 23 Aug 2016 09:11:23 -0400 Subject: [PATCH] Added ability to specify multiple directories in node_path. --- config/paths.js | 22 +++++++++++++++++++--- config/webpack.config.dev.js | 8 +++++--- config/webpack.config.prod.js | 8 +++++--- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/config/paths.js b/config/paths.js index e85fe385a6e..2edd496f6cb 100644 --- a/config/paths.js +++ b/config/paths.js @@ -11,6 +11,18 @@ var path = require('path'); +// We support resolving modules according to NODE_PATH. +// This lets you use absolute paths in imports inside large monorepos: +// https://github.com/facebookincubator/create-react-app/issues/253. +// It works just like NODE_PATH in Node: +// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders +// We will export `nodePaths` as an array of absolute paths. +// It will then be used by Webpack (and potentially other tools). +var nodePaths = (process.env.NODE_PATH || '') + .split(process.platform === 'win32' ? ';' : ':') + .filter(Boolean) + .map(p => path.resolve(p)); + function resolveApp(relativePath) { return path.resolve(relativePath); } @@ -22,13 +34,15 @@ module.exports = { appPackageJson: resolveApp('package.json'), appSrc: resolveApp('src'), appNodeModules: resolveApp('node_modules'), - ownNodeModules: resolveApp('node_modules') + ownNodeModules: resolveApp('node_modules'), + nodePaths: nodePaths }; // @remove-on-eject-begin function resolveOwn(relativePath) { return path.resolve(__dirname, relativePath); } + // config before eject: we're in ./node_modules/react-scripts/config/ module.exports = { appBuild: resolveApp('build'), @@ -37,7 +51,8 @@ module.exports = { appSrc: resolveApp('src'), appNodeModules: resolveApp('node_modules'), // this is empty with npm3 but node resolution searches higher anyway: - ownNodeModules: resolveOwn('../node_modules') + ownNodeModules: resolveOwn('../node_modules'), + nodePaths: nodePaths }; // @remove-on-eject-end @@ -48,6 +63,7 @@ module.exports = { appPackageJson: resolveOwn('../package.json'), appSrc: resolveOwn('../template/src'), appNodeModules: resolveOwn('../node_modules'), - ownNodeModules: resolveOwn('../node_modules') + ownNodeModules: resolveOwn('../node_modules'), + nodePaths: nodePaths }; // @remove-on-publish-end diff --git a/config/webpack.config.dev.js b/config/webpack.config.dev.js index dfad61f5ab8..b5f848bbde9 100644 --- a/config/webpack.config.dev.js +++ b/config/webpack.config.dev.js @@ -67,9 +67,11 @@ module.exports = { publicPath: '/' }, resolve: { - // This allows you to set a root for where webpack should look for modules. - // This enables you to use absolute imports from the root. - root: path.resolve(process.env.NODE_PATH || ''), + // This allows you to set a root for where Webpack should look for modules. + // It must be an absolute path or an array of absolute paths. + // This lets you use absolute paths in imports inside large monorepos: + // https://github.com/facebookincubator/create-react-app/issues/253. + root: paths.nodePaths, // These are the reasonable defaults supported by the Node ecosystem. extensions: ['.js', '.json', ''], alias: { diff --git a/config/webpack.config.prod.js b/config/webpack.config.prod.js index 7ebe693686f..95e8c949ce6 100644 --- a/config/webpack.config.prod.js +++ b/config/webpack.config.prod.js @@ -62,9 +62,11 @@ module.exports = { publicPath: publicPath }, resolve: { - // This allows you to set a root for where webpack should look for modules. - // This enables you to use absolute imports from the root. - root: path.resolve(process.env.NODE_PATH || ''), + // This allows you to set a root for where Webpack should look for modules. + // It must be an absolute path or an array of absolute paths. + // This lets you use absolute paths in imports inside large monorepos: + // https://github.com/facebookincubator/create-react-app/issues/253. + root: paths.nodePaths, // These are the reasonable defaults supported by the Node ecosystem. extensions: ['.js', '.json', ''], alias: {