jin*_*mer 4 webpack vue.js vue-cli-3
vue.js webpack 问题:我无法使用 configureWebpack 将插件添加到 vue.config.js
我使用 vue cli 3 创建了一个 vue.js 项目。我遵循以下示例: https: //cli.vuejs.org/guide/webpack.html
我的 vue.config.js:
let webpack = require('webpack');
module.exports = {
configureWebpack: {
plugins: [
new webpack.DefinePlugin({
__TEST_MESSAGE__: JSON.stringify('howdy there!')
})
]
},
};
Run Code Online (Sandbox Code Playgroud)
解析后的 webpack 配置如下所示:
{
mode: 'production',
...
plugins: [
/* config.plugin('vue-loader') */
new VueLoaderPlugin(),
/* config.plugin('define') */
new DefinePlugin(
{
'process.env': {
VUE_APP_CLI_UI_URL: '""',
NODE_ENV: '"production"',
BASE_URL: '"/"'
}
}
),
/* config.plugin('case-sensitive-paths') */
new CaseSensitivePathsPlugin(),
...
/////////////////////////////////////////////////////
// Inserted note for SO: This is messed up! Should
// be:
// new DefinePlugin({ __TEST_MESSAGE__: '"howdy there!"' })
/////////////////////////////////////////////////////
{
definitions: {
__TEST_MESSAGE__: '"howdy there!"'
}
}
],
...
}
Run Code Online (Sandbox Code Playgroud)
configureWebPack 应该将我的插件与 vue 定义的插件合并。为什么它要剥离 DefinePlugin 类并仅将构造函数的参数包含在插件数组中?
由于 Vue 已经包含了DefinePlugin
,因此您需要使用 Webpack 的链 API 来修改它,而不是尝试添加新的。
module.exports = {
chainWebpack: config => {
config.plugin('define').tap(args => {
args[0].__TEST_MESSAGE__ = JSON.stringify('howdy there!')
return args
})
}
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下配置(仅作为示例)...
Run Code Online (Sandbox Code Playgroud)new DefinePlugin( { 'process.env': { NODE_ENV: '"development"', BASE_URL: '"/"' }, __TEST_MESSAGE__: '"howdy there!"' } ),
请参阅https://cli.vuejs.org/guide/webpack.html#modifying-options-of-a-plugin