學(xué)習(xí) babel 時(shí),遇到的問(wèn)題,使用舊版本 babel 命名規(guī)則安裝后運(yùn)行報(bào)錯(cuò),初步查找到原因是因?yàn)?babel 各個(gè)preset和plugin新舊不同版本之間存在兼容問(wèn)題,提示使用 npx babel-upgrade 可以自動(dòng)升級(jí),但是我升級(jí)失敗了,提示解析錯(cuò)誤,后來(lái)看到了這篇文章,問(wèn)題得以解決?,F(xiàn)在被我原封不動(dòng)的給扒了過(guò)來(lái)當(dāng)筆記(復(fù)制,主要還是懶 [手動(dòng)狗頭])
最近工作比較忙,有一段時(shí)間沒(méi)有寫(xiě)前端玩了。今天試著搭一個(gè)項(xiàng)目,發(fā)現(xiàn)各種坑,以前用起來(lái)非常好的配置文件各種報(bào)錯(cuò)。排查后發(fā)現(xiàn)原來(lái)babel
升級(jí)了一個(gè)大版本,已經(jīng)到7.X
了,這里我總結(jié)一下升級(jí)過(guò)程中踩到的坑。
Error: Cannot find module '@babel/core' babel-loader@8 requires Babel 7.x (the package '@babel/core'). If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'. at Function.Module._resolveFilename (module.js:547:15) at Function.Module._load (module.js:474:25) at Module.require (module.js:596:17) at require (internal/module.js:11:18) ....
沒(méi)找到@babel/core
,需要把babel-core
卸載掉,從新安裝@babel/core
npm un babel-core
npm i -D @babel/core
ERROR in ./src/index.jsx Module build failed (from ./node_modules/babel-loader/lib/index.js):Error: Plugin/Preset files are not allowed to export objects, only functions. ...
將babel-preset-*
卸載,重新安裝@babel/preset-*
,并且修改 .babelrc
中的 presets
比如我的
npm:- babel-preset-env+ @babel/preset-env- babel-preset-react+ @babel/preset-react- babel-preset-stage-0 .babelrc:- "presets": ["react", "env", "stage-0", "mobx"]+ "presets": ["@babel/preset-react", "@babel/preset-env", "mobx"]
除了上述的preset
,我還用了babel-preset-mobx
但是沒(méi)找到 @babel/preset-mobx
,從babel-preset-mobx git提交日志上看,作者已經(jīng)支持了最新的babel
。在之后的測(cè)試中,發(fā)現(xiàn)mobx
的功能也能正常使用。
另外,stage-*已棄用
ERROR in ./src/index.jsx Module build failed (from ./node_modules/babel-loader/lib/index.js):TypeError: this.setDynamic is not a functionat PluginPass.pre ...
這次是插件了,一樣把babel-plugin-*
卸載,重新安裝@babel/plugin-*
然后修改.babelrc
文件
具體的包名可以在 npm倉(cāng)庫(kù) 里找
.babelrc:
{"presets": ["@babel/preset-env", "@babel/preset-react", "mobx"],"plugins": ["@babel/plugin-proposal-object-rest-spread","@babel/plugin-transform-runtime" ] }
package.json:
"devDependencies": {"@babel/core": "^7.1.0","@babel/plugin-proposal-object-rest-spread": "^7.0.0","@babel/plugin-transform-runtime": "^7.1.0","@babel/preset-env": "^7.1.0","@babel/preset-react": "^7.0.0","babel-loader": "^8.0.2""babel-preset-mobx": "^2.0.0", ... }, "dependencies": {"@babel/runtime": "^7.0.0", ... }
這次升級(jí),功能上有什么變化我就不在這里寫(xiě)了,大家可以自行搜索
總的來(lái)說(shuō),babel
舍棄了以前的 babel-*-*
的命名方式,改成了@babel/*-*
修改依賴(lài)和.babelrc
文件后就能正常啟動(dòng)項(xiàng)目了。webpack
不用修改(除非你是webpack 3.X
升webpack 4.X
)
上面的只是我遇到的問(wèn)題,如果還有其他問(wèn)題,可以參考資料 升級(jí)指南 Upgrade to Babel 7
本作品系作者:Shurlormes原創(chuàng) , 采用《署名-非商業(yè)性使用-禁止演繹 4.0 國(guó)際》許可協(xié)議
轉(zhuǎn)自:https://segmentfault.com/a/1190000016458913 作者:Shurlormes
聯(lián)系客服