如何在使用无服务器时只打包生产依赖项

How can I only package production dependencies when using serverless

我正在使用 serverless 来打包由 typescript 实现的 lambda。我已经定义了 serverless.yml,我发现 serverless deploy 将压缩所有 node_modules 目录并作为 lambda 文件上传到 s3 存储桶。我的 pakcage.json 有 dependenciesdevDependencies,我想排除开发部门。我知道我可以做如下的事情:

package:
  exclude:
    - node_modules/**
    - '!node_modules/node-fetch/**'

但我必须将它们一一排除,这不是一个好主意。

我能想到的另一种方法是 运行 在 docker 容器中构建和部署命令,其中仅下载生产依赖项。但是,它有点慢,因为每次我都必须旋转一个容器和 运行 yarn install --only=production 命令来下载这些依赖项。

所以我正在寻找更好的解决方案来解决这个问题。

我认为您可以像这样排除开发部门。

package:
  excludeDevDependencies: true

启动 docker 并从中安装 deps 实际上是一个更好的主意。即使在部署之前需要更多时间来启动它。一些部门可能已经编译了二进制文件,如果你在本地安装它们然后部署,它们可能无法在远程服务器上工作。

除此之外,您是否尝试过 parcel bundler 和 serverless-simple-parcel 插件? 这是它的示例配置(进入 serverless.yml 进入自定义块):

custom:
  parcel:
    entries:
      - file: src/handler.ts
        target: node
        outFile: handler.js
    options:
      publicUrl: .