Browse Source

finish vite config

dev
刘嘉炜 1 week ago
parent
commit
d57be341f7
  1. 5
      config/appid.dev.js
  2. 5
      config/appid.prod.js
  3. 4
      envs/.env.development
  4. 4
      envs/.env.production
  5. 4
      package.json
  6. 20
      src/App.vue
  7. 4
      src/manifest.json
  8. 50
      src/pages/index/index.vue
  9. BIN
      src/static/logo.png
  10. 14
      tools/json-editor.js
  11. 19
      vite.config.js

5
config/appid.dev.js

@ -1,5 +0,0 @@
module.exports = {
'mp-toutiao': {
appid: 'ttc690b02d599a28ee01-d',
},
};

5
config/appid.prod.js

@ -1,5 +0,0 @@
module.exports = {
'mp-toutiao': {
appid: 'ttc690b02d599a28ee01-p',
},
};

4
envs/.env.development

@ -0,0 +1,4 @@
# 接口地址
VITE_API_BASE=https://api.test.com
# 头条小程序 appid
VITE_TOUTIAO_APPID=ttc690b02d599a28ee01

4
envs/.env.production

@ -0,0 +1,4 @@
# 接口地址
VITE_API_BASE=https://api.formal.com
# 头条小程序 appid
VITE_TOUTIAO_APPID=ttc690b02d599a28ee01-p

4
package.json

@ -11,8 +11,8 @@
"dev:mp-kuaishou": "uni -p mp-kuaishou", "dev:mp-kuaishou": "uni -p mp-kuaishou",
"dev:mp-lark": "uni -p mp-lark", "dev:mp-lark": "uni -p mp-lark",
"dev:mp-qq": "uni -p mp-qq", "dev:mp-qq": "uni -p mp-qq",
"dev:mp-toutiao": "uni -p mp-toutiao --mode dev",
"dev:mp-toutiao-prod": "uni -p mp-toutiao --mode prod",
"dev:mp-toutiao": "uni -p mp-toutiao --mode development",
"pro:mp-toutiao": "uni -p mp-toutiao --mode production",
"dev:mp-harmony": "uni -p mp-harmony", "dev:mp-harmony": "uni -p mp-harmony",
"dev:mp-weixin": "uni -p mp-weixin", "dev:mp-weixin": "uni -p mp-weixin",
"dev:mp-xhs": "uni -p mp-xhs", "dev:mp-xhs": "uni -p mp-xhs",

20
src/App.vue

@ -1,17 +1,13 @@
<script>
export default {
onLaunch: function () {
console.log('App Launch')
},
onShow: function () {
console.log('App Show')
},
onHide: function () {
console.log('App Hide')
},
}
<script setup>
import { onLaunch } from '@dcloudio/uni-app';
onLaunch(() => {
console.log('App onLaunch');
})
</script> </script>
<style> <style>
/*每个页面公共css */ /*每个页面公共css */
view, scroll-view, picker, input {
box-sizing: border-box;
}
</style> </style>

4
src/manifest.json

@ -1,5 +1,5 @@
{ {
"name" : "",
"name" : "app",
"appid" : "", "appid" : "",
"description" : "", "description" : "",
"versionName" : "1.0.0", "versionName" : "1.0.0",
@ -63,7 +63,7 @@
"usingComponents" : true "usingComponents" : true
}, },
"mp-toutiao" : { "mp-toutiao" : {
"appid" : "ttc690b02d599a28ee01-d",
"appid" : "ttc690b02d599a28ee01",
"setting" : { "setting" : {
"urlCheck" : false "urlCheck" : false
}, },

50
src/pages/index/index.vue

@ -1,48 +1,14 @@
<template>
<view class="content">
<image class="logo" src="/static/logo.png"></image>
<view class="text-area">
<text class="title">{{ title }}</text>
</view>
</view>
</template>
<script setup>
import { onLoad } from '@dcloudio/uni-app';
<script>
export default {
data() {
return {
title: 'Hello',
}
},
onLoad() {},
methods: {},
}
</script>
onLoad(() => {});
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
</script>
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
<template>
<view class="index-index">hello</view>
</template>
.text-area {
display: flex;
justify-content: center;
}
<style lang="scss" scoped>
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style> </style>

BIN
src/static/logo.png

Before

Width: 72  |  Height: 72  |  Size: 3.9 KiB

14
tools/json-editor.js

@ -13,7 +13,7 @@ const path = require('path');
function updateJsonFileField(filePath, fieldPath, newValue, options = {}) { function updateJsonFileField(filePath, fieldPath, newValue, options = {}) {
const tabSize = options.tabSize || 2; const tabSize = options.tabSize || 2;
if (!fs.existsSync(filePath)) { if (!fs.existsSync(filePath)) {
console.warn(`[json-editor] ⚠️ 文件不存在: ${filePath}`);
console.warn(`[json-editor] 文件不存在: ${filePath}`);
return; return;
} }
try { try {
@ -21,16 +21,16 @@ function updateJsonFileField(filePath, fieldPath, newValue, options = {}) {
parse(raw); // 验证是否为合法 JSONC parse(raw); // 验证是否为合法 JSONC
const edits = modify(raw, fieldPath, newValue, { const edits = modify(raw, fieldPath, newValue, {
formattingOptions: {
insertSpaces: true,
tabSize,
},
formattingOptions: {
insertSpaces: true,
tabSize,
},
}); });
const updated = applyEdits(raw, edits); const updated = applyEdits(raw, edits);
fs.writeFileSync(filePath, updated, 'utf-8'); fs.writeFileSync(filePath, updated, 'utf-8');
console.log(`[json-editor] ${path.basename(filePath)} -> 修改 ${fieldPath.join('.')} = ${JSON.stringify(newValue)}`);
console.log(`[json-editor] ${path.basename(filePath)} -> 修改 ${fieldPath.join('.')} = ${JSON.stringify(newValue)}`);
} catch (err) { } catch (err) {
console.error(`[json-editor] 修改失败: ${err.message}`);
console.error(`[json-editor] 修改失败: ${err.message}`);
} }
} }

19
vite.config.js

@ -1,19 +1,22 @@
import { defineConfig } from 'vite';
import { defineConfig, loadEnv } from 'vite';
import uni from '@dcloudio/vite-plugin-uni'; import uni from '@dcloudio/vite-plugin-uni';
import path from 'path'; import path from 'path';
import { updateJsonFileField } from './tools/json-editor.js'; import { updateJsonFileField } from './tools/json-editor.js';
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig(({ mode }) =>{
console.log('当前模式:', mode);
const configAppidPath = path.resolve(__dirname, `config/appid.${mode}.js`);
const appidConfig = require(configAppidPath);
/**
* @param {String} mode development/production
*/
export default defineConfig(({ mode = 'development' }) =>{
const env = loadEnv(mode, './envs');
console.log('current mode:', mode);
const manifestPath = path.resolve(__dirname, 'src/manifest.json'); const manifestPath = path.resolve(__dirname, 'src/manifest.json');
for (const [platform, config] of Object.entries(appidConfig)) {
updateJsonFileField(manifestPath, [platform, 'appid'], config.appid);
}
updateJsonFileField(manifestPath, ['mp-toutiao', 'appid'], env.VITE_TOUTIAO_APPID);
return { return {
plugins: [ plugins: [
uni(), uni(),
], ],
define: {
__API__: JSON.stringify(env.VITE_API_BASE),
}
} }
}) })
Loading…
Cancel
Save