用无服务器正确实现babel

在我们的项目中,我们最近决定,对于某些实现,es6 +函数使我们的生活变得更加简单。 然而,这个“更容易”并没有被babel与无服务器结合的翻译。

项目结构如下:

+ |- serverless.yaml |- package.json |- .babelrc |- /src |- handler.js |- package.json |- /test |- test.js 

这显示了整个项目文件夹。 以下是各个文件的源代码。

serverless.yaml

 provider: name: aws runtime: nodejs6.10 functions: getHello: handler: handler.lambdaGetHello events: - http: path: api/hello method: GET authorizer: aws_iam cors: true package: include: - handler.js - node_modules/** 

package.json(顶层,请注意,这可能有太多的包,实际的代码有更多的用法,但清理这可能是为了)

 { "name": "Default-package-JSON-file", "version": "0.1.0", "description": "Simple serverless test", "scripts": { "build": "npm run build:init && npm run build:js && npm run build:install", "build:init": "rm -rf dist && mkdir dist && rm -rf src/node_modules && rm -rf src/test", "build:install": "cp src/package.json dist/ && cd dist && yarn", "build:js": "babel src --out-dir dist", "install-base": "npm run install-base:tld && npm run install-base:src", "install-base:tld": "yarn", "install-base:src": "cd src/ && yarn", "svl-deploy": "cp serverless.yaml dist/ && cd dist && serverless deploy", "test-dist": "cp -R test/ dist/ && cd dist && mocha", "test-src": "cp -R test/ src/ && cd src && mocha" }, "author": "Mathieu Devos", "license": "UNLICENSED", "dependencies": { "add": "^2.0.6", "babel-runtime": "^6.26.0", "chai": "^4.0.1", "mocha": "^3.4.2", "serverless": "^1.20.2" }, "devDependencies": { "babel-cli": "^6.26.0", "babel-core": "^6.26.0", "babel-eslint": "^7.2.3", "babel-plugin-transform-async-to-generator": "^6.24.1", "babel-plugin-transform-class-properties": "^6.24.1", "babel-plugin-transform-runtime": "^6.23.0", "babel-polyfill": "^6.26.0", "babel-preset-es2015": "^6.24.1", "babel-register": "^6.26.0" }, "private": true } 

.babelrc

 { "plugins": [ "transform-async-to-generator", "transform-runtime", { "polyfill": false, "regenerator": true }], "presets": ["es2015"] } 

src / handler.js(显然不是所有东西都被定义了,这只是一个例子)。

 async function hello(items = []) { const output = []; for (const item of items) { output.push(await item.handle()); } return output; }; module.exports.hello = hello; const lambdaAddItems = (event, context, cb) => { const req = new Request(event); const args = context.args; magicFunction(hello, [args], callback); }; module.export.lambdaHello = hello; 

src / package.json(这个只包含src中所需的所有不同的包,现在只包含足够的请求)。

 { "name": "source-package-json-file", "version": "0.1.0", "description": "Generic package module for source code", "scripts": {}, "author": "Mathieu Devos", "license": "UNLICENSED", "dependencies": { "lambda-proxy-utils": "^1.2.4" }, "devDependencies": {}, "private": true } 

test / test.js(testing本地代码)。

 const handler = ../handler.js; const hello = handler.hello; const lambdaHello = handler.lambdaHello; ... Run the tests 

那么我们做什么来运行这个代码呢?

我们有一个pipe道(如果你愿意,还可以使用本地脚本),按顺序运行以下命令:

npm run install-base npm run test-src npm run build npm run test-dist npm run svl-deploy

不同命令的解释:1)npm run install-base – 安装所有的dev依赖和命令来运行linting,testing,babel和无服务器。 它也进入/ src来安装所需的pacakges,以便src可以运行

2)npm run test-src – 将testing复制到src并运行它,这将testing所有的代码到本地文件夹(在这种情况下是src文件夹)

3)npm run build – 从src中删除node_modules&test,使用babel将src转换为dist(这实际上正确地翻译了)

4)npm run test-dist – 将testing复制到dist并运行它,这将testing本地文件夹在这种情况下dist。 这里的问题是,这不工作没有 – 需要babel-polyfill命令。 然而,翻译是

5)npm run svl-deploy – 鉴于一切正常,部署到无服务器。


问题出现在第4步,如果我没有运行 – 需要babel-polyfill我得到的错误:

 regeneratorRuntime is not defined 

我可以“修复”给予摩卡以下命令 – 需要babel-polyfill。

但是运行会导致无服务器部署中断。

我现在的问题是:这个“理智”的方式是什么? 如果可能的话,我想保留这样的文件夹概览,因为它真的清楚地显示了需要的地方,而不会混淆整体视图。

BR,Mathieu

答案隐藏在babel的转换运行时插件中。

正确的.babelrc文件:

 { "plugins": [ "transform-async-to-generator", "transform-runtime" ], "presets": ["es2015"] } 

在src / package.json中添加babel-polyfill (因为无服务器需要部署)。

诸如将webpack与无服务器一起使用的替代方式是非常不受欢迎的。