Babel編輯ES6代碼

1.安裝babel依賴javascript

npm install --save-dev @babel/core @babel/cli @babel/preset-env
npm install --save @babel/polyfill

  

2.用node初始化一個項目java

node init -y

  

初始化後的package.json長這樣node

{
  "name": "learn2",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

  

在package.json中增長scripts命令chrome

{
  "name": "learn2",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "babel src -d dest"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

  

3.配置 babel.config.jsnpm

const presets = [
	[
		"@babel/env",
		{
			targets: {
				edge: "9",
				firefox: "60",
				chrome: "67",
				safari: "11.1",
			},
			useBuiltIns: "usage",
		},
	],
];

module.exports = { presets };

  

4.運行json

npm run build
相關文章
相關標籤/搜索