| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- import { defineConfig, type UserConfigExport } from '@tarojs/cli';
- import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
- import devConfig from './dev';
- import prodConfig from './prod';
- import path from 'path';
- import TerserPlugin from 'terser-webpack-plugin';
- import { DefinePlugin } from 'webpack';
- // https://taro-docs.jd.com/docs/next/config#defineconfig-辅助函数
- export default defineConfig<'webpack5'>(async (merge) => {
- const baseConfig: UserConfigExport<'webpack5'> = {
- projectName: 'zsis',
- date: '2025-5-29',
- designWidth: 750,
- deviceRatio: {
- 640: 2.34 / 2,
- 750: 1,
- 375: 2,
- 828: 1.81 / 2,
- },
- sourceRoot: 'src',
- outputRoot: 'dist',
- plugins: ['@tarojs/plugin-http'],
- defineConstants: {
- // 开发环境下使用空字符串,实际请求会自动拼接当前域名
- API_BASE_URL_FROM_WEBPACK:
- process.env.NODE_ENV === 'development'
- ? '""'
- : (`"${process.env.TARO_API_URL}"`),
- MQTT_BROKER_URL_FROM_WEBPACK: `"${process.env.TARO_MQTT_URL}"`,
- },
- copy: {
- patterns: [],
- options: {},
- },
- framework: 'react',
- compiler: 'webpack5',
- // ✅ 优化:启用 Webpack 持久化缓存(在 webpackChain 中配置)
- cache: {
- enable: true, // Taro 层面启用缓存
- },
- mini: {
- postcss: {
- pxtransform: {
- enable: true,
- config: {},
- },
- cssModules: {
- enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
- config: {
- namingPattern: 'module', // 转换模式,取值为 global/module
- generateScopedName: '[name]__[local]___[hash:base64:5]',
- },
- },
- },
- webpackChain(chain) {
- // ✅ 优化:配置 Webpack 持久化缓存
- chain.cache({
- type: 'filesystem',
- cacheDirectory: path.resolve(__dirname, '../node_modules/.cache/webpack-mini'),
- buildDependencies: {
- config: [
- __filename,
- path.resolve(__dirname, './prod.ts'),
- path.resolve(__dirname, './dev.ts'),
- ],
- },
- name: `mini-${process.env.NODE_ENV || 'development'}`,
- version: '1.0.0',
- });
- chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin);
- },
- },
- h5: {
- esnextModules: [],
- publicPath: './',
- staticDirectory: 'static',
- output: {
- filename: '[name].js',
- chunkFilename: '[name].[chunkhash:8].js',
- },
- miniCssExtractPluginOption: {
- ignoreOrder: true,
- filename: 'css/[name].[fullhash].css',
- chunkFilename: 'css/[name].[chunkhash].css',
- },
- postcss: {
- autoprefixer: {
- enable: true,
- config: {},
- },
- cssModules: {
- enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
- config: {
- namingPattern: 'module', // 转换模式,取值为 global/module
- generateScopedName: '[name]__[local]___[fullhash:base64:5]',
- },
- },
- },
- webpackChain(chain) {
- /* 1️⃣ 排除 svg(防止被 base64)这是为了把svg解析为组件 ,用于svg icon*/
- if (chain.module.rules.has('image')) {
- chain.module
- .rule('image')
- .exclude.add(/\.svg$/)
- }
- if (chain.module.rules.has('media')) {
- chain.module
- .rule('media')
- .exclude.add(/\.svg$/)
- }
- if (chain.module.rules.has('asset')) {
- chain.module
- .rule('asset')
- .exclude.add(/\.svg$/)
- }
- chain.module
- .rule('svgr-icons')
- .before('image')
- .test(/\.svg$/)
- .include.add(
- path.resolve(__dirname, '../src/assets/Icons')
- )
- .end()
- .use('svgr')
- .loader('@svgr/webpack')
- .options({
- dimensions: false,
- icon: false,
- });
- // chain.module
- // .rule('svgr')
- // .test(/\.svg$/)
- // .include.add(
- // path.resolve(__dirname, '../src/assets/Icons')
- // )
- // .when(
- // true,
- // (rule) => {
- // rule
- // .loader('@svgr/webpack')
- // .options({
- // dimensions: false,
- // icon: false,
- // });
- // }
- // );
- // ⚡ 优化:只在生产环境编译 node_modules(开发环境跳过,提升 20-30% 构建速度)
- // 2025.12.12 这一段解决的问题是在雷神模拟器上运行android时不支持es2020语法的问题
- if (process.env.NODE_ENV === 'production') {
- chain.module
- .rule('force-es5')
- .test(/\.(js|mjs)$/)
- .include.add([
- /node_modules[\\/]ahooks/,
- /node_modules[\\/]lodash-es/,
- /node_modules[\\/]dayjs/,
- /node_modules[\\/]antd-mobile/,
- /node_modules[\\/]@ant-design/,
- /node_modules[\\/]@cornerstonejs/,
- /node_modules[\\/]react-query/,
- // ↑ 把自己项目里报错或输出 const/let 的包加进来就行
- ])
- .end()
- .use('babel-loader')
- .loader('babel-loader')
- .options({
- presets: [
- ['@babel/preset-env', {
- targets: { chrome: '91' }, // 或者 "iOS >= 9, Android >= 5"
- useBuiltIns: 'usage',
- corejs: 3,
- }]
- ]
- });
- }
- // ✅ 优化:配置 Webpack 持久化缓存
- chain.cache({
- type: 'filesystem',
- cacheDirectory: path.resolve(__dirname, '../node_modules/.cache/webpack-h5'),
- buildDependencies: {
- config: [
- __filename,
- path.resolve(__dirname, './prod.ts'),
- path.resolve(__dirname, './dev.ts'),
- ],
- },
- name: `h5-${process.env.NODE_ENV || 'development'}`,
- version: '1.0.0',
- });
- chain.output.path(path.resolve(__dirname, '../dist/h5'));
- chain.optimization.minimize(false); // 彻底关闭压缩
- // chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin);
- chain.resolve.plugin('tsconfig-paths').use(TsconfigPathsPlugin, [
- {
- configFile: path.resolve(__dirname, '../tsconfig.json'), // 必须指向存在的 tsconfig
- },
- ]);
- // 打开详细日志
- //chain.mode('production').stats('verbose');
- chain.merge({
- resolve: {
- fallback: {
- util: require.resolve('util/'),
- stream: require.resolve('stream-browserify'),
- fs: require.resolve('browserify-fs'),
- path: require.resolve('path-browserify'),
- },
- },
- });
- // ✅ 优化:启用 Terser 并行处理
- chain.optimization.minimizer('terser').use(TerserPlugin, [
- {
- parallel: true, // 多线程并行,提升构建速度
- terserOptions: {
- compress: false, // 先不压缩,只混淆
- mangle: false,
- format: {
- comments: false,
- },
- },
- // eslint-disable-next-line
- } as any]);
- chain
- .plugin('define-plugin')
- // eslint-disable-next-line
- .use(DefinePlugin as any, [{
- 'process.env.USE_MSW': JSON.stringify(
- process.env.USE_MSW || 'false'
- ),
- 'process.env.NODE_ENV': JSON.stringify(
- process.env.NODE_ENV || 'production'
- ),
- },
- ]);
- },
- },
- rn: {
- appName: 'taroDemo',
- postcss: {
- cssModules: {
- enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
- },
- },
- },
- };
- if (process.env.NODE_ENV === 'development') {
- // 本地开发构建配置(不混淆压缩)
- return merge({}, baseConfig, devConfig);
- }
- // 生产构建配置(默认开启压缩混淆等)
- return merge({}, baseConfig, prodConfig);
- });
|