@0xc/serverless-plugin-typescript 中文文档教程

发布于 4年前 浏览 26 项目主页 更新于 3年前

builds.sr.ht status License: MIT

@0xc/serverless-plugin-typescript

这是 serverless-plugin-typescript 的一个分支包裹。

Features

  • Zero-or-more-config: Works out of the box, but you can still define some configurations!
  • Supports ES2015 syntax + features (export, import, async, await, Promise, …)
  • Supports sls package, sls deploy and sls deploy function
  • Supports sls invoke local + --watch mode
  • Integrates nicely with serverless-offline

Install

yarn add --dev serverless-plugin-typescript typescript
# or
npm install -D serverless-plugin-typescript typescript

将以下插件添加到您的 serverless.yml

plugins:
  - serverless-plugin-typescript

Configure

请参阅 example folder 以获取最小示例。

tsconfig.json

插件使用的默认 tsconfig.json 文件如下所示:

{
  "compilerOptions": {
    "preserveConstEnums": true,
    "strictNullChecks": true,
    "sourceMap": true,
    "allowJs": true,
    "target": "es5",
    "outDir": ".build",
    "moduleResolution": "node",
    "lib": ["es2015"],
    "rootDir": "./"
  }
}

注意 1:不能覆盖 outDirrootDir 选项。

注意 2:不要将此存储库中的 tsconfig.json 与上面提到的混淆。

Including extra files

package/include 中的所有文件都将包含在最终构建文件中。 href

Usage

Google Cloud Functions

通过 serverless-google-cloudfunctions 插件,您只需在 package.json 中提供一个 main 字段:

{
  // ...
  "main": "handler.js",
  // ..
}

此插件将自动正确编译您的打字稿。 笔记 该字段必须引用编译后的文件名,即以 .js 结尾 扩大。

如果未找到 main 字段,则此插件将使用 index.js。 前 编译开始,它将检查指示的文件是否存在 .ts 扩展名,然后再实际尝试编译它。

Automatic compilation

正常的 Serverless 部署过程将自动使用 Typescript 进行编译:

  • Create the Serverless project with serverless create -t aws-nodejs
  • Install Serverless Typescript as above
  • Deploy with serverless deploy

Usage with serverless-offline

该插件与 serverless-offline 集成得很好 在本地模拟 AWS Lambda 和 AWS API Gateway。

将插件添加到您的 serverless.yml 文件并确保 serverless-plugin-typescriptserverless-offline 之前,因为顺序很重要:

  plugins:
    ...
    - serverless-plugin-typescript
    ...
    - serverless-offline
    ...

运行 serverless offlineserverless offline start 以启动 Lambda/API 模拟。

serverless offline 相比,start 命令将触发一个 init 和一个 end 生命周期钩子,这是需要的serverless-offline 和例如 serverless-dynamodb-local 关闭资源(见下文)

serverless-dynamodb-local

配置您的服务与上述相同,但另外添加 serverless- dynamodb-local 插件如下:

  plugins:
    - serverless-plugin-typescript
    - serverless-dynamodb-local
    - serverless-offline

运行serverless offline start

Other useful options

您可以使用 --dontPrintOutput 和 使用 --noTimeout 禁用超时。

Run a function locally

要在本地运行已编译的函数,您可以:

$ serverless invoke local --function <function-name>

选项是:

  • --function or -f (required) is the name of the function to run
  • --watch - recompile and run a function locally on source changes
  • --path or -p (optional) path to JSON or YAML file holding input data
  • --data or -d (optional) input data

Enabling source-maps

您可以通过安装和使用以下插件轻松启用对源映射的支持(使堆栈跟踪更易于阅读):

yarn add --dev source-map-support
// inside of your function
import 'source-map-support/register'

如果您正在使用 webpack(最有可能)。 将 devtool: 'source-map' 添加到 webpack.config.js

module.exports = {
  .... snip ....
  devtool: 'source-map',
  .... snip ....

}

Help & Community

加入我们的 Spectrum 社区< /a> 如果您遇到问题或有疑问。 我们喜欢和你聊天!

Prisma

builds.sr.ht status License: MIT

@0xc/serverless-plugin-typescript

This is a fork of the serverless-plugin-typescript package.

Features

  • Zero-or-more-config: Works out of the box, but you can still define some configurations!
  • Supports ES2015 syntax + features (export, import, async, await, Promise, …)
  • Supports sls package, sls deploy and sls deploy function
  • Supports sls invoke local + --watch mode
  • Integrates nicely with serverless-offline

Install

yarn add --dev serverless-plugin-typescript typescript
# or
npm install -D serverless-plugin-typescript typescript

Add the following plugin to your serverless.yml:

plugins:
  - serverless-plugin-typescript

Configure

See example folder for a minimal example.

tsconfig.json

The default tsconfig.json file used by the plugin looks like this:

{
  "compilerOptions": {
    "preserveConstEnums": true,
    "strictNullChecks": true,
    "sourceMap": true,
    "allowJs": true,
    "target": "es5",
    "outDir": ".build",
    "moduleResolution": "node",
    "lib": ["es2015"],
    "rootDir": "./"
  }
}

Note 1: The outDir and rootDir options cannot be overwritten.

Note 2: Don't confuse the tsconfig.json in this repository with the one mentioned above.

Including extra files

All files from package/include will be included in the final build file. See Exclude/Include

Usage

Google Cloud Functions

When using with Google Cloud Functions via the serverless-google-cloudfunctions plugin, you simply have to provide a main field in your package.json:

{
  // ...
  "main": "handler.js",
  // ..
}

And this plugin will automatically compile your typescript correctly. Note that the field must refer to the compiled file name, namely, ending with a .js extension.

If a main field was not found, then this plugin will use index.js. Before compilation begins, it will check to see that the file indicated exists with a .ts extension before actually trying to compile it.

Automatic compilation

The normal Serverless deploy procedure will automatically compile with Typescript:

  • Create the Serverless project with serverless create -t aws-nodejs
  • Install Serverless Typescript as above
  • Deploy with serverless deploy

Usage with serverless-offline

The plugin integrates very well with serverless-offline to simulate AWS Lambda and AWS API Gateway locally.

Add the plugins to your serverless.yml file and make sure that serverless-plugin-typescript precedes serverless-offline as the order is important:

  plugins:
    ...
    - serverless-plugin-typescript
    ...
    - serverless-offline
    ...

Run serverless offline or serverless offline start to start the Lambda/API simulation.

In comparison to serverless offline, the start command will fire an init and a end lifecycle hook which is needed for serverless-offline and e.g. serverless-dynamodb-local to switch off resources (see below)

serverless-dynamodb-local

Configure your service the same as mentioned above, but additionally add the serverless-dynamodb-local plugin as follows:

  plugins:
    - serverless-plugin-typescript
    - serverless-dynamodb-local
    - serverless-offline

Run serverless offline start.

Other useful options

You can reduce the clutter generated by serverless-offline with --dontPrintOutput and disable timeouts with --noTimeout.

Run a function locally

To run your compiled functions locally you can:

$ serverless invoke local --function <function-name>

Options are:

  • --function or -f (required) is the name of the function to run
  • --watch - recompile and run a function locally on source changes
  • --path or -p (optional) path to JSON or YAML file holding input data
  • --data or -d (optional) input data

Enabling source-maps

You can easily enable support for source-maps (making stacktraces easier to read) by installing and using the following plugin:

yarn add --dev source-map-support
// inside of your function
import 'source-map-support/register'

If you are using webpack (most likely). Add devtool: 'source-map' to webpack.config.js:

module.exports = {
  .... snip ....
  devtool: 'source-map',
  .... snip ....

}

Help & Community

Join our Spectrum community if you run into issues or have questions. We love talking to you!

Prisma

更多

友情链接

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文