webpack,babel配置项目适配IE11

package.json

"browserslist": [
    "ie 11"
  ]

 

webpack.config.js

const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')

module.exports = {
  entry: './src/index.ts',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
    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,
    }
  },
  target: ['web', 'es5'], // 必须
  module: {
    rules: [
      {
        test: /\.(ts|js)$/,
        exclude: /node_modules/, // 排除依赖里的代码
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [
                [
                  '@babel/preset-env',
                  {
                    useBuiltIns: 'usage',
                    corejs: '3.35.0'
                  }
                ]
              ],
              plugins: ['@babel/plugin-transform-runtime']
            }
          },
          'ts-loader'
        ]
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new htmlWebpackPlugin()
  ],
  resolve: {
    extensions: ['.ts', '.js']
  }
}

 

posted @ 2024-01-15 18:10  yaokunlun  阅读(160)  评论(0编辑  收藏  举报