我有一个 NodeJS AWS Lambda 函数,它根据 html 模板文件生成电子邮件(emailTemplate.html
)。我已开始通过 SAM 使用 esbuild 构建我的 lambda https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-build-typescript.html。现在我想知道如何配置 SAM/esbuild 将此文件包含到我的 lambda 包中。
这是 lambda 的 SAM 模板配置:
EmailNotificationFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./lambdas-node/email-notifications/
Handler: daily-summary.handler
Timeout: 120
MemorySize: 512
Runtime: nodejs16.x
Metadata:
BuildMethod: esbuild
BuildProperties:
Sourcemap: true
EntryPoints:
- daily-summary.ts
在我的应用程序代码中,我从本地文件系统读取文件:
fs.readFileSync("./emailTemplate.html", "utf-8")
html 文件很小,所以我想坚持这种简约的方法。我总是可以从 S3 获取文件或将其打包在一个层中,但我不想去那里。
好的,基本上是 ESBuild 的文件加载器 https://esbuild.github.io/content-types/#file是要走的路。 ESBuild 将用对文件的引用替换导入,并将文件复制到结果包中。 (这正是我想要的。)
这种行为似乎是 ESBuild 特有的,并且不适用于常规的tsc
编译器。所以我将构建步骤替换为类型检查tsc
并转译为esbuild
(见下文)
我将 html 文件的导入添加到我的代码中。这将触发 ESBuild 对此文件执行某些操作。
import emailTemplateHtmlUrl from "./emailTemplate.html";
为了让类型检查员满意,我还添加了一个types.d.ts
文件(注意d.ts
扩大)
declare module '*.html' {
const value: string;
export default value
}
然后我添加了Loader
到我的 SAM 模板,以便 ESBuild 复制 html 文件并在导入中引用它们:
EmailNotificationFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./lambdas-node/email-notifications/
Handler: daily-summary.handler
Timeout: 120
MemorySize: 512
Runtime: nodejs16.x
Metadata:
BuildMethod: esbuild
BuildProperties:
Sourcemap: true
Loader:
- .html=file
EntryPoints:
- daily-summary.ts
最后,我的新测试命令现在如下所示:
tsc --noEmit
npx esbuild daily-summary.ts --outdir=. --loader:.html=file --platform=node
--bundle
mocha *.spec.js
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)