w***@163.com
w***@163.com
  • 发布:2024-08-08 10:42
  • 更新:2024-12-03 11:58
  • 阅读:168

使用了vue.config.js 配置以后打包一直失败

分类:uni-app

使用了vue.config.js 配置以后打包一直失败
ERROR Error loading vue.config.js:
[HBuilder] 10:40:52.547 ERROR TypeError: Cannot read properties of undefined (reading 'appid')
[HBuilder] 10:40:52.557 TypeError: Cannot read properties of undefined (reading 'appid')

2024-08-08 10:42 负责人:无 分享
已邀请:
BoredApe

BoredApe - 有问题就会有答案。

请上传完整的vue.config.js和manifest.json

  • w***@163.com (作者)

    // 导入fs模块

    const fs = require('fs')

    // 导入环境变量配置文件

    const ENV_CONFIG = require('./config/env.js');

    const manifestPath = ${__dirname}/manifest.json;

    let Manifest = fs.readFileSync(manifestPath, {

    encoding: 'utf-8'

    })


    function replaceManifest(path, value) {

    const arr = path.split('.')

    const len = arr.length

    const lastItem = arr[len - 1]


    let i = 0  
    let ManifestArr = Manifest.split(/\n/)

    for (let index = 0; index < ManifestArr.length; index++) {
    const item = ManifestArr[index]
    if (new RegExp(`"${arr[i]}"`).test(item)) ++i
    if (i === len) {
    const hasComma = /,/.test(item)
    ManifestArr[index] = item.replace(
    new RegExp(`"${lastItem}"[\\s\\S]*:[\\s\\S]*`),
    `"${lastItem}": ${value}${hasComma ? ',' : ''}`
    )
    break
    }
    }
    Manifest = ManifestArr.join('\n')

    }

    console.log(process.env)

    console.log(ENV_CONFIG.profiles.active)

    if(process.env.NODE_ENV=='development'){

    process.env.UNI_APP_NAME=process.env.UNI_SCRIPT

    }

    console.log(ENV_CONFIG.profiles.active)

    // 读取环境变量内容

    const appid = ENV_CONFIG[ENV_CONFIG.profiles.active].appid

    console.log(appid)

    if (appid) {

    // #ifdef MP-WEIXIN

    replaceManifest('mp-weixin.appid', "${appid}")

    // #endif

    // #ifdef MP-ALIPAY

    replaceManifest('mp-alipay.appid', "${appid}")

    // #endif

    }


    fs.writeFileSync(manifestPath, Manifest, {

    flag: 'w'

    })




    {

    "name" : "小程序模板",

    "appid" : "UNIC6CF201",

    "description" : "1",

    "versionName" : "1.0.0",

    "versionCode" : "100",

    "transformPx" : false,

    / 5+App特有相关 /

    "app-plus" : {

    "usingComponents" : true,

    "nvueCompiler" : "uni-app",

    "compilerVersion" : 3,

    "splashscreen" : {

    "alwaysShowBeforeRender" : true,

    "waiting" : true,

    "autoclose" : true,

    "delay" : 0

    },

    / 模块配置 /

    "modules" : {},

    / 应用发布信息 /

    "distribute" : {

    / android打包配置 /

    "android" : {

    "permissions" : [

    "<uses-permission android:name=\"android.permission.CHANGE_NETWORK_STATE\"/>",

    "<uses-permission android:name=\"android.permission.MOUNT_UNMOUNT_FILESYSTEMS\"/>",

    "<uses-permission android:name=\"android.permission.READ_CONTACTS\"/>",

    "<uses-permission android:name=\"android.permission.VIBRATE\"/>",

    "<uses-permission android:name=\"android.permission.READ_LOGS\"/>",

    "<uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\"/>",

    "<uses-feature android:name=\"android.hardware.camera.autofocus\"/>",

    "<uses-permission android:name=\"android.permission.WRITE_CONTACTS\"/>",

    "<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\"/>",

    "<uses-permission android:name=\"android.permission.CAMERA\"/>",

    "<uses-permission android:name=\"android.permission.RECORD_AUDIO\"/>",

    "<uses-permission android:name=\"android.permission.GET_ACCOUNTS\"/>",

    "<uses-permission android:name=\"android.permission.MODIFY_AUDIO_SETTINGS\"/>",

    "<uses-permission android:name=\"android.permission.READ_PHONE_STATE\"/>",

    "<uses-permission android:name=\"android.permission.CHANGE_WIFI_STATE\"/>",

    "<uses-permission android:name=\"android.permission.WAKE_LOCK\"/>",

    "<uses-permission android:name=\"android.permission.CALL_PHONE\"/>",

    "<uses-permission android:name=\"android.permission.FLASHLIGHT\"/>",

    "<uses-permission android:name=\"android.permission.ACCESS_COARSE_LOCATION\"/>",

    "<uses-feature android:name=\"android.hardware.camera\"/>",

    "<uses-permission android:name=\"android.permission.ACCESS_FINE_LOCATION\"/>",

    "<uses-permission android:name=\"android.permission.WRITE_SETTINGS\"/>"

    ]

    },

    / ios打包配置 /

    "ios" : {},

    / SDK配置 /

    "sdkConfigs" : {}

    },

    "uniStatistics" : {

    "enable" : false

    }

    },

    / 快应用特有相关 /

    "quickapp" : {},

    / 小程序特有相关 /

    "mp-weixin" : {

    "appid": "111",

    "setting" : {

    "urlCheck" : false,

    "minified" : true,

    "postcss" : true,

    "es6" : true

    },

    "usingComponents" : true,

    "uniStatistics" : {

    "enable" : false

    },

    "optimization" : {

    "subpackages" : true

    },

    "navigateToMiniProgramAppIdList" : [

    "wx7a02312af36f26f0"

    ],

    "usingShopPlugin" : true

    },


    "mp-alipay" : {  
    "usingComponents" : true,
    "uniStatistics" : {
    "enable" : false
    },
    "appid": "",
    "plugins" : {
    "petShopFuli" : {
    "version" : "*", // 拉取当前上架最新版本
    "provider" : "2021001155639035"
    },
    "huabeiPay" : {
    "version" : "*", // 拉取当前上架最新版本
    "provider" : "2019072565990006" //花呗插件的ID,为固定值
    }
    }
    },
    "mp-baidu" : {
    "usingComponents" : true,
    "uniStatistics" : {
    "enable" : false
    },
    "appid" : "22125866",
    "setting" : {
    "urlCheck" : false
    }
    },
    "mp-toutiao" : {
    "usingComponents" : true,
    "uniStatistics" : {
    "enable" : false
    },
    "appid" : "tta56fd3d23a540074",
    "setting" : {
    "urlCheck" : false
    }
    },
    "uniStatistics" : {
    "enable" : false
    },
    "h5" : {
    "uniStatistics" : {
    "enable" : false
    }
    },
    "mp-qq" : {
    "uniStatistics" : {
    "enable" : false
    },
    "appid" : "",
    "groupIdList" : [ "", "", "", "", "" ]
    }

    }

    2024-08-09 16:09

