From 6eae7a9a31559b9668e9e8884b26475ced821032 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 11 Oct 2018 15:37:05 -0500 Subject: [PATCH] use a customized require/.resolve instead of relying on NODE_PATH # Conflicts: # cmd/cmd.js --- cmd/cmd.js | 10 ------ lib/modules/pipeline/webpack.config.js | 49 +++++++++++++++----------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index 179b5d23..5382d6cb 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -25,16 +25,6 @@ if (!process.env.PKG_PATH) { process.env.PKG_PATH = process.env.PWD; } -// NOTE: setting NODE_PATH at runtime won't effect lookup behavior in the -// current process, but will take effect in child processes; this enables -// lookup of *global* embark's own node_modules from within dapp scripts (such -// as an ejected webpack.config.js), making embark's dependencies trasitive -// dependencies of a dapp without the dapp explicitly specifying embark as a -// dependency in the dapp's package.json -process.env.NODE_PATH = utils.joinPath(process.env.EMBARK_PATH, 'node_modules') + - (process.env.NODE_PATH ? require('path').delimiter : '') + - (process.env.NODE_PATH || ''); - process.env.DEFAULT_DIAGRAM_PATH = utils.joinPath(process.env.DAPP_PATH, 'diagram.svg'); process.env.DEFAULT_CMD_HISTORY_PATH = utils.joinPath(process.env.DAPP_PATH, '.embark', 'cmd_history'); process.env.DEFAULT_CMD_HISTORY_SIZE = 20; diff --git a/lib/modules/pipeline/webpack.config.js b/lib/modules/pipeline/webpack.config.js index a350e25d..d60591df 100644 --- a/lib/modules/pipeline/webpack.config.js +++ b/lib/modules/pipeline/webpack.config.js @@ -1,27 +1,36 @@ -// some packages, plugins, and presets referenced/required in this webpack -// config are deps of embark and will be transitive dapp deps unless specified -// in the dapp's own package.json +/* global __dirname module process require */ -// embark modifies process.env.NODE_PATH so that when running dapp scripts in -// embark's child processes, embark's own node_modules directory will be -// searched by node's require(); however, webpack and babel do not directly -// support NODE_PATH, so modules such as babel plugins and presets must be -// resolved with require.resolve(); that is only necessary if a plugin/preset -// is in embark's node_modules vs. the dapp's node_modules - -const cloneDeep = require('lodash.clonedeep'); -// const CompressionPlugin = require('compression-webpack-plugin'); -const glob = require('glob'); -const HardSourceWebpackPlugin = require('hard-source-webpack-plugin'); const path = require('path'); const dappPath = process.env.DAPP_PATH; const embarkPath = process.env.EMBARK_PATH; +const dappNodeModules = path.join(dappPath, 'node_modules'); +const embarkNodeModules = path.join(embarkPath, 'node_modules'); + +function customRequire(mod) { + return require(customRequire.resolve(mod)); +} + +customRequire.resolve = function (mod) { + return require.resolve( + mod, + {paths: [dappNodeModules, embarkNodeModules]} + ); +}; + +// some packages, plugins, and presets referenced/required in this webpack +// config are deps of embark and will effectively be transitive dapp deps +// unless specified in the dapp's own package.json + +const cloneDeep = customRequire('lodash.clonedeep'); +// const CompressionPlugin = customRequire('compression-webpack-plugin'); +const glob = customRequire('glob'); +const HardSourceWebpackPlugin = customRequire('hard-source-webpack-plugin'); + const embarkAliases = require(path.join(dappPath, '.embark/embark-aliases.json')); const embarkAssets = require(path.join(dappPath, '.embark/embark-assets.json')); const embarkJson = require(path.join(dappPath, 'embark.json')); -const embarkNodeModules = path.join(embarkPath, 'node_modules'); const embarkPipeline = require(path.join(dappPath, '.embark/embark-pipeline.json')); const buildDir = path.join(dappPath, embarkJson.buildDir); @@ -57,10 +66,10 @@ const entry = Object.keys(embarkAssets) function resolve(pkgName) { if (Array.isArray(pkgName)) { const _pkgName = pkgName[0]; - pkgName[0] = require.resolve(_pkgName); + pkgName[0] = customRequire.resolve(_pkgName); return pkgName; } - return require.resolve(pkgName); + return customRequire.resolve(pkgName); } // base config @@ -200,7 +209,7 @@ const isFlowEnabled = !embarkPipeline.typescript; if (isFlowEnabled) { // position @babel/plugin-transform-flow-strip-types per babel-preset-react-app baseBabelLoader.options.plugins.unshift( - require.resolve('@babel/plugin-transform-flow-strip-types') + customRequire.resolve('@babel/plugin-transform-flow-strip-types') ); } @@ -213,7 +222,7 @@ if (isTypeScriptEnabled) { // position @babel/preset-typescript as the last preset (runs first) // see: https://blogs.msdn.microsoft.com/typescript/2018/08/27/typescript-and-babel-7/ baseBabelLoader.options.presets.push( - require.resolve('@babel/preset-typescript') + customRequire.resolve('@babel/preset-typescript') ); // additional extensions baseBabelLoader.test = /\.(js|ts)x?$/; @@ -250,7 +259,7 @@ production.name = 'production'; const prodBabelLoader = production.module.rules[3]; // position babel-plugin-transform-react-remove-prop-types per babel-preset-react-app prodBabelLoader.options.plugins.splice(prodBabelLoader.length - 1, 0, [ - require.resolve('babel-plugin-transform-react-remove-prop-types'), + customRequire.resolve('babel-plugin-transform-react-remove-prop-types'), { removeImport: true }