nomadro/apps/miniprogram/config/index.ts
eric 69b0bd8224 Ship Taro H5 Vite build and Next-parity hub/nav/book/vip pages.
Switch miniprogram H5 to Vite for fast local compile; align portal pages and static icons for cross-machine continue.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-29 04:36:12 -05:00

153 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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,
},
}
const pxExclude = (file: string) =>
/[\\/]styles[\\/]hub\.scss/.test(file) ||
/[\\/]styles[\\/]navmini\.scss/.test(file) ||
/[\\/]styles[\\/]ebook-cover\.scss/.test(file) ||
/[\\/]pages[\\/]index[\\/]index\.scss/.test(file) ||
/[\\/]pages[\\/]nav[\\/]index\.scss/.test(file) ||
/[\\/]pages[\\/]book[\\/]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/' },
{ from: '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'),
},
// 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: {} },
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 },
pxtransform: {
enable: true,
config: {
exclude: pxExclude,
},
},
},
},
}
if (process.env.NODE_ENV === 'development') {
return merge({}, baseConfig, { logger: { quiet: false, stats: true } })
}
return merge({}, baseConfig)
})