nomadro/apps/taro/web/config/index.ts

147 lines
4.2 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'
/** Standalone Taro Web preview; mini-program builds never read this directory. */
const target = 'h5'
const devPort = Number(process.env.TARO_H5_PORT || 10087)
const previewApi = process.env.TARO_PREVIEW_API || 'http://127.0.0.1:10089'
const defaultApi = '/ebook-api'
const h5ViteProxy = {
'/ebook-api': {
target: previewApi,
changeOrigin: true,
secure: false,
rewrite: (p: string) => p.replace(/^\/ebook-api/, ''),
},
'/community-api': {
target: previewApi,
changeOrigin: true,
secure: false,
rewrite: (p: string) => p.replace(/^\/community-api/, '/api/v1'),
},
'/api': {
target: previewApi,
changeOrigin: true,
secure: false,
},
}
export default defineConfig(async (merge) => {
const baseConfig: UserConfigExport<'vite'> = {
projectName: 'nomadro-h5',
date: '2026-3-30',
designWidth: 750,
deviceRatio: {
640: 2.34 / 2,
750: 1,
375: 2,
828: 1.81 / 2,
},
sourceRoot: 'src',
outputRoot: 'dist',
plugins: ['@tarojs/plugin-framework-react'],
defineConstants: {
TARO_APP_API: JSON.stringify(defaultApi),
TARO_APP_COMMUNITY_API: JSON.stringify(
'/community-api',
),
TARO_APP_GATEWAY: JSON.stringify(
defaultApi,
),
TARO_APP_TARGET: JSON.stringify(target),
},
copy: {
patterns: [
{ from: 'src/assets/', to: 'assets/' },
{ from: path.resolve(__dirname, '..', 'src', 'public', 'icons'), to: 'icons' },
],
options: {},
},
framework: 'react',
compiler: {
type: 'vite',
vitePlugins: [
{
name: 'nomadro-spa-fallback',
config() {
return {
resolve: {
dedupe: ['react', 'react-dom', '@tarojs/plugin-framework-react', '@tarojs/runtime', '@tarojs/shared', '@tarojs/taro'],
alias: [{
find: /^.*(?:\/|^)@tarojs\/plugin-framework-react\/dist\/runtime(?:\.js)?$/,
replacement: path.join(path.dirname(require.resolve('@tarojs/plugin-framework-react/package.json')), 'dist/runtime.js'),
}],
},
optimizeDeps: { exclude: ['@tarojs/plugin-framework-react'], include: ['react', 'react-dom/client'] },
server: { strictPort: true },
}
},
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()
})
},
},
],
},
cache: { enable: false },
alias: {
'@': path.resolve(__dirname, '..', 'src'),
},
modifyViteConfig(viteConfig: {
publicDir?: string
appType?: string
server?: {
host?: string
port?: number
open?: boolean
proxy?: Record<string, unknown>
}
}) {
viteConfig.publicDir = path.resolve(__dirname, '..', 'src', 'public')
viteConfig.appType = 'spa'
// Taro 4 + vite runner 在部分场景不会读取 h5.devServer.proxy,
// 这里双写到 Vite server 配置,避免本地 /api/* 命中 404 导致白屏。
viteConfig.server = {
...(viteConfig.server || {}),
host: '0.0.0.0',
port: devPort,
open: false,
proxy: h5ViteProxy,
}
},
h5: {
publicPath: '/',
staticDirectory: 'static',
router: {
mode: 'hash',
},
devServer: {
host: '0.0.0.0',
port: devPort,
open: false,
proxy: h5ViteProxy,
},
postcss: {
autoprefixer: { enable: true },
cssModules: { enable: false },
// 对齐 Next:真实 px/rem,禁止 750 设计稿换算
pxtransform: {
enable: false,
},
},
},
}
if (process.env.NODE_ENV === 'development') {
return merge({}, baseConfig, { logger: { quiet: false, stats: true } })
}
return merge({}, baseConfig)
})