Webpack4+MobX State Tree+Babel环境下IE11报Object.assign错误,选哪个Babel插件?
Hey there! Let's work through this IE11 error together. First, the root cause is clear: IE11 doesn't natively support Object.assign, and the error is popping up in mobx-state-tree's library code (not your own source files)—that's a critical detail here.
两者的核心区别
babel-plugin-transform-object-assign
This plugin only transformsObject.assigncalls in your own project code, converting them into compatibility-friendly equivalent logic. However, it won't touch code insidenode_modules(like mobx-state-tree's files). Since your error comes from a third-party library, this plugin can't fix the problem for you.babel-polyfill
It works by adding missing JavaScript APIs to the global environment, includingObject.assign. Once you include it, every piece of code—whether yours or a third-party library's—will use this polyfilled implementation when callingObject.assign. This is exactly what you need here, because the error originates from mobx-state-tree's code.
推荐解决方案
Go with babel-polyfill (or the more modern core-js, which is the recommended approach for Babel 7+):
- Install the dependency:
npm install --save babel-polyfill - Import it at the very top of your project's entry file (like
index.js):import 'babel-polyfill';
For Babel 7+ users, a better practice is to use core-js with @babel/preset-env to auto-include only the polyfills IE11 needs:
- Install
core-js:npm install --save core-js@3 - Update your Babel config:
This setup will automatically inject required polyfills (including{ "presets": [ [ "@babel/preset-env", { "targets": { "ie": "11" }, "useBuiltIns": "usage", "corejs": 3 } ] ] }Object.assign) based on your target browser, no manual import needed.
总结
Since your error comes from a third-party library, babel-plugin-transform-object-assign can't help—it only processes your own code. You need babel-polyfill (or core-js) to globally patch the missing Object.assign API, so IE11 can run mobx-state-tree correctly.
内容的提问来源于stack exchange,提问作者chobo2

