我使用的是yarn workspaces,它的根目录有一个包含我所有repos的包目录。每个存储库都有自己的node_modules目录,其中包含它的依赖项。根node_modules目录包含整个项目的所有开发依赖项,以及所有其他与开发相关的内容,如webpack.config文件。对于express服务器包,Webpack使用热模块重新加载。
我的问题是,如何配置webpack外部来排除整个项目中的所有node_modules目录,而不仅仅是根目录?
在这种情况下,webpack-node-externals似乎不起作用。
错误消息:
WARNING in ./packages/servers/express/node_modules/colors/lib/colors.js
127:29-43 Critical dependency: the request of a dependency is an expression
WARNING in ./packages/servers/express/node_modules/express/lib/view.js
79:29-41 Critical dependency: the request of a dependency is an expressionWebpack配置:
const webpack = require('webpack');
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const StartServerPlugin = require('start-server-webpack-plugin');
module.exports = {
entry: [
'babel-polyfill',
'webpack/hot/poll?1000',
path.join(__dirname, '../packages/servers/express/server/index.js')
],
watch: true,
target: 'node',
externals: [
nodeExternals({
whitelist: ['webpack/hot/poll?1000']
})
],
resolve: {
alias: {
handlebars: 'handlebars/dist/handlebars.js'
}
},
module: {
rules: [
{
test: /\.js?$/,
use: 'babel-loader',
exclude: /node_modules/
}
]
},
plugins: [
new StartServerPlugin('server.js'),
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.DefinePlugin({
'process.env': { BUILD_TARGET: JSON.stringify('server') }
})
],
output: {
path: path.join(__dirname, '../packages/servers/express/.build'),
filename: 'server.js'
}
};发布于 2018-11-24 05:21:06
如果将yarn工作区与webpack-node-externals一起使用,比设置modulesFromFile: true更好的解决方案是在您的webpack配置中使用以下externals设置:
externals: [
nodeExternals(),
nodeExternals({
modulesDir: path.resolve(__dirname, 'path/to/root/node_modules'),
}),
],本质上是使用nodeExternals的两个实例。一个用于包node_modules,一个用于根node_modules。
发布于 2017-12-03 01:06:46
多亏了@blackxored,我才能在我的项目中修复它。
在您的webpack配置文件中,执行以下操作:
import nodeExternals from 'webpack-node-externals'然后添加
externals: [
nodeExternals({
modulesFromFile: true,
}),
],发布于 2017-10-15 01:42:57
Yarn工作区将兼容的模块提升到根node_modules目录,而不保留任何不兼容的模块(不同的semver等)具有依赖工作区的node_modules目录的模块。如果在没有使用相对路径的情况下请求包,那么它可能是原生的,来自node_module的,或者可能是来自您的某个工作区的符号链接的包。您可能希望所有这些包都是外部的。
如何配置webpack外部来排除整个项目中的所有node_modules目录,而不仅仅是根目录?
我会试试using a function with webpack's external option。系统会向您传递请求的上下文、所请求模块的名称和一个回调,以指示是否应将此特定的导入(请求)视为外部导入。
externals: [
(ctx, req, cb) => {
if (!/node_modules/.test(ctx) && req[0] !== '.') {
// Assumes you have defined an "entries" variable
let notAnEntry = (path) => {
return Object.keys(entries).every((entry) => {
return entries[entry] !== path
});
};
if (notAnEntry(require.resolve(req))) {
// This module is external in a commonjs context
return cb(null, `commonjs ${req}`);
}
}
cb();
}
]https://stackoverflow.com/questions/46010926
复制相似问题