2017-04-17 15:26:32 +02:00
# serverless-plugin-typescript
2017-12-29 11:27:09 +01:00
[](http://www.serverless.com) [](https://badge.fury.io/js/serverless-plugin-typescript) [](https://travis-ci.org/graphcool/serverless-plugin-typescript)
2017-04-17 15:28:32 +02:00
2017-04-17 15:26:32 +02:00
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` , ...)
2017-07-26 10:22:19 +02:00
* Supports `sls package` , `sls deploy` and `sls deploy function`
* Supports `sls invoke local` + `--watch` mode
* Integrates nicely with [`serverless-offline` ](https://github.com/dherault/serverless-offline )
2017-04-17 15:26:32 +02:00
## Install
``` sh
yarn add --dev serverless-plugin-typescript
```
Add the following plugin to your `serverless.yml` :
``` yaml
plugins :
- serverless-plugin-typescript
```
2017-07-25 19:48:07 +02:00
## Configure
2017-04-17 15:26:32 +02:00
See [example folder ](example ) for a minimal example.
2017-07-25 19:48:07 +02:00
### `tsconfig.json`
2017-04-17 15:26:32 +02:00
The default `tsconfig.json` file used by the plugin looks like this:
``` json
{
2017-08-20 21:39:13 +02:00
"compilerOptions" : {
"preserveConstEnums" : true ,
"strictNullChecks" : true ,
"sourceMap" : true ,
"target" : "es5" ,
"outDir" : ".build" ,
"moduleResolution" : "node" ,
"lib" : [ "es2015" ] ,
"rootDir" : "./"
}
2017-04-17 15:26:32 +02:00
}
```
2017-07-25 19:48:07 +02:00
> Note 1: The `outDir` and `rootDir` options cannot be overwritten.
2017-06-13 15:12:58 +02:00
2017-06-13 15:12:34 +02:00
> Note 2: Don't confuse the [`tsconfig.json`](tsconfig.json) in this repository with the one mentioned above.
2017-04-17 15:26:32 +02:00
2017-07-25 19:48:07 +02:00
### Including extra files
2017-05-19 17:10:07 -04:00
All files from `package/include` will be included in the final build file. See [Exclude/Include ](https://serverless.com/framework/docs/providers/aws/guide/packaging#exclude--include )
2017-07-25 19:48:07 +02:00
## Usage
2017-08-11 09:56:23 +10:00
### Google Cloud Functions
When using with Google Cloud Functions via the [serverless-google-cloudfunctions ](https://github.com/serverless/serverless-google-cloudfunctions )
plugin, you simply have to provide a `main` field in your `package.json` :
``` js
{
// ...
"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.
2017-07-25 19:48:07 +02:00
### 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
2017-11-01 21:44:29 +08:00
The plugin integrates very well with [serverless-offline ](https://github.com/dherault/serverless-offline ) to
2017-07-25 19:48:07 +02:00
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:
``` yaml
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:
``` yaml
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:
``` bash
$ serverless invoke local --function <function -name>
```
2017-09-06 22:44:59 +02:00
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
2017-09-06 22:44:28 +02:00
### Enabling source-maps
You can easily enable support for source-maps (making stacktraces easier to read) by installing and using the following plugin:
``` sh
yarn add --dev source-map-support
```
``` ts
// inside of your function
import 'source-map-support/register'
```
2017-11-02 12:48:41 +01:00
If you are using webpack (most likely). Add `devtool: 'source-map'` to `webpack.config.js` :
``` js
module . exports = {
... . snip ... .
devtool : 'source-map' ,
... . snip ... .
}
```
2017-04-17 15:26:32 +02:00
## Help & Community [](https://slack.graph.cool)
Join our [Slack community ](http://slack.graph.cool/ ) if you run into issues or have questions. We love talking to you!
