Просмотр исходного кода

fix: 修复小程序开发版预览二维码生成失败问题

- 发布脚本: 开发版默认跳过构建,使用 watch 模式现有输出
- 发布脚本: 根据类型动态选择构建输出路径(development/production)
- 发布脚本: 预览设置 useProjectConfig=false 避免 terser 问题
- 企业小程序: 添加 ES5 转译配置,修复可选链语法导致预览失败

Generated with [Claude Code](https://claude.com/claude-code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
yourname 1 день назад
Родитель
Сommit
cc204321e6
2 измененных файлов с 59 добавлено и 12 удалено
  1. 50 10
      .claude/skills/weapp-publish/scripts/publish-weapp.js
  2. 9 2
      mini/config/dev.ts

+ 50 - 10
.claude/skills/weapp-publish/scripts/publish-weapp.js

@@ -10,26 +10,42 @@ const PUBLIC_HOST = WEB_HOST.startsWith('http') ? WEB_HOST : `https://${WEB_HOST
 // 项目根目录(从 scripts/ 目录向上 4 级)
 // 项目根目录(从 scripts/ 目录向上 4 级)
 const PROJECT_ROOT = path.resolve(__dirname, '../../../../')
 const PROJECT_ROOT = path.resolve(__dirname, '../../../../')
 
 
-// 小程序配置
+// 小程序配置基础信息
 const MINI_CONFIGS = {
 const MINI_CONFIGS = {
   enterprise: {
   enterprise: {
     name: '企业小程序',
     name: '企业小程序',
     appid: 'wx1e791ed2e0229eb8',
     appid: 'wx1e791ed2e0229eb8',
-    projectPath: path.resolve(PROJECT_ROOT, 'mini/dist/weapp/production'),
+    dir: 'mini',
     privateKeyPath: path.resolve(PROJECT_ROOT, 'mini/certs/private.upload.key'),
     privateKeyPath: path.resolve(PROJECT_ROOT, 'mini/certs/private.upload.key'),
-    buildCmd: 'cd mini && pnpm build:weapp',
     distPath: 'dist/weapp',
     distPath: 'dist/weapp',
   },
   },
   talent: {
   talent: {
     name: '人才小程序',
     name: '人才小程序',
     appid: 'wx3c47dbce1ea7d43c',
     appid: 'wx3c47dbce1ea7d43c',
-    projectPath: path.resolve(PROJECT_ROOT, 'mini-talent/dist/weapp/production'),
+    dir: 'mini-talent',
     privateKeyPath: path.resolve(PROJECT_ROOT, 'mini-talent/certs/private.upload.key'),
     privateKeyPath: path.resolve(PROJECT_ROOT, 'mini-talent/certs/private.upload.key'),
-    buildCmd: 'cd mini-talent && pnpm build:weapp',
     distPath: 'dist/weapp',
     distPath: 'dist/weapp',
   },
   },
 }
 }
 
 
+/**
+ * 获取构建配置(根据 action 类型动态选择)
+ */
+function getBuildConfig(config, action) {
+  const isDev = action === 'dev'
+  const mode = isDev ? 'development' : 'production'
+
+  // 开发版和体验版都设置 NODE_ENV=development(使用 .env.development 配置)
+  // Taro build 默认不会进入 watch 模式(除非加 --watch 参数)
+  const buildCmd = `cd ${config.dir} && NODE_ENV=development pnpm taro build --type weapp`
+
+  return {
+    ...config,
+    projectPath: path.resolve(PROJECT_ROOT, `${config.dir}/${config.distPath}/${mode}`),
+    buildCmd,
+  }
+}
+
 /**
 /**
  * 构建小程序
  * 构建小程序
  */
  */
@@ -123,13 +139,22 @@ async function previewDevProject(config, options) {
     await ci.preview({
     await ci.preview({
       project,
       project,
       desc,
       desc,
-      setting: { useProjectConfig: true },
+      setting: {
+        useProjectConfig: false,  // 不使用项目配置,完全使用下方自定义设置
+        es6: false,
+        enhance: false,
+        minified: false,  // 禁用代码压缩
+        minifyWXSS: false,  // 禁用样式压缩
+        minifyWXML: false,  // 禁用模板压缩
+        codeProtect: false,
+      },
       qrcodeFormat: 'image',
       qrcodeFormat: 'image',
       qrcodeOutputDest: qrcodePath,
       qrcodeOutputDest: qrcodePath,
       onProgressUpdate: (progress) => {
       onProgressUpdate: (progress) => {
         process.stdout.write(`\r   生成进度: ${progress}%`)
         process.stdout.write(`\r   生成进度: ${progress}%`)
       },
       },
       robot,
       robot,
+      uploadType: 'preview',
     })
     })
 
 
     // 公网访问 URL
     // 公网访问 URL
@@ -155,13 +180,26 @@ async function previewDevProject(config, options) {
  */
  */
 async function main() {
 async function main() {
   const args = process.argv.slice(2)
   const args = process.argv.slice(2)
+
+  // 开发版默认跳过构建(使用已有的 watch 模式构建输出)
+  // 可以通过 --build 参数强制重新构建
+  const forceBuildIndex = args.indexOf('--build')
+  const forceBuild = forceBuildIndex !== -1
+  if (forceBuild) {
+    args.splice(forceBuildIndex, 1)
+  }
+
   const noBuildIndex = args.indexOf('--no-build')
   const noBuildIndex = args.indexOf('--no-build')
   const noBuild = noBuildIndex !== -1
   const noBuild = noBuildIndex !== -1
   if (noBuild) {
   if (noBuild) {
     args.splice(noBuildIndex, 1)
     args.splice(noBuildIndex, 1)
   }
   }
+
   const [miniType, action = 'experience'] = args
   const [miniType, action = 'experience'] = args
 
 
+  // 开发版默认跳过构建(除非指定 --build)
+  const shouldSkipBuild = action === 'dev' && !forceBuild
+
   // 解析参数
   // 解析参数
   const options = {
   const options = {
     version: process.env.VERSION || '1.0.0',
     version: process.env.VERSION || '1.0.0',
@@ -175,14 +213,16 @@ async function main() {
     process.exit(1)
     process.exit(1)
   }
   }
 
 
-  const config = MINI_CONFIGS[miniType]
+  // 根据类型和动作获取构建配置
+  const config = getBuildConfig(MINI_CONFIGS[miniType], action)
 
 
   try {
   try {
-    // 构建项目(除非使用 --no-build
-    if (!noBuild) {
+    // 构建项目(跳过或显式指定构建时
+    if (!shouldSkipBuild && !noBuild) {
       buildMiniProject(config)
       buildMiniProject(config)
     } else {
     } else {
       console.log(`\n⏭️ 跳过构建,使用现有构建输出`)
       console.log(`\n⏭️ 跳过构建,使用现有构建输出`)
+      console.log(`   使用路径: ${config.projectPath}`)
     }
     }
 
 
     // 执行操作
     // 执行操作
@@ -197,7 +237,7 @@ async function main() {
       console.error(`❌ 操作无效: ${action}`)
       console.error(`❌ 操作无效: ${action}`)
       process.exit(1)
       process.exit(1)
     }
     }
-  } catch (error) {
+  } catch (_error) {
     process.exit(1)
     process.exit(1)
   }
   }
 }
 }

+ 9 - 2
mini/config/dev.ts

@@ -5,7 +5,14 @@ export default {
     quiet: false,
     quiet: false,
     stats: true
     stats: true
   },
   },
-  mini: {},
+  mini: {
+    compile: {
+      include: [
+        // 确保产物为 es5,转译所有 node_modules(除了 Babel 核心)
+        filename => /node_modules\/(?!(@babel|core-js|style-loader|css-loader|react|react-dom))/.test(filename)
+      ]
+    }
+  },
   h5: {
   h5: {
     devServer: {
     devServer: {
       port: 10086,
       port: 10086,
@@ -19,7 +26,7 @@ export default {
       },
       },
       open: false
       open: false
     },
     },
-    webpackChain(chain, webpack) {  
+    webpackChain(chain) {  
       // 确保在 HtmlWebpackPlugin 之后添加  
       // 确保在 HtmlWebpackPlugin 之后添加  
       chain  
       chain  
         .plugin('iframeCommunicationPlugin')  
         .plugin('iframeCommunicationPlugin')