我正在尝试捆绑我的项目,然后需要捆绑的缩小输出。
我的 index.js 文件如下所示:
const browserHost = require('./hosts/browserHost')
const workerHost = require('./hosts/workerHost')
module.exports = {
initBrowserHost: options => browserHost.init(options),
initWorkerHost: options => workerHost.init(options)
}
当需要它时,我有两个初始化函数。当我将项目与 webpack
捆绑在一起并需要 index.min.js
时,我有一个空对象。
Webpack 配置:
const TerserPlugin = require('terser-webpack-plugin')
const path = require('path')
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: [
/node_modules/,
/\.unit\.js$/
],
use: ['babel-loader']
}
]
},
node: {
fs: 'empty',
dns: 'empty'
},
target: 'node',
entry: [
'./src/index.js'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'index.min.js'
},
optimization: {
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
ecma: 6
}
})
]
}
}
我做错了什么?
最佳答案
如果你从node_modules构建一个库供其他人使用,你需要告诉webpack你想要支持umd
(简单来说,你想让你的消费者从使用它>require(yinon_lib)
或 import .. from 'yinon_lib'`)。
这样做的方法是:
output: {
...
libraryTarget: 'umd',
},
更多信息:
示例:
https://github.com/stavalfi/lerna-yarn-workspaces-example/tree/master/packages/x-core
关于javascript - 为什么 webpack 在需要输出时返回一个空对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54434924/