972bdde650
CI / verify (pull_request) Has been cancelled
Production builds were baking localhost:3000 after the local-dev default change, causing failed to fetch on product list. Co-authored-by: Cursor <cursoragent@cursor.com>
130 lines
3.9 KiB
TypeScript
130 lines
3.9 KiB
TypeScript
import { createRequire } from 'node:module';
|
||
import path from 'node:path';
|
||
import { defineConfig } from '@tarojs/cli';
|
||
|
||
/** 小程序/H5 请求的后端 origin(不含 /api);本地默认本机,生产构建默认远程 */
|
||
const API_ORIGIN =
|
||
process.env.VITE_API_TARGET ??
|
||
(process.env.NODE_ENV === 'production' ? 'https://dkapi.runxian.top' : 'http://localhost:3000');
|
||
|
||
const requireFromApp = createRequire(path.resolve(__dirname, '../package.json'));
|
||
|
||
/** 解析包目录/文件,避免 pnpm/Vite 把同一 runtime 打成两份 → useDidShow 读到空 reactMeta */
|
||
function resolvePkgFile(id: string): string {
|
||
return requireFromApp.resolve(id);
|
||
}
|
||
|
||
function resolvePkgDir(id: string): string {
|
||
return path.dirname(requireFromApp.resolve(`${id}/package.json`));
|
||
}
|
||
|
||
const TARO_FRAMEWORK_RUNTIME = resolvePkgFile('@tarojs/plugin-framework-react/dist/runtime.js');
|
||
const REACT_DIR = resolvePkgDir('react');
|
||
const REACT_DOM_DIR = resolvePkgDir('react-dom');
|
||
const TARO_RUNTIME_DIR = resolvePkgDir('@tarojs/runtime');
|
||
const TARO_SHARED_DIR = resolvePkgDir('@tarojs/shared');
|
||
|
||
export default defineConfig(async () => ({
|
||
projectName: 'mini-user',
|
||
date: '2026-7-12',
|
||
designWidth: 375,
|
||
deviceRatio: {
|
||
640: 2.34 / 2,
|
||
750: 1,
|
||
375: 2,
|
||
828: 1.81 / 2,
|
||
},
|
||
sourceRoot: 'src',
|
||
outputRoot: 'dist',
|
||
plugins: ['@tarojs/plugin-framework-react', '@tarojs/plugin-html'],
|
||
alias: {
|
||
// 指向包目录(不是 index.js),否则 react-dom/client 会变成 index.js/client
|
||
react: REACT_DIR,
|
||
'react-dom': REACT_DOM_DIR,
|
||
'@tarojs/runtime': TARO_RUNTIME_DIR,
|
||
'@tarojs/shared': TARO_SHARED_DIR,
|
||
'@tarojs/plugin-framework-react/dist/runtime': TARO_FRAMEWORK_RUNTIME,
|
||
'@tarojs/plugin-framework-react/dist/runtime.js': TARO_FRAMEWORK_RUNTIME,
|
||
},
|
||
defineConstants: {
|
||
TARO_APP_API_ORIGIN: JSON.stringify(API_ORIGIN),
|
||
},
|
||
copy: {
|
||
patterns: [
|
||
{ from: 'src/assets/', to: 'assets/' },
|
||
],
|
||
options: {},
|
||
},
|
||
framework: 'react',
|
||
compiler: {
|
||
type: 'vite',
|
||
vitePlugins: [
|
||
{
|
||
name: 'dukang-mini-user-vite-memory',
|
||
config() {
|
||
return {
|
||
// H5 Vite watch 预构建 Stencil 组件时峰值极易 >8GB;noDiscovery 降低冷启动成本
|
||
optimizeDeps: {
|
||
noDiscovery: true,
|
||
include: ['react', 'react-dom', 'react/jsx-runtime'],
|
||
},
|
||
resolve: {
|
||
// 防止 reactMeta(useDidShow 依赖)在 vendors 里出现两份
|
||
dedupe: [
|
||
'react',
|
||
'react-dom',
|
||
'@tarojs/runtime',
|
||
'@tarojs/shared',
|
||
'@tarojs/plugin-framework-react',
|
||
],
|
||
},
|
||
build: {
|
||
sourcemap: false,
|
||
},
|
||
};
|
||
},
|
||
},
|
||
],
|
||
},
|
||
cache: {
|
||
enable: true,
|
||
},
|
||
mini: {
|
||
/** dev:weapp 预览模式需开启,否则 React hooks 在 Vite 下会失效 */
|
||
debugReact: process.env.NODE_ENV !== 'production',
|
||
postcss: {
|
||
pxtransform: { enable: true, config: {} },
|
||
cssModules: { enable: false },
|
||
},
|
||
},
|
||
h5: {
|
||
// 生产发版由 remote-release 注入 TARO_H5_PUBLIC_PATH=/user/、TARO_H5_ROUTER_BASENAME=/user
|
||
// 本地预览勿强制 basename,否则静态托管下易白屏
|
||
publicPath: process.env.TARO_H5_PUBLIC_PATH || '/',
|
||
...(process.env.TARO_H5_ROUTER_BASENAME
|
||
? {
|
||
router: {
|
||
mode: 'browser' as const,
|
||
basename: process.env.TARO_H5_ROUTER_BASENAME,
|
||
},
|
||
}
|
||
: {}),
|
||
staticDirectory: 'static',
|
||
devServer: {
|
||
port: 5177,
|
||
host: '0.0.0.0',
|
||
proxy: {
|
||
'/api': {
|
||
target: API_ORIGIN,
|
||
changeOrigin: true,
|
||
},
|
||
},
|
||
},
|
||
postcss: {
|
||
autoprefixer: { enable: true, config: {} },
|
||
pxtransform: { enable: true, config: {} },
|
||
cssModules: { enable: false },
|
||
},
|
||
},
|
||
}));
|