uni-app中配置自定义条件编译

前提:官网提供的自定义编译不满足条件

package.json | uni-app官网

下文:不详细写,主要写关键思路

package.json文件

主要看scripts的执行命令,其他依赖就是用vue-cli方式创建uni-app项目生成的

{
  "name": "uniapp",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "npm run h5:dev",
    "h5:dev": "cross-env NODE_ENV=development UNI_PLATFORM=h5 node './cx.js' && vue-cli-service uni-serve ",
  },
  "dependencies": {
    "@dcloudio/uni-app": "^2.0.2-3080420230530001",
    "@dcloudio/uni-app-plus": "^2.0.2-3080420230530001",
    "@dcloudio/uni-h5": "^2.0.2-3080420230530001",
    "@dcloudio/uni-i18n": "^2.0.2-3080420230530001",
    "@dcloudio/uni-mp-360": "^2.0.2-3080420230530001",
    "@dcloudio/uni-mp-alipay": "^2.0.2-3080420230530001",
    "@dcloudio/uni-mp-baidu": "^2.0.2-3080420230530001",
    "@dcloudio/uni-mp-jd": "^2.0.2-3080420230530001",
    "@dcloudio/uni-mp-kuaishou": "^2.0.2-3080420230530001",
    "@dcloudio/uni-mp-lark": "^2.0.2-3080420230530001",
    "@dcloudio/uni-mp-qq": "^2.0.2-3080420230530001",
    "@dcloudio/uni-mp-toutiao": "^2.0.2-3080420230530001",
    "@dcloudio/uni-mp-vue": "^2.0.2-3080420230530001",
    "@dcloudio/uni-mp-weixin": "^2.0.2-3080420230530001",
    "@dcloudio/uni-mp-xhs": "^2.0.2-3080420230530001",
    "@dcloudio/uni-quickapp-native": "^2.0.2-3080420230530001",
    "@dcloudio/uni-quickapp-webview": "^2.0.2-3080420230530001",
    "@dcloudio/uni-stacktracey": "^2.0.2-3080420230530001",
    "@dcloudio/uni-stat": "^2.0.2-3080420230530001",
    "@vue/shared": "^3.0.0",
    "autoprefixer": "^8.0.0",
    "core-js": "^3.8.3",
    "flyio": "^0.6.2",
    "pixi.js": "^7.2.4",
    "postcss-loader": "^7.3.3",
    "vue": ">= 2.6.14 < 2.7",
    "vuex": "^3.2.0",
    "weapp-adapter": "^1.0.0"
  },
  "devDependencies": {
    "@dcloudio/types": "^3.3.2",
    "@dcloudio/uni-automator": "^2.0.2-3080420230530001",
    "@dcloudio/uni-cli-i18n": "^2.0.2-3080420230530001",
    "@dcloudio/uni-cli-shared": "^2.0.2-3080420230530001",
    "@dcloudio/uni-helper-json": "*",
    "@dcloudio/uni-migration": "^2.0.2-3080420230530001",
    "@dcloudio/uni-template-compiler": "^2.0.2-3080420230530001",
    "@dcloudio/vue-cli-plugin-hbuilderx": "^2.0.2-3080420230530001",
    "@dcloudio/vue-cli-plugin-uni": "^2.0.2-3080420230530001",
    "@dcloudio/vue-cli-plugin-uni-optimize": "^2.0.2-3080420230530001",
    "@dcloudio/webpack-uni-mp-loader": "^2.0.2-3080420230530001",
    "@dcloudio/webpack-uni-pages-loader": "^2.0.2-3080420230530001",
    "@pixi/unsafe-eval": "^7.2.4",
    "@vue/cli-plugin-babel": "~5.0.0",
    "@vue/cli-service": "~5.0.0",
    "babel-plugin-import": "^1.11.0",
    "cross-env": "^7.0.2",
    "jest": "^25.4.0",
    "mini-types": "*",
    "miniprogram-api-typings": "*",
    "postcss": "^8.4.24",
    "postcss-comment": "^2.0.0",
    "vue-template-compiler": ">= 2.6.14 < 2.7",
    "weapp-pixi-adapter": "^0.2.0"
  },
  "browserslist": [
    "Android >= 4.4",
    "ios >= 9"
  ],
  "uni-app": {

  }
}

ct.js

javascript 复制代码
const fs=require("fs");
let path=require("path");
function replacePlugin(){
    let pluginPath=path.resolve(__dirname,"./node_modules/@dcloudio/uni-cli-shared/lib/plugin.js");
    let copyPluginPath=path.resolve(__dirname,"./node_modules/@dcloudio/uni-cli-shared/lib/copyplugin.js");
    let defaultPlugin=fs.readFileSync(pluginPath,'utf8');
    let checkDir = fs.existsSync(copyPluginPath);
    if(!checkDir){
        fs.writeFileSync(copyPluginPath,defaultPlugin);
    }else{
        defaultPlugin=fs.readFileSync(copyPluginPath,'utf8');
    }
    let a='process.UNI_SCRIPT_DEFINE={"WOBUGUAN":true,"WOBUGUAN2":false};';//你自己需要的自定义编译条件
    fs.writeFileSync(pluginPath,a+defaultPlugin);
}
replacePlugin();

条件编译起作用的地方在这个地方node_modules/@dcloudio/uni-cli-shared/lib/plugin.js文件的这里

这里的取值,在vue-cli-service执行时就完成了,所以在执行前做变量赋值,替换文件,

这里的ct.js文件中的a是你需要自定义 process.UNI_SCRIPT_DEFINE赋值内容

index.vue文件

javascript 复制代码
<script>
export default{
    created(){
        console.log(2222)
        //#ifdef WOBUGUAN2
        console.log("WOBUGUAN2")
        //#endif
        //#ifdef WOBUGUAN
        console.log("WOBUGUAN");
        //#endif
    }
}
</script>

此时自定义条件编译就完成了!!!

虽然不太合理,修改node-module,但没找到别的办法

相关推荐
洗发水很好用2 小时前
uniApp打包H5发布到服务器(docker)
uni-app
YUJIAN。3 小时前
使用uniapp开发微信小程序-框架搭建
微信小程序·小程序·uni-app
还这么多错误?!12 小时前
uniapp微信小程序,使用fastadmin完成一个一键获取微信手机号的功能
微信小程序·小程序·uni-app
IT 前端 张12 小时前
Uniapp 手机基座调试App 打包成Apk文件,并上传到应用商店
uni-app
User_undefined12 小时前
uniapp Native.js原生arr插件服务发送广播到uniapp页面中
android·javascript·uni-app
web1350858863512 小时前
uniapp小程序使用webview 嵌套 vue 项目
vue.js·小程序·uni-app
麦兜*12 小时前
轮播图带详情插件、uniApp插件
前端·javascript·uni-app·vue
veminhe12 小时前
uni-app使用组件button遇到的问题
uni-app·vue
m0_7482400212 小时前
uniapp跨平台开发---webview调用app方法
uni-app
407指导员13 小时前
uniapp 微信小程序 页面部分截图实现
微信小程序·小程序·uni-app