1.0.0 • Published 7 months ago

esbuild-plugin-serverless v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
7 months ago

Esbuild Serverless Plugin

Node version \ This plugin allows you to imitate hot-module-replacement when working with NodeJS based Cloud Functions in Yandex Cloud. \ Using esbuild it rebuilds the code on save and deploys it to the cloud without even saving compiled files to the disk

Setup

Install the neccessary packages

npm install esbuild esbuild-plugin-serverless ts-node

Create a file with a name similar to build.ts

Describe your Cloud Functions entrypoints and pass them to esbuild build function.

import {
  EntrypointConfig,
  Entrypoints,
  getFromEnv,
  esbuildServerlessPlugin
} from 'esbuild-plugin-serverless'

const common: EntrypointConfig = {
  runtime: 'nodejs16',
  tag: ['latest'],
  resources: {
    $type: 'yandex.cloud.serverless.functions.v1.Resources',
    memory: 128 * 1024 * 1024
  },
  executionTimeout: {
    $type: 'google.protobuf.Duration',
    seconds: 5,
    nanos: 0
  }
}

const entrypoints: Entrypoints = {
  index: () => ({
    ...common,
    functionId: getFromEnv('CLOUD_FUNCTION_ID'),
    entrypoint: 'index.handler'
  })
}

import { context as createContext } from 'esbuild'
import esbuildServerlessPlugin from 'esbuild-plugin-serverless'
import { Entrypoints } from 'esbuild-plugin-serverless/entrypoint'

const build = async (entrypointConfig: Entrypoints) => {
  process.env.NODE_ENV = 'PRODUCTION'
  const entryPoints = Object.keys(entrypointConfig).map(
    // This should include the path to a folder
    // where your entrypoints reside as individual files or folders
    (entrypoint) => `./src/${entrypoint}`
  )

  // Some packages can break the build process so we can move them outside
  const external = {
    '@yandex-cloud/nodejs-sdk': '*',
    googleapis: '*'
  }
  const context = await createContext({
    entryPoints,
    bundle: true,
    minify: true,
    platform: 'node',
    target: 'node16',
    external: Object.keys(external),
    treeShaking: true,
    outdir: 'build',
    write: false,
    plugins: [esbuildServerlessPlugin(entrypointConfig, external)]
  })
  await context.watch()
}

export default build

Bind build function to dev script by adding it into your package.json

{
  "scripts": {
    "dev": "ts-node chores/index.ts"
  }
}

Add some authorization by providing the following variables from service sccount authorized key. You can use getFromEnv helper.

YC_ACCESS_KEY_ID=
YC_SERVICE_ACCOUNT_ID=
YC_PRIVATE_KEY=

This method is used for security reasons, as using OAuth token is not recommended. If these variables are not provided the library will attempt to authorize from Metadata service. One can also work entirely in the cloud using a VM with service-account attached to it.

Run

npm run dev

and enjoy automatic re-deployment on save

1.0.0

7 months ago

0.2.5

7 months ago

0.2.3

9 months ago

0.2.4

9 months ago

0.2.1

10 months ago

0.2.2

10 months ago

0.2.0

1 year ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago