7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Webpack v4 の babel-loader で async / await に対応させ、 IE11 にも対応させる設定

Last updated at Posted at 2019-04-17

要件

  • Webpack で Babel をコンパイルしたい
    • async / await (ES2016+) に対応
    • IE11 にも対応 (polyfill)

install

npm install webpack babel-loader @babel/core @babel/preset-env @babel/polyfill core-js@2

書き方

設定ファイル

(ルールの箇所のみを抜粋)

webpack.config.js
{
    test: /\.js$/,
    use: [
      {
        loader: 'babel-loader',
        options: {
          presets: [
            [
              '@babel/preset-env',
              {
                modules: false,
                targets: {
                  node: 'current',
                  ie: 11
                },
                useBuiltIns: 'entry',
                corejs: 2
              }
            ]
          ]
        }
      }
    ]
  }

entryポイントのjs

(importの箇所のみを抜粋)

app.js
import '@babel/polyfill';

[追記]

@babel/polyfill is deprecated

@babel/polyfill は廃止予定らしい?!

今後は、@babel/polyfill を構成している
core-jsregenerator-runtime/runtimeを直接読み込んで対応していくっぽい。

app.js
// import '@babel/polyfill';
import 'core-js';
import 'regenerator-runtime/runtime';
webpack.config.js
{
    test: /\.js$/,
    use: [
      {
        loader: 'babel-loader',
        options: {
          presets: [
            [
              '@babel/preset-env',
              {
//              以下の設定は、無くても動いた
//                modules: false,
//                targets: {
//                  node: 'current',
//                  ie: 11
//                },
                useBuiltIns: 'entry',
                corejs: 2
              }
            ]
          ]
        }
      }
    ]
  }

参考: core-js@3 だと、IE11でエラーが出る模様。。
https://github.com/zloirock/core-js/issues/514#issuecomment-476364862

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?