【发布时间】:2021-10-04 04:20:44
【问题描述】:
我需要为我的项目使用条件编译,我正在尝试安装这个名为 ifdef-loader 的库。我知道设置需要一个 webpack.config.json 文件,该文件在我的 Vue 环境中丢失。经过研究,我发现可以使用 vue.config.json 代替。我在我的项目目录中创建了这个文件并使用了这个设置:
const opts = {
DEBUG: true,
version: 3,
"ifdef-verbose": true, // add this for verbose output
"ifdef-triple-slash": false, // add this to use double slash comment instead of default triple slash
"ifdef-fill-with-blanks": true, // add this to remove code with blank spaces instead of "//" comments
"ifdef-uncomment-prefix": "// #code " // add this to uncomment code starting with "// #code "
};
module.exports = {
configureWebpack: {
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{ loader: "ts-loader" },
{ loader: "ifdef-loader", options: opts }
]
},
}
我如何在我的代码中使用该库。使用布尔值显示/隐藏某些文本的非常简单的示例:
<template>
<p>Lorem ipsum dolor sit amet.</p>
// #if name
<p>Lorem ipsum dolor sit.</p>
// #endif
</template>
<script>
export default {
name: 'App',
setup() {
const name = "example"
return {
name
}
}
}
</script>
但是,在我的应用程序中什么都没有发生, /// 条件代码只是显示为没有功能的普通文本。
不确定我是否设置正确,但我真的很感激一些指针。
【问题讨论】:
-
嗨,@Nora。这可能是缓存的问题。尝试删除 node_modules/.cache 并再次构建。另外,你肯定在使用 vue-cli,所以 webpack 配置在 vue.config.json 文件中。
-
嗨@Victor,我试过这样做,但没有帮助。我觉得我的 vue.config.js 文件设置可能有问题。
标签: javascript vue.js webpack conditional-compilation