umijs 开发优化和生产优化

.umirc.ts

import { defineConfig } from ‘umi‘;

import dev from ‘./config/dev‘;
import prod from ‘./config/prod‘;

console.log(process.env.NODE_ENV);
const envConfig = ‘development‘ === process.env.NODE_ENV ? dev : prod;

export default defineConfig({
  webpack5: {}, // 使用webpack 5
  nodeModulesTransform: {
    type: ‘none‘,
  },
  routes: [{ path: ‘/‘, component: ‘@/pages/index‘ }],
  fastRefresh: {},
  ...envConfig,
});

config/dev

开发时使用chunk缓存减少打包时间

import { defineConfig } from ‘umi‘;

export default defineConfig({
  hash: false,
  devServer: {
    // writeToDisk: true,
  },
  chunks: [‘vendors‘, ‘umi‘],
  chainWebpack: function (config, { webpack }) {
    config.merge({
      optimization: {
        moduleIds: ‘deterministic‘,
        splitChunks: {
          cacheGroups: {
            vendor: {
              test: /[\\/]node_modules[\\/]/,
              name: ‘vendors‘,
              chunks: ‘all‘,
            },
          },
        },
      },
    });
  },
});

config/prod

打包生产时 能用cdn的就用cdn

import { defineConfig } from ‘umi‘;
import * as pkg from ‘../package.json‘;

const externalsConfig: any = {
  lodash: {
    name: ‘_‘,
    cdn: (version: string) =>
      `https://cdnjs.cloudflare.com/ajax/libs/react/${version}/umd/react.production.min.js`,
  },
  react: {
    name: ‘React‘,
    cdn: (version: string) =>
      `https://cdnjs.cloudflare.com/ajax/libs/react-dom/${version}/umd/react-dom.production.min.js`,
  },
  ‘react-dom‘: {
    name: ‘ReactDOM‘,
    cdn: (version: string) =>
      `https://cdnjs.cloudflare.com/ajax/libs/lodash.js/${version}/lodash.min.js`,
  },
};

const externals: any = {};
const scripts: string[] = [];
for (const pname in externalsConfig) {
  externals[pname] = externalsConfig[pname].name;
  if (pname in pkg.dependencies) {
    const version = (pkg.dependencies as any)[pname].replace(/^\D/, ‘‘);
    scripts.push(externalsConfig[pname].cdn(version));
  }
}

export default defineConfig({
  hash: true,
  chainWebpack: function (config, { webpack }) {
    config.merge({
      optimization: {
        minimize: true,
        usedExports: true,
      },
    });
  },
  externals,
  scripts,
});

umijs 开发优化和生产优化

上一篇:Core Python:Python中继承特性的简述


下一篇:记录一次Http Get请求遇到的坑,请求返回Server returned HTTP response code: 400 for URL