@3boysdad/serverless-plugin-typescript 中文文档教程

发布于 6年前 浏览 31 项目主页 更新于 3年前

serverless-plugin-typescript

serverless

无服务器插件零配置 Typescript 支持

3boysdad Update

原始 NPM 模块:http:

//www.npmjs.com/package/serverless-plugin-typescript此版本的 serverless-typescript-plugin 修复了 Windows 操作系统的符号链接问题,导致以下错误:

1) EPERM:不允许操作,符号链接…

当尝试创建符号链接到构建目录中的“package.json”文件时。

我不会为原始插件创建 PR,因为它现在似乎已经死了。 虽然我的解决方案有效,但我很确定这只是一个 hack。

Features

  • Zero-config: Works out of the box without the need to install any other compiler or plugins
  • 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 @3boysdad/serverless-plugin-typescript

npm install --save-dev @3boysdad/serverless-plugin-typescript

添加以下插件到你的serverless.yml

plugins:
  - '@3boysdad/serverless-plugin-typescript'

Configure

example 文件夹 一个最小的例子。

tsconfig.json

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

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

注意 1:不能覆盖 outDirrootDir 选项。 ** 考虑“修复”这个 ** 因为它看起来很老套。

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

Including extra files

个人而不是这个请使用并安装插件 - serverless-plugin-include-dependencies。

Usage

Google Cloud Functions

通过 serverless-google-cloudfunctions 使用 Google Cloud Functions 时 插件,您只需在 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 文件并确保 @hewmen/serverless-plugin-typescriptserverless-offline 之前,因为顺序很重要:

  plugins:
    ...
    - '@hewmen/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:
    - '@hewmen/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 Slack Status

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

serverless-plugin-typescript

serverless

Serverless plugin for zero-config Typescript support

3boysdad Update

Original NPM Module: http://www.npmjs.com/package/serverless-plugin-typescript

This version of serverless-typescript-plugin fixes symbolic link issue with Windows OS resulting in the following errors:

1) EPERM: operation not permitted, symlink…

When trying to create symlink to the "package.json" file in the build directory.

I won't create a PR for the original plugin as it seems dead now. And while my solution works, I am pretty sure it's only a hack.

Features

  • Zero-config: Works out of the box without the need to install any other compiler or plugins
  • 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 @3boysdad/serverless-plugin-typescript

or

npm install --save-dev @3boysdad/serverless-plugin-typescript

Add the following plugin to your serverless.yml:

plugins:
  - '@3boysdad/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,
    "target": "es5",
    "outDir": ".build",
    "moduleResolution": "node",
    "lib": ["es2015"],
    "rootDir": "./"
  }
}

Note 1: The outDir and rootDir options cannot be overwritten. ** mulling over 'fixing' this ** as it seems rather hacky.

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

Including extra files

Personally rather than this please use and install the plugin - serverless-plugin-include-dependencies.

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 @hewmen/serverless-plugin-typescript precedes serverless-offline as the order is important:

  plugins:
    ...
    - '@hewmen/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:
    - '@hewmen/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 Slack Status

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

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