import path from 'path' import { defineConfig, type UserConfigExport } from '@tarojs/cli' const target = process.env.TARO_APP_TARGET || process.env.TARO_ENV || 'weapp' const isH5 = target === 'h5' const outputRoot = target === 'h5' ? 'dist/h5' : target === 'tt' ? 'dist/tt' : target === 'xhs' ? 'dist/xhs' : 'dist/weapp' const defaultApi = target === 'tt' ? 'https://dyapi.nomadro.cn' : 'https://api.nomadro.cn' const iconsDir = path.resolve(__dirname, '..', 'src', 'assets', 'icons') /** Vite H5 代理(比 webpack 快一个数量级) */ const h5ViteProxy = { '/ebook-api': { target: process.env.TARO_H5_EBOOK_PROXY || 'https://api.nomadro.cn', changeOrigin: true, secure: false, rewrite: (p: string) => p.replace(/^\/ebook-api/, ''), }, '/community-api': { target: process.env.TARO_H5_COMMUNITY_PROXY || 'https://nomadweb.nomadro.com', changeOrigin: true, secure: false, rewrite: (p: string) => p.replace(/^\/community-api/, '/api/v1'), }, '/api': { target: process.env.TARO_H5_PROXY || 'https://nomadro.cn', changeOrigin: true, secure: false, }, } /** H5 视觉 SoT:这些表用真实 px/rem,小程序端禁止 750→rpx(否则布局塌成蚂蚁字) */ const pxExclude = (file: string) => /[\\/]styles[\\/]hub\.scss/.test(file) || /[\\/]styles[\\/]navmini\.scss/.test(file) || /[\\/]styles[\\/]ebook-cover\.scss/.test(file) || /[\\/]styles[\\/]community\.scss/.test(file) || /[\\/]styles[\\/]community-navbar\.scss/.test(file) || /[\\/]styles[\\/]portal-nav-h5\.scss/.test(file) || /[\\/]styles[\\/]vip-landing\.scss/.test(file) || /[\\/]styles[\\/]h5-theme\.scss/.test(file) || /[\\/]styles[\\/]weapp-parity\.scss/.test(file) || /[\\/]styles[\\/]page-rem\.scss/.test(file) || /[\\/]pages[\\/]index[\\/]index\.scss/.test(file) || /[\\/]pages[\\/]nav[\\/]index\.scss/.test(file) || /[\\/]pages[\\/]book[\\/]index\.scss/.test(file) || /[\\/]pages[\\/]community[\\/]index\.scss/.test(file) || /[\\/]pages[\\/]vip[\\/]index\.scss/.test(file) export default defineConfig(async (merge) => { const baseConfig: UserConfigExport = { projectName: 'nomadro-miniprogram', date: '2026-3-28', designWidth: 750, deviceRatio: { 640: 2.34 / 2, 750: 1, 375: 2, 828: 1.81 / 2, }, sourceRoot: 'src', outputRoot, plugins: ['@tarojs/plugin-framework-react'], defineConstants: { TARO_APP_API: JSON.stringify(process.env.TARO_APP_API || defaultApi), TARO_APP_COMMUNITY_API: JSON.stringify( process.env.TARO_APP_COMMUNITY_API || 'https://nomadweb.nomadro.com/api/v1', ), TARO_APP_GATEWAY: JSON.stringify( process.env.TARO_APP_GATEWAY || process.env.TARO_APP_API || defaultApi, ), TARO_APP_TARGET: JSON.stringify(target), }, copy: { patterns: [ { from: 'src/assets/', to: 'assets/' }, // 导航图标:内置 webp → 小程序包根 /icons/* { from: path.resolve(__dirname, '..', 'src', 'public', 'icons'), to: 'icons' }, ], options: {}, }, framework: 'react', // H5 → Vite(快);微信/抖音 → webpack5 compiler: isH5 ? { type: 'vite', vitePlugins: [ { name: 'nomadro-spa-fallback', configureServer(server: { middlewares: { use: (fn: (req: { url?: string }, res: unknown, next: () => void) => void) => void } }) { server.middlewares.use((req, _res, next) => { if (req.url === '/' || req.url === '') { req.url = '/index.html' } next() }) }, }, ], } : { type: 'webpack5', prebundle: { enable: false }, }, cache: { enable: !isH5 }, alias: { '@': path.resolve(__dirname, '..', 'src'), }, // 微信构建强制把 icons 拷进输出根(双保险,配合 postbuild 脚本) modifyWebpackChain(chain) { if (isH5) return // eslint-disable-next-line @typescript-eslint/no-var-requires const CopyWebpackPlugin = require('copy-webpack-plugin') chain.plugin('nomadro-copy-icons').use(CopyWebpackPlugin, [ { patterns: [ { from: path.resolve(__dirname, '..', 'src', 'public', 'icons'), to: 'icons', noErrorOnMissing: true, }, ], }, ]) }, // Vite root=src,publicDir 默认 src/public → /icons/* modifyViteConfig(viteConfig: { publicDir?: string; appType?: string }) { if (!isH5) return viteConfig.publicDir = path.resolve(__dirname, '..', 'src', 'public') viteConfig.appType = 'spa' }, mini: { postcss: { pxtransform: { enable: true, config: { // 与 H5 同源的 Next/门户样式保持 px/rem;阅读器等 750 稿仍走换算 exclude: pxExclude, }, }, cssModules: { enable: false }, }, }, h5: { publicPath: '/', staticDirectory: 'static', router: { mode: 'hash', }, devServer: { host: '0.0.0.0', port: 10086, open: false, proxy: h5ViteProxy, }, postcss: { autoprefixer: { enable: true }, cssModules: { enable: false }, // H5 对齐 Next:用真实 px/rem,禁止 750 设计稿换算(否则 250px→6.25rem 地球塌掉) pxtransform: { enable: false, }, }, }, } if (process.env.NODE_ENV === 'development') { return merge({}, baseConfig, { logger: { quiet: false, stats: true } }) } return merge({}, baseConfig) })