现在 Node 9.2.0 具有该语言的所有新功能,我该如何创建一个向后兼容旧版本的节点模块?
如果我有一个节点 9 开箱即用的小模块,就像这样。
const {map} = require('lodash')
async function test (...args) {
return map(args, (item) => {
return `${item} yeah`
})
}
module.exports = test
有没有使用 babel 将其转译为我需要使用 babel env 支持的特定向后版本?有什么方法可以有条件地加载这些 babel 开发依赖项,比如使用安装后脚本通过节点 4 安装它?
这似乎是一种解决方案,其中一个缺点是它需要 babel-runtime
作为 dep 以防万一,即使当前版本的节点不需要它。但是在 9.2.0 中,上面的代码是构建的代码,它只是由 babel 移动。
这是一个示例 package.json,在安装时它将构建 src。
{
"name": "example",
"version": "1.0.0",
"main": "lib/index.js",
"scripts": {
"build": "babel src -d lib",
"postinstall": "npm run build"
},
"dependencies": {
"babel-runtime": "^6.26.0",
"lodash": "^4.17.4"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1"
},
"babel": {
"plugins": [
"transform-runtime"
],
"presets": [
[
"env",
{
"targets": {
"node": "current"
}
}
]
]
}
}