7***@qq.com

7***@qq.com

{
"name" : "uniapp-test",
"appid" : "UNIC62A409",
"description" : "",
"versionName" : "1.0.0",
"versionCode" : "100",
"transformPx" : false,
/ 5+App特有相关 /
"app-plus" : {
"usingComponents" : true,
"nvueStyleCompiler" : "uni-app",
"compilerVersion" : 3,
"splashscreen" : {
"alwaysShowBeforeRender" : true,
"waiting" : true,
"autoclose" : true,
"delay" : 0
},
/ 模块配置 /
"modules" : {},
/ 应用发布信息 /
"distribute" : {
/ android打包配置 /
"android" : {
"permissions" : [
"<uses-permission android:name=\"android.permission.CHANGE_NETWORK_STATE\"/>",
"<uses-permission android:name=\"android.permission.MOUNT_UNMOUNT_FILESYSTEMS\"/>",
"<uses-permission android:name=\"android.permission.VIBRATE\"/>",
"<uses-permission android:name=\"android.permission.READ_LOGS\"/>",
"<uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\"/>",
"<uses-feature android:name=\"android.hardware.camera.autofocus\"/>",
"<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\"/>",
"<uses-permission android:name=\"android.permission.CAMERA\"/>",
"<uses-permission android:name=\"android.permission.GET_ACCOUNTS\"/>",
"<uses-permission android:name=\"android.permission.READ_PHONE_STATE\"/>",
"<uses-permission android:name=\"android.permission.CHANGE_WIFI_STATE\"/>",
"<uses-permission android:name=\"android.permission.WAKE_LOCK\"/>",
"<uses-permission android:name=\"android.permission.FLASHLIGHT\"/>",
"<uses-feature android:name=\"android.hardware.camera\"/>",
"<uses-permission android:name=\"android.permission.WRITE_SETTINGS\"/>"
]
},
/ ios打包配置 /
"ios" : {},
/ SDK配置 /
"sdkConfigs" : {}
}
},
/ 快应用特有相关 /
"quickapp" : {},
/ 小程序特有相关 /
"mp-weixin" : {
"appid" : "",
"setting" : {
"urlCheck" : false
},
"usingComponents" : true
},
"mp-alipay" : {
"usingComponents" : true
},
"mp-baidu" : {
"usingComponents" : true
},
"mp-toutiao" : {
"usingComponents" : true
},
"uniStatistics" : {
"enable" : false
},
"vueVersion" : "3"
}


  • import { defineConfig } from 'vite'
    // import compression from 'vite-plugin-compression'

export default defineConfig({
plugins:[]
})


-报错

  • 4 11:33:52 [vite] Pre-transform error: Failed to resolve import "./App" from "D:/uniapp-test/main.js". Does the file exist?
    11:33:52.706 11:33:52 [vite] Internal server error: Failed to resolve import "./App" from "D:/uniapp-test/main.js". Does the file exist?
    11:33:52.707 Plugin: vite:import-analysis
    11:33:52.707 File: D:/uniapp-test/main.js:1:18
    11:33:52.707 1 | import App from './App'
    11:33:52.707 | ^
    11:33:52.707 2 | console.log(App);
    11:33:52.707 3 |
    11:33:52.707 at formatError (file:///C:/HBuilderX/plugins/uniapp-cli-vite/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:50863:46)
    11:33:52.707 at TransformContext.error (file:///C:/HBuilderX/plugins/uniapp-cli-vite/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:50857:19)
    11:33:52.707 at normalizeUrl (file:///C:/HBuilderX/plugins/uniapp-cli-vite/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:66092:33)
    11:33:52.707 at async file:///C:/HBuilderX/plugins/uniapp-cli-vite/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:66247:47
    11:33:52.707 at async Promise.all (index 0)
    11:33:52.707 at async TransformContext.transform (file:///C:/HBuilderX/plugins/uniapp-cli-vite/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:66168:13)
    11:33:52.708 at async Object.transform (file:///C:/HBuilderX/plugins/uniapp-cli-vite/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:51172:30)
    11:33:52.708 at async loadAndTransform (file:///C:/HBuilderX/plugins/uniapp-cli-vite/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:53923:29)
    11:33:52.708 at async viteTransformMiddleware (file:///C:/HBuilderX/plugins/uniapp-cli-vite/node_modules/vite/dist/node/chunks/dep-whKeNLxG.js:63775:32)
锦鲤丶接单丶

锦鲤丶接单丶 - 锦鲤丶接单丶18560000860丶10+年开发经验

取appid的时候用可选操作符(?.)取

要回复问题请先登录注册