serverless-plugin-typescript

授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发、 常用JavaScript包
软件类型 开源软件
地区 不详
投 递 者 纪翰
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

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

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

Non-standard tsconfig.json locations

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

custom:
  serverlessPluginTypescript:
    tsConfigFileLocation: './tsconfig.build.json'

Usage

Google Cloud Functions

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

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

And this plugin will automatically compile your typescript correctly. Notethat the field must refer to the compiled file name, namely, ending with a .jsextension.

If a main field was not found, then this plugin will use index.js. Beforecompilation 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 tosimulate AWS Lambda and AWS API Gateway locally.

Add the plugins to your serverless.yml file and make sure that serverless-plugin-typescriptprecedes 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-localplugin 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 anddisable 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 ....

}
  • serverless create 基本命令 serverless create --template aws-nodejs 该命令用于在当前目录使用aws-nodejs template创建一个工程。 命令参数 --template 可以缩写为-t 用于指定模板名称 --template-url 可以缩写为-u 用于指定远程模板的地址,比如在github上的模板 --template-path

  • TypeScript and Lambda Serverless Project Limit Recently, our project deployment met this exception while deploy An error occurred: DevicePairingUpdatesLambdaFunction - Unzipped size must be smaller th

  • TypeScript and Serverless Memory Issue Change the file to add the follow codes at the top /opt/node/lib/node_modules/serverless/bin/serverless #!/usr/bin/env node --max-old-space-size=3000 --trace-gc-

  • 用过 ant-design 的同学可能对 babel-plugin-import 有印象,它可以帮助实现模块的按需引用,比如: import { Button } from 'antd'复制代码 在使用该 Plugin 之后会被转换成: import Button from 'antd/lib/button'复制代码 在一个没有使用 antd 全部组件的项目里,这样做可以明显减少打包后的代码体积。

 相关资料
  • Deploy AppSync API's in minutes using this Serverless plugin. Getting Started Be sure to check out all that AWS AppSync has to offer. Here are a few resources to help you understand everything needed

  • Serverless WarmUp Plugin ♨ Keep your lambdas warm during winter. Requirements: Serverless v1.12.x or higher (Recommended v1.33.x or higher because of this). AWS provider How it works WarmUp solves col

  • Serverless Optimize Plugin Bundle with Browserify, transpile and minify with Babel automatically to your NodeJS runtime compatible JavaScript. This plugin is a child of the great serverless-optimizer-

  • Serverless AWS Alerts Plugin A Serverless plugin to easily add CloudWatch alarms to functions Installation npm i serverless-plugin-aws-alertsORyarn add --dev serverless-plugin-aws-alerts Usage Basic U

  • Serverless Plugin Canary Deployments A Serverless plugin to implement canary deployments of Lambda functions, making use of the traffic shifting feature in combination with AWS CodeDeploy Contents Ins

  • 云原生应用开发 回顾过去二十年,应用开发有以下几个显著的特点: 以应用服务器为中心,典型应用服务器包括 tomcat,JBoss,WebLogic,WebSphere,应用服务器提供了丰富的技术堆栈和系统构建范式,对应用开发人员友好 JavaEE/Spring,JavaEE/Spring 是应用开发的基本技能,这项技能有广泛的开发者基础,过去二十年中 JavaEE/Spring 的技术发展/版本的