LoginSignup
12
8

More than 5 years have passed since last update.

react native v0.49で無効になったdynamic importについて

Last updated at Posted at 2017-10-04

2017.10.3にリリースされた、react-native v0.49の割と大きいbreaking change

Transformerror:... "require() must have a single string literal argument" 
  • この Transformerrorはα版で出るようになった
  • が、結局リリース版出ても出続けることがわかったので、調べた

Transformerrorの原因

// こういうのが、通らなくなった。静的解析時にstringでないとNG
require('moment/locale/' + name);

dynamic importのサポートが(いったん)終了した

  • react-nativeのbundlerである、metro-bundlerがdynamic importのサポートを切った
  • 影響範囲大きいが、計画的なものっぽいので、ケアされることはなさそう

metro-bundlerのdynamic import自体は、そのうち復活する

workaround

projectのdependencies

  • 汎用的な方法はない。
  • 自分が使ってるモジュールがTransformerrorを吐いているなら、パッチを当てたforkを指定するか、そのフォークを手元にforkするとか指定する他ない
- "moment": "latest",
+ "moment": "git+ssh://git@github.com/tqc/moment.git#no-dynamic-import"
  • 2017.10.12時点で、moment.jsはupdate済みなのでこれは実際には不要

dependenciesのdependencies

少し面倒なケース。yarnを使ってると苦労は軽減可能

  • 使用してるnpm pkgの依存pkgがdynamic importを使ってるケース
  • 例:react-native-gifted-chat
    • package.jsonで"moment": "2.18.1"と固定していた
      • Transformerrorを吐くversionが指定された
      • 数日前にfixされたので
  • yarnだとSelective dependency resolutionsを使うことで解決
//package.jsonに追記したら、通常通り。yarn install, yarn.lock生成, etc..
"resolutions": {
    "react-native-gifted-chat/moment": "latest"
}
12
8
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
12
8