webpack5 + babel 兼容 IE11 浏览器

webpack 配置

module.exports = {
	entry: {
      polyfill: ['core-js/stable'], // polyfill 必须
      index: resolveFile('../src/index.tsx'),
    },
    target: ['web', 'es5'], // 必须
    output: {
      publicPath: '', // fix: Webpack5 Automatic publicPath is not supported in this browser
      devtoolModuleFilenameTemplate: 'webpack:///[resource-path]?[loaders]',
      clean: true,
      environment: { // 必须
        // The environment supports arrow functions ('() => { ... }').
        arrowFunction: false,
        // The environment supports BigInt as literal (123n).
        bigIntLiteral: false,
        // The environment supports destructuring ('{ a, b } = obj').
        destructuring: false,
        // The environment supports an async import() function to import EcmaScript modules.
        dynamicImport: false,
        // The environment supports 'for of' iteration ('for (const x of array) { ... }').
        forOf: false,
        // The environment supports ECMAScript Module syntax to import ECMAScript modules (import ... from '...').
        module: false,
        // The environment supports optional chaining ('obj?.a' or 'obj?.()').
        optionalChaining: false,
        // The environment supports template literals.
        templateLiteral: false,
      },
    },
    module: {
      rules: [
        {
          test: /\.(ts|js)$/,
          include: [path.resolve('src'), path.resolve('node_modules', 'usehooks-ts')], // 有些第三方库是基于 es6 的,所以需要对第三方库进行编译
          exclude: /assets/,
          use: {
            loader: 'babel-loader',
            options: {
              plugins: [isDev && require.resolve('react-refresh/babel')].filter(Boolean),
            },
          },
        },
        {
          test: /\.tsx?$/,
          exclude: /node_modules/,
          use: [
            {
              loader: require.resolve('ts-loader'),
              options: {
                getCustomTransformers: () => ({
                  before: [isDev && ReactRefreshTypeScript()].filter(Boolean),
                }),
                transpileOnly: isDev,
              },
            },
          ],
        },
      ],
    },
    // ... 其他配置
}

babel 配置

babel.config.js 文件

module.exports = {
  presets: [
    [
      '@babel/preset-env',
      {
        useBuiltIns: 'entry',
        targets: {
          browsers: ['ie 11'],
        },
        corejs: {
          version: 3,
          proposals: false,
        },
      },
    ],
    '@babel/preset-react',
    '@babel/preset-typescript',
  ],
  plugins: ['@babel/plugin-transform-runtime'],
};

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值