TS 中文文档 TS 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • 入门教程

    • TypeScript 手册
    • 基础知识
    • 日常类型
    • 类型缩小
    • 更多关于函数
    • 对象类型
    • 从类型创建类型
    • 泛型
    • Keyof 类型运算符
    • Typeof 类型运算符
    • 索引访问类型
    • 条件类型
    • 映射类型
    • 模板字面类型
    • 类
    • 模块
  • 参考手册

  • 项目配置

serverless-plugin-typescript


Originally developed by Prisma Labs, now maintained in scope of Serverless, Inc

Serverless plugin for zero-config Typescript support

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


  1. ``` shell
  2. yarn add --dev serverless-plugin-typescript typescript
  3. # or
  4. npm install -D serverless-plugin-typescript typescript
  5. ```

Add the following plugin to your serverless.yml :

  1. ``` yaml
  2. plugins:
  3.   - serverless-plugin-typescript
  4. ```

Configure


See example folder for a minimal example.

tsconfig.json


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

  1. ``` json
  2. {
  3.   "compilerOptions": {
  4.     "preserveConstEnums": true,
  5.     "strictNullChecks": true,
  6.     "sourceMap": true,
  7.     "allowJs": true,
  8.     "target": "es5",
  9.     "outDir": ".build",
  10.     "moduleResolution": "node",
  11.     "lib": ["es2015"],
  12.     "rootDir": "./"
  13.   }
  14. }
  15. ```

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

Non-standard tsconfig.json locations


Override what tsconfig.json to use with the following snippet in your severless.yaml

  1. ``` sh
  2. custom:
  3.   serverlessPluginTypescript:
  4.     tsConfigFileLocation: './tsconfig.build.json'

  5. ```

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 :

  1. ``` js
  2. {
  3.   // ...
  4.   "main": "handler.js",
  5.   // ..
  6. }
  7. ```

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:

  1. ``` yaml
  2.   plugins:
  3.     ...
  4.     - serverless-plugin-typescript
  5.     ...
  6.     - serverless-offline
  7.     ...
  8. ```

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:

  1. ``` yaml
  2.   plugins:
  3.     - serverless-plugin-typescript
  4.     - serverless-dynamodb-local
  5.     - serverless-offline
  6. ```

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:

  1. ``` shell
  2. $ serverless invoke local --function <function-name>
  3. ```

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:

  1. ``` shell
  2. yarn add --dev source-map-support
  3. ```

  1. ``` ts
  2. // inside of your function
  3. import 'source-map-support/register'
  4. ```

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

  1. ``` js
  2. module.exports = {
  3.   .... snip ....
  4.   devtool: 'source-map',
  5.   .... snip ....

  6. }
  7. ```
Last Updated: 2023-09-03 17:10:52