|
今天學(xué)習(xí)es7新特性裝飾器時(shí),代碼提示語(yǔ)法錯(cuò)誤,babel照著以前的方法轉(zhuǎn)碼也不成功,故寫(xiě)下此文談?wù)勅绾谓鉀Q
大致步驟如下:
1.安裝babel轉(zhuǎn)碼工具
2.安裝第三方插件,用于支持decorators
3.配置jsconfig.json解決vscode提示語(yǔ)法錯(cuò)誤
4.babel打包成功運(yùn)行
注:我這里沒(méi)有配置.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)境下運(yùn)行
運(yùn)行也可使用require,不過(guò)還是建議使用上面那種
require('babel-register')({
plugins: ['transform-decorators-legacy']
});
require("./input.js")
|