TypeScript, AWS SAM and Rollup.js

Rollup Configuration

// rollup.config.js
import fs from 'fs'
import CloudFormation from 'yaml-cfn'
import typescript from 'rollup-plugin-typescript'
import nodeResolve from 'rollup-plugin-node-resolve'
const defaultConfig = {
plugins: [
nodeResolve(),
typescript(),
],
output: {
format: 'commonjs',
},
external: [
'aws-sdk',
],
}
const { Resources } = CloudFormation.yamlParse(fs.readFileSync('template.yml'))const entries = Object.values(Resources)
.filter(resource => resource.Type == 'AWS::Serverless::Function')
.filter(resource => resource.Properties.Runtime.startsWith('nodejs'))
.map(resource => {
const file = resource.Properties.Handler.split('.')[0]
const prefix = resource.Properties.CodeUri.substr(8)
return Object.assign({}, defaultConfig, {
input: `${prefix}/${file}.ts`,
output: {
format: 'cjs',
dir: `.rollup/${prefix}`,
}
})
})
export default entries

AWS SAM Template

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
MyFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs10.x
CodeUri: .rollup/packages/my-function

Result

$ yarn rollup -c rollup.config.jspackages/my-function/index.ts → .rollup/packages/my-function...
created .rollup/packages/my-function in 283ms

--

--

--

I build stuff at Relatel A/S, vivid traveller and gear junkie.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why JavaScript Developers Should Prefer Axios Over Fetch

A dog catching a ball

Deploy a static Vue.js cli based project with Dokku

Build a Product Review app with React Hooks (Part 3)

State Management in React with Recoil

Vim global command

React Hooks Basics

Jest Testing

Roman Numerals

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Henrik Bjørnskov

Henrik Bjørnskov

I build stuff at Relatel A/S, vivid traveller and gear junkie.

More from Medium

GraphQL for Node.js developers, pt. 2 — Unions and Interfaces

Implement Domain-Driven Design (DDD) in Golang

How to Create Global Variables in TypeScript with Node.js

A developer solving a problem

Cloudflare Workers vs AWS Lambda: Detailed Comparison