使用 Babel
為 js 的第三方編譯工具
在 Node.js 使用 import 並讀取 React component
新增 .babelrc
然後安裝
preset-env 整合了原本 es2015 等plugin,然後preset-react 用來轉 jsx 為 React.createElement
babel-node 類似 babel-cli 不過 前者預先在執行前 compile presets and plugins
之後即可如下使用
test.js
執行
babel-node
只會執行以下條件的編譯
Rooted at the current working directory
Not inside
node_modules
in the current working directory
babel-core/register
babel-core/register
1.我們在webpack寫入babel後可使用es6功能,但在entry file外就無法使用es6功能
2.Node.js的--harmony不包含module功能
所以要在Node.js使用ES6 的export default 須另外寫一個.babelrc檔案
2.再於使用export default 的parent檔案,使用(require)
babel-core/register
第二種方法
將會自動轉有關import或相關ES6.7
於Production
因為babel-node或bable-register在runtime build
所以我們要先compile
如果有react
之後使用
lib為來源要compile 的server 檔案,dist為compile後會產生的資料夾
注意,css檔案不會被compile,所以如果是把整個資料夾compile裡面的css不會出現在compile後的資料夾
有關import 'babel-polyfill';
不錯的文章
可直接加到html的polyfill
之後不用compile可直接使用ES6,7,8以上新功能
Last updated
Was this helpful?