丶听雨
丶听雨
  • 发布:2020-08-27 10:19
  • 更新:2020-08-27 11:03
  • 阅读:2391

【报Bug】npm创建uni-app项目,依赖包内JS代码条件编译不生效

分类:uni-app

产品分类: uniapp/H5

PC开发环境操作系统: Mac

PC开发环境操作系统版本号: 10.13.6

浏览器平台: Chrome

浏览器版本: 84.0.4147.135

项目创建方式: CLI

CLI版本号: 2.0.0-28820200820001

示例代码:
npm包入口文件代码如下
// index.js  
import store from './src/store/index'  
export let CoreStore = store
npm包内store/index代码如下
// src/store/index.js  
import Vue from 'vue'  
import Vuex from 'vuex'  
Vue.use(Vuex)  
const store = new Vuex.Store({  
    state: {user: {}},  
    actions: {  
        getUserInfo() {  
           /* #ifdef MP-ALIPAY */  
            console.log(11111111)  
            /* #endif */  
            /* #ifdef MP-WEIXIN */  
            console.log(22222222)  
            /* #endif */  
            /* #ifdef MP-TOUTIAO */  
            console.log(33333333)  
            /* #endif */  
            /* #ifdef MP-BAIDU */  
            console.log(44444444)  
            /* #endif */  
            /* #ifdef MP-QQ */  
            console.log(55555555)  
            /* #endif */  
            /* #ifdef H5 */  
            console.log(66666666)  
            /* #endif */  
        }  
    }  
})

操作步骤:

必现

应用项目内
// main.js  
import Vue from 'vue'  
import App from './App'  
import {CoreStore} from 'core-store'  

Vue.config.productionTip = false  

Vue.prototype.$store = CoreStore  
Vue.store = CoreStore  

CoreStore.dispatch('getUserInfo')  

const app = new Vue({  
  ...App,  
  store: CoreStore  
})  
app.$mount()

预期结果:

应该在对应平台只展示对应的数据内容

实际结果:
此时打印的结果为
11111111  
22222222  
33333333  
44444444  
55555555  
66666666

bug描述:

项目通过cli创建,引入了一个私有的npm组件集合包,包内的JS代码条件编译没有效果

npm包入口文件代码如下
// index.js  
import store from './src/store/index'  
export let CoreStore = store
npm包内store/index代码如下
// src/store/index.js  
import Vue from 'vue'  
import Vuex from 'vuex'  
Vue.use(Vuex)  
const store = new Vuex.Store({  
    state: {user: {}},  
    actions: {  
        getUserInfo() {  
           /* #ifdef MP-ALIPAY */  
            console.log(11111111)  
            /* #endif */  
            /* #ifdef MP-WEIXIN */  
            console.log(22222222)  
            /* #endif */  
            /* #ifdef MP-TOUTIAO */  
            console.log(33333333)  
            /* #endif */  
            /* #ifdef MP-BAIDU */  
            console.log(44444444)  
            /* #endif */  
            /* #ifdef MP-QQ */  
            console.log(55555555)  
            /* #endif */  
            /* #ifdef H5 */  
            console.log(66666666)  
            /* #endif */  
        }  
    }  
})
2020-08-27 10:19 负责人:无 分享
已邀请:

最佳回复

DCloud_UNI_HT

DCloud_UNI_HT

在 vue.config.js 中配置 transpileDependencies 显示转译你的依赖包

参考: https://cli.vuejs.org/zh/config/#transpiledependencies

  • 丶听雨 (作者)

    可以了,感谢

    2020-08-27 11:10

该问题目前已经被锁定, 无法添加新回复