gpt4 book ai didi

vue.js webpack 问题 : can't add plugin to vue. config.js with configureWebpack

转载 作者:行者123 更新时间:2023-12-04 01:51:08 24 4
gpt4 key购买 nike

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!')
})
]
},
};

解析后的 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!"'
}
}
],
...
}

configureWebPack 应该将我的插件与 vue 定义的插件合并。为什么它剥离了 DefinePlugin 类,而只在 plugins 数组中包含构造函数的参数?

最佳答案

因为 Vue 已经包含了 DefinePlugin ,您需要使用 Webpack 的链 API 对其进行修改,而不是尝试添加新的。

module.exports = {
chainWebpack: config => {
config.plugin('define').tap(args => {
args[0].__TEST_MESSAGE__ = JSON.stringify('howdy there!')
return args
})
}
}

这导致以下配置(只是一个例子)...

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

关于vue.js webpack 问题 : can't add plugin to vue. config.js with configureWebpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53076540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com