nomadro/apps/miniprogram/config/index.ts
eric 1d3e513ae2 Ship Douyin tt/ miniprogram: offline reader, clipboard privacy, README.
Add apps/miniprogram/tt as a standalone Douyin app with official usePrivacyCheck + tt.showModal/setClipboardData flow, single-chapter offline reader, and document console privacy setup for clipboard.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-30 15:17:52 +08:00

197 lines
6.6 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'
/** 本目录仅微信 + H5;抖音见 tt/,小红书见 xhs/ */
const outputRoot = isH5 ? 'dist/h5' : 'dist/weapp'
const defaultApi = '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) ||
/[\\/]components[\\/]WechatOaModal\.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')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { execSync } = require('child_process')
const miniRoot = path.resolve(__dirname, '..')
chain.plugin('nomadro-copy-icons').use(CopyWebpackPlugin, [
{
patterns: [
{
from: path.resolve(__dirname, '..', 'src', 'public', 'icons'),
to: 'icons',
noErrorOnMissing: true,
},
],
},
])
// watch/build 每次产出后:静态资源 + 分享开关(Taro 会丢掉 enableShare*)
chain.plugin('nomadro-after-emit-static').use(
class NomadroAfterEmitStatic {
apply(compiler: { hooks: { afterEmit: { tap: (n: string, cb: () => void) => void } } }) {
compiler.hooks.afterEmit.tap('NomadroAfterEmitStatic', () => {
try {
execSync('node ./scripts/copy-weapp-static.mjs', {
cwd: miniRoot,
stdio: 'inherit',
})
} catch (e) {
console.warn('[nomadro-after-emit-static]', (e as Error).message)
}
})
}
},
)
},
// 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)
})