javascript - 将模块导入 vue.config.js
问题描述
我想导入 npm 包 vue-remove-attributes 以从 DOM 中删除我的“data-testid”属性。https://www.npmjs.com/package/vue-remove-attributes
在自述文件中,他们说我必须在我的 webpack 配置中将模块传递给 vue-loader。我已经安装了 webpack,但我唯一的配置文件是 vue.config.js,它看起来像这样:
module.exports = {
publicPath: "./",
productionSourceMap: false,
lintOnSave: process.env.NODE_ENV !== "production",
}
我尝试像这样导入模块
const createAttributeRemover = require('vue-remove-attributes');
module.exports = {
publicPath: "./",
productionSourceMap: false,
lintOnSave: process.env.NODE_ENV !== "production",
configureWebpack: {
plugins: new createAttributeRemover(),
module: {
rules: [
{
test: /\.vue$/,
use: {
loader: "vue-loader",
options: {
compilerOptions: {
modules: [createAttributeRemover("data-testid")]
}
}
}
}
]
}
},
};
但这似乎不起作用。有什么想法可以在我的 vue 配置中导入 npm 包吗?
解决方案
您没有标记vue-cli
,但您正在使用该configureWebpack
属性,所以我假设您是。您需要修改现有的加载程序规则。您可以通过链接来利用它并添加您的选项:
chainWebpack: config => {
config.module
.rule('vue')
.use('vue-loader')
.tap(options => {
// modify the options...
options.compilerOptions.modules = [createAttributeRemove('data-testid')]
return options
})
推荐阅读
- ruby-on-rails - cron 和 rake 任务有什么区别?
- c# - 序列化 protobuf 时的 XMLSerializer InvalidOperationException
- linux - 了解 cuobjdump 输出
- excel - 向下拖动公式,直到最后一个用 VBA 填充的单元格
- unity3d - 在 Unity 3D 中使用 PUN2 进行 VR 多人游戏体验时出现问题
- java - Gradle 无法识别 Java 10+ 方法
- sql - 将 varchar(max) 字段复制到自己的表中,但新表明显更大
- django - Django - 如何将第一个登录用户重定向到加载页面
- karate - 如果应用程序是多语言的,如何以及在何处传递应用程序数据语言?
- xamarin.forms - MasterDetailPage xamarin.forms 从右到左的流向在 Android 上不起作用