Help us understand the problem. What is going on with this article?

babelでトランスパイルしたときに、this を void 0 に変えてしまう設定を無効化したメモ

More than 1 year has passed since last update.

現象

  • elmの吐き出したjsをbabelにかけたら動作しなくなった
  • (this)void 0 に変更されていたのが原因。

対応

babel.config.jsにoverridesオプションをつけて対応

babel.config.js-before
const presets = [
  [
    "@babel/env",
    {
      targets: {
        chrome: "70"
      },
      useBuiltIns: "entry"
    },
  ],
];

module.exports = { presets };
babel.config.js-after
const presets = [
  [
    "@babel/env",
    {
      targets: {
        chrome: "70"
      },
      useBuiltIns: "entry"
    },
  ],
];
const overrides = [{
  test: "./src/assets/elm/ElmTest.js",
  sourceType: "script",
}];
module.exports = { presets, overrides };

結果

src/assets/elm/ElmTest.js-before
// 省略
  _Platform_export({
    'Main': {
      'init': author$project$Main$main(elm$json$Json$Decode$succeed(0))(0)
    }
  });
})(void 0);
src/assets/elm/ElmTest.js-after
// 省略
  _Platform_export({
    'Main': {
      'init': author$project$Main$main(elm$json$Json$Decode$succeed(0))(0)
    }
  });
})(this);

参考

stack over flow: how to stop babel from transpiling this to undefined
stack over flow: how to properly overrides
babel overrides

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away