如何为AWS Lambda nodejs运行时构建单个js文件 [英] How do I build a single js file for AWS Lambda nodejs runtime
问题描述
我们正在开发一个有助于在AWS中部署和维护代码的项目/框架 LAMBDA。我想将lambda函数的所有node.js代码构建/捆绑到一个js文件中,因为:
We are working on a project/framework that aids in deploying and maintaining code in AWS Lambda. I want to build/bundle all node.js code for a lambda function into one js file because:
- 较小的代码库有助于lambda冷启动问题
- Lambda的代码拉链大小限制为50MB
我们不想创建一个自定义捆绑工具,因为有很多选项(systemjs,browserify,webpack等)。但是,我们担心某些节点模块与捆绑器/构建器不兼容的问题。
We don't want to create a custom bundler to do this because there are many options already out there (systemjs,browserify,webpack etc). HOWEVER we are concerned with issues with some node modules not playing well with bundlers/builders.
具体 aws-sdk
有 webpack的已知问题,说它有浏览支持但我已经与浏览过awts-sdk问题的人进行了交谈
Specifically aws-sdk
has known issues with webpack, says it has browserify support but I've talked to people who have had issues with browserify'ing aws-sdk
我们想选择一个现有的捆绑器(或2),但我们希望确保它适用于尽可能多的模块/代码。我们正在尝试为JAWS创建一个插件生态系统,所以要做到这一点非常重要(不要让人们关闭,因为他们使用的模块X不会捆绑)。
We want to pick an existing bundler (or 2), but we want to make sure it works for as many modules/code as possible. We are trying to create a plugin ecosystem for JAWS, so getting this right is important (don't want to turn people off because module X they use wont bundle).
问题:
- 我如何进行捆绑/构建以满足这些约束条件?
- 是否存在我们可以向消费者提供我们产品的任何指导方针,以确保他们编写/使用的代码捆绑在一起?例如:动态require()导致问题AFAIK。
推荐答案
aws-sdk-js 现在正式支持browserify 。你可以为什么这是一件好事在我的博客上。
aws-sdk-js now officially supports browserify. You can why this is a great thing on my blog.
我创建了一个名为 serverless-plugin-browserify 将以极少的配置完成所有繁重的任务。
I have created a serverless plugin called serverless-plugin-browserify that will do all the heavy lifting with very minimal configuration.
要具体回答这个问题,我用这个browserify配置解决了这个问题:
To answer the question specifically, I solved the problem with this browserify config:
{
disable: false, //Not an official option, used as internal option to skip browserify
exclude: [], //Not an option, but will use for setting browserify.exclude() if defined in yml
ignore: [], //Not an option, but will use for setting browserify.ignore() if defined in yml
basedir: this.serverless.config.servicePath,
entries: [],
standalone: 'lambda',
browserField: false, // Setup for node app (copy logic of --node in bin/args.js)
builtins: false,
commondir: false,
ignoreMissing: true, // Do not fail on missing optional dependencies
detectGlobals: true, // We don't care if its slower, we want more mods to work
insertGlobalVars: { // Handle process https://github.com/substack/node-browserify/issues/1277
//__filename: insertGlobals.lets.__filename,
//__dirname: insertGlobals.lets.__dirname,
process: function() {
},
},
debug: false,
}
You can see my full code here with a complete example here
这篇关于如何为AWS Lambda nodejs运行时构建单个js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!