From cb4a48b16164d8debdf753a6cd6de46e26cc509c Mon Sep 17 00:00:00 2001 From: Jimmy Miller Date: Mon, 22 Aug 2016 15:21:17 -0400 Subject: [PATCH 1/3] Made webpack respect NODE_PATH environment variable Fixes: #253 --- config/env.js | 4 +++- config/webpack.config.dev.js | 3 +++ config/webpack.config.prod.js | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/config/env.js b/config/env.js index cfa10c75b44..07ecdbc3a30 100644 --- a/config/env.js +++ b/config/env.js @@ -12,6 +12,7 @@ var REACT_APP = /^REACT_APP_/i; var NODE_ENV = JSON.stringify(process.env.NODE_ENV || 'development'); +var NODE_PATH = process.env.NODE_PATH || ''; module.exports = Object .keys(process.env) @@ -20,5 +21,6 @@ module.exports = Object env['process.env.' + key] = JSON.stringify(process.env[key]); return env; }, { - 'process.env.NODE_ENV': NODE_ENV + 'process.env.NODE_ENV': NODE_ENV, + 'process.env.NODE_PATH': NODE_PATH }); diff --git a/config/webpack.config.dev.js b/config/webpack.config.dev.js index 2b6da367f79..d646c8ecd1a 100644 --- a/config/webpack.config.dev.js +++ b/config/webpack.config.dev.js @@ -65,6 +65,9 @@ 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(env['process.env.NODE_PATH']), // 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 5781ae8636b..a5ba5464fda 100644 --- a/config/webpack.config.prod.js +++ b/config/webpack.config.prod.js @@ -60,6 +60,9 @@ 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(env['process.env.NODE_PATH']), // These are the reasonable defaults supported by the Node ecosystem. extensions: ['.js', '.json', ''], alias: { From adab3e2e68e56f8c8ea9b4671212ced6a512e1e7 Mon Sep 17 00:00:00 2001 From: Jimmy Miller Date: Mon, 22 Aug 2016 15:53:29 -0400 Subject: [PATCH 2/3] Removed NODE_PATH from env.js env.js is only for variables injected into the app. --- config/env.js | 4 +--- config/webpack.config.dev.js | 2 +- config/webpack.config.prod.js | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/config/env.js b/config/env.js index 07ecdbc3a30..cfa10c75b44 100644 --- a/config/env.js +++ b/config/env.js @@ -12,7 +12,6 @@ var REACT_APP = /^REACT_APP_/i; var NODE_ENV = JSON.stringify(process.env.NODE_ENV || 'development'); -var NODE_PATH = process.env.NODE_PATH || ''; module.exports = Object .keys(process.env) @@ -21,6 +20,5 @@ module.exports = Object env['process.env.' + key] = JSON.stringify(process.env[key]); return env; }, { - 'process.env.NODE_ENV': NODE_ENV, - 'process.env.NODE_PATH': NODE_PATH + 'process.env.NODE_ENV': NODE_ENV }); diff --git a/config/webpack.config.dev.js b/config/webpack.config.dev.js index d646c8ecd1a..d3e44de962d 100644 --- a/config/webpack.config.dev.js +++ b/config/webpack.config.dev.js @@ -67,7 +67,7 @@ module.exports = { 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(env['process.env.NODE_PATH']), + root: path.resolve(process.env.NODE_PATH || ''), // 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 a5ba5464fda..27817b52e96 100644 --- a/config/webpack.config.prod.js +++ b/config/webpack.config.prod.js @@ -62,7 +62,7 @@ module.exports = { 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(env['process.env.NODE_PATH']), + root: path.resolve(process.env.NODE_PATH || ''), // These are the reasonable defaults supported by the Node ecosystem. extensions: ['.js', '.json', ''], alias: { From 59453c1c064ad57a53ca97263b8d2d09b12f7a44 Mon Sep 17 00:00:00 2001 From: Jimmy Miller Date: Tue, 23 Aug 2016 09:11:23 -0400 Subject: [PATCH 3/3] Added ability to specify multiple directories in node_path. --- config/paths.js | 20 +++++++++++++++++--- config/webpack.config.dev.js | 2 +- config/webpack.config.prod.js | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/config/paths.js b/config/paths.js index e6f2ff6cb3a..4570e4b4a5c 100644 --- a/config/paths.js +++ b/config/paths.js @@ -11,6 +11,7 @@ // and use those instead. This way we don't need to branch here. var path = require('path'); +var os = require('os'); // True after ejecting, false when used as a dependency var isEjected = ( @@ -31,6 +32,16 @@ function resolveApp(relativePath) { return path.resolve(relativePath); } +function resolveNodePath(paths) { + if (paths === '') { + return [] + } + var separator = os.platform() === 'win32' ? ';' : ':'; + return paths.split(separator).map(p => path.resolve(p)); +} + +var nodePath = resolveNodePath(process.env.NODE_PATH || ''); + if (isInCreateReactAppSource) { // create-react-app development: we're in ./config/ module.exports = { @@ -39,7 +50,8 @@ if (isInCreateReactAppSource) { appPackageJson: resolveOwn('../package.json'), appSrc: resolveOwn('../template/src'), appNodeModules: resolveOwn('../node_modules'), - ownNodeModules: resolveOwn('../node_modules') + ownNodeModules: resolveOwn('../node_modules'), + nodePath: nodePath }; } else if (!isEjected) { // before eject: we're in ./node_modules/react-scripts/config/ @@ -50,7 +62,8 @@ if (isInCreateReactAppSource) { 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'), + nodePath: nodePath }; } else { // after eject: we're in ./config/ @@ -60,6 +73,7 @@ if (isInCreateReactAppSource) { appPackageJson: resolveApp('package.json'), appSrc: resolveApp('src'), appNodeModules: resolveApp('node_modules'), - ownNodeModules: resolveApp('node_modules') + ownNodeModules: resolveApp('node_modules'), + nodePath: nodePath }; } diff --git a/config/webpack.config.dev.js b/config/webpack.config.dev.js index d3e44de962d..2a11970d6ad 100644 --- a/config/webpack.config.dev.js +++ b/config/webpack.config.dev.js @@ -67,7 +67,7 @@ module.exports = { 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 || ''), + root: paths.nodePath, // 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 27817b52e96..aa0263f8a59 100644 --- a/config/webpack.config.prod.js +++ b/config/webpack.config.prod.js @@ -62,7 +62,7 @@ module.exports = { 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 || ''), + root: paths.nodePath, // These are the reasonable defaults supported by the Node ecosystem. extensions: ['.js', '.json', ''], alias: {