0
0

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.

`@babel/preset-env` を useBuiltIns: 'usage' でコンパイルしたjsをAndroid5で見ると React 内部の Map, Set が undefined になる

Last updated at Posted at 2020-02-21

何が起きたか

  • Android 5 の標準ブラウザでReact動かそうとしたらMap Set がないと言われて動かなかった。
  • @babel/preset-env で polyfill してるから Map, Set はあるはずなのになぜ‥
babel.config.js
  ...
  presets: [
    [require('@babel/preset-env').default,
      {
        forceAllTransforms: true,
        targets: {
          ie: '11',
          android: '5'
        },
        debug: true,
        useBuiltIns: 'usage',
        corejs: 3,
        modules: false
      },
      ...

原因

  • @babel/preset-envuseBuiltIns: 'usage' はコードを解析して必要なpolyfillのみを読み込んでくれる。
  • babel 7 はデフォルトで node_modules 以下をコンパイルの対象外としてる
  • これにより、React 内部で使われてる MapSet が polyfill されずに例外になってた。

We also exclude node_modules by default and only look in the root unless you opt-in to setting an array of the .babelrcRoots option such as "babelrcRoots": [".", "node_modules/pkgA"]

対応1

babel.config.js
  ...
  useBuiltIns: 'entry'
  ...

にして、エントリーポイントで

index.jsなど
import "core-js/stable";
import "regenerator-runtime/runtime";

する。問題としてはファイルサイズがでかくなる。

対応2

babel.config.js
  ...
  useBuiltIns: 'usage'
  ...

のまま、エントリーポイントで

index.jsなど
import "core-js/modules/es.map";
import "core-js/modules/es.set";

する。問題としてはほかにnode_modulesでpolyfillが必要なものが使われてたら例外になるので怖い。

対応3

node_modules もコンパイルする

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?