@govtechsg/serverless-selective-functions

中文文档
1.1.0发布于 2022/04/13MITTypeScriptCommonJS零依赖近 30 天下载 6961

Serverless plugin to selectively deploy functions per stage

原始使用文档
文档目录3 节
  1. Installation
  2. Usage
  3. Why?

Serverless Selective Functions

Serverless License NPM

A Serverless plugin that enables you to selectively deploy functions for specific stages.

Installation

npm i @govtechsg/serverless-selective-functions --save-dev

Add the plugin to your serverless.yml file:

plugins:
  - "@govtechsg/serverless-selective-functions"

Usage

# Everything included by default
addVote:
  handler: src/functionHandlers/addVote/index.handler
  events:
    - http:
        path: /v1/vote
        method: post
        cors: true
  # Not specifying the `stages` property will include this function

# Exclude all prod-[0-9]*
deleteVote:
  handler: src/functionHandlers/deleteVote/index.handler
  events:
    - http:
        path: /v1/vote
        method: delete
        cors: true
  stages:
    exclude: # Specify stages that should exclude this function
      - "prod-[0-9]*" # Regex

# Include all dev-.* except dev-123
# Note that checks will run through the inclusion list first, then the exclusion list
editVote:
  handler: src/functionHandlers/editVote/index.handler
  events:
    - http:
        path: /v1/vote
        method: patch
        cors: true
  stages: # Stage will be tested with both the include and exclude properties
    include:
      - "dev-.*"
    exclude:
      - "dev-123"

# Include dev-123 and all prod-.*
summary:
  handler: src/functionHandlers/summary/index.handler
  events:
    - http:
        path: /v1/summary
        method: get
        cors: true
  stages:
    include:
      - "dev-123"
      - "prod-.*"

Why?

You may want to deploy some functions as specified in your serverless.yml only for a specific stage. A simple approach would be the following:

functions:
  - ${file(serverless/functions/base.yml)}
  - ${file(serverless/functions/${self:provider.stage}.yml)}

Then if you wanted to deploy functions only when the stage is set to offline, you can create a serverless/functions/offline.yml file and populate it with functions meant only for the offline stage.

The approach works for simple cases, but lets say you want to deploy the following functions for 3 stages:

prod: lambdaA, lambdaB
staging: lambdaB
offline: lambdaA, lambdaC

Given that there isn't a common subset of functions, you cannot create a base.yml. If you create a prod.yml with lambdaA + lambdaB, and staging.yml with lambdaB, you will then need to maintain 2 separate definitions of lambdaB which would be error prone. This restricts you to having mutually exclusive sets of functions defined in different yml files, otherwise we tradeoff maintainability.

版本与发布记录

@govtechsg/serverless-selective-functions 共发布 10 个版本,首次发布于 2020/12/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2022/04/134 年前
1.0.22022/04/134 年前
1.0.12022/04/134 年前
1.0.02022/04/134 年前
0.2.32022/04/074 年前
0.2.12021/12/174 年前

关于 @govtechsg/serverless-selective-functions 的常见问题

@govtechsg/serverless-selective-functions 是什么?

Serverless plugin to selectively deploy functions per stage

如何安装 @govtechsg/serverless-selective-functions?

在项目目录运行 npm install @govtechsg/serverless-selective-functions,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:serverless(^2.53.0 || 3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@govtechsg/serverless-selective-functions 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@govtechsg/serverless-selective-functions 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@govtechsg/serverless-selective-functions 可以商用吗?

@govtechsg/serverless-selective-functions 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@govtechsg/serverless-selective-functions 的下载量有多少?

根据 npm 官方下载统计,@govtechsg/serverless-selective-functions 在 2026/10/17 – 2026/11/15 这 30 天内被下载 6,961 次,日均约 232 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@govtechsg/serverless-selective-functions 还在维护吗?

最新版本 1.1.0 发布于 2022/04/13(4 年前);至今共发布 10 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/12/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。