今天學(xué)習(xí)es7新特性裝飾器時,代碼提示語法錯誤,babel照著以前的方法轉(zhuǎn)碼也不成功,故寫下此文談?wù)勅绾谓鉀Q
大致步驟如下:
1.安裝babel轉(zhuǎn)碼工具
2.安裝第三方插件,用于支持decorators
3.配置jsconfig.json解決vscode提示語法錯誤
4.babel打包成功運行
注:我這里沒有配置.babelrc文件
1,2.npm install babel-cli babel-plugin-transform-decorators-legacy babel-register --save-dev
3.jsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true
}
}
4.package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "babel --plugins transform-decorators-legacy src -d lib"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"babel-plugin-transform-decorators-legacy": "^1.3.5",
"babel-register": "^6.26.0"
},
"dependencies": {
"babel-cli": "^6.26.0"
}
}
最后執(zhí)行打包命令:npm run build ,即可成功打包代碼,并且可在node環(huán)境下運行
運行也可使用require,不過還是建議使用上面那種
require('babel-register')({
plugins: ['transform-decorators-legacy']
});
require("./input.js")
|