Coder Social home page Coder Social logo

serverless-plugin-typescript's Introduction

serverless-plugin-typescript

serverless

Serverless plugin for zero-config Typescript support

mtwzim Update

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

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

  1. EPERM: operation not permitted, scandir...
  2. EPERM: operation not permitted, symlink...

When attaching the "node_modules" folder to the build

Created PR for original plugin: http://www.github.com/prismagraphql/serverless-plugin-typescript/pull/103

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 @mtwzim/serverless-plugin-typescript

or

npm install --save-dev @mtwzim/serverless-plugin-typescript

Add the following plugin to your serverless.yml:

plugins:
  - "@mtwzim/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.

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

plugins: ...
  - '@mtwzim/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:
  - "@mtwzim/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 ....

}

serverless-plugin-typescript's People

Contributors

adieuadieu avatar ajmath avatar balassy avatar dlackty avatar dtslvr avatar ghostfromtexas avatar greenkeeper[bot] avatar kandros avatar kbrandwijk avatar kennu avatar kopertop avatar movecodemove avatar mtwzim avatar nicholasdunham avatar oscargalindo avatar pjsier avatar renovate-bot avatar renovate[bot] avatar romanhotsiy avatar schickling avatar tomjelen avatar wyattjoh avatar

Watchers

 avatar  avatar

Forkers

arunjayakumar01

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.