c8ea5a3119
Replace fetch with wx/Taro.request in client-logging for WeChat mini program; point production build to api.dukanghaoke.com. Co-authored-by: Cursor <cursoragent@cursor.com>
135 lines
4.0 KiB
TypeScript
135 lines
4.0 KiB
TypeScript
import { createRequire } from 'node:module';
|
||
import path from 'node:path';
|
||
import { defineConfig } from '@tarojs/cli';
|
||
|
||
/** watch / --mode development 视为本地联调;其余(含 build:weapp)走生产 */
|
||
const isDevMode =
|
||
process.env.NODE_ENV === 'development' ||
|
||
process.argv.includes('--watch') ||
|
||
process.argv.includes('development');
|
||
|
||
/** 小程序/H5 请求的后端 origin(不含 /api);本地默认本机,生产构建默认远程 */
|
||
const API_ORIGIN =
|
||
process.env.VITE_API_TARGET ??
|
||
(isDevMode ? 'http://localhost:3000' : 'https://api.dukanghaoke.com');
|
||
|
||
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: isDevMode,
|
||
postcss: {
|
||
pxtransform: { enable: true, config: {} },
|
||
cssModules: { enable: false },
|
||
},
|
||
},
|
||
h5: {
|
||
// staging:user-test.dukanghaoke.com 根路径;生产:remote-release 注入 /user/
|
||
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 },
|
||
},
|
||
},
|
||
}));
|