Skip to content

Include esbuild files besides the handler #13163

@ewsbr

Description

@ewsbr

Issue description

I'm currently migrating from Serverless V3 to V4, and one of the blockers today seems to be the inability to include files besides the handler in the esbuild package. This StackOverflow user has a similar issue.

I have the following configuration:

const fs = require('fs');
const { copy } = require('esbuild-plugin-copy');

// inspired by https://github.com/evanw/esbuild/issues/1685
const excludeVendorFromSourceMap = (includes = []) => ({
  name: 'excludeVendorFromSourceMap',
  setup(build) {
    const emptySourceMap = '\n//# sourceMappingURL=data:application/json;base64,' + Buffer.from(JSON.stringify({
      version  : 3,
      sources  : [''],
      mappings : 'A'
    })).toString('base64');

    build.onLoad({ filter: /node_modules.+\.(js|ts)$/ }, (args) => {
      return {
        contents: `${fs.readFileSync(args.path, 'utf8')}\n//# sourceMappingURL=data:application/json;base64,${emptySourceMap}`,
        loader: 'default'
      };
    });
  }
});

/** @type {() => import('esbuild').BuildOptions} */
module.exports = () => ({
  bundle    : true,
  minify    : false,
  sourcemap : true,
  format    : 'cjs',
  exclude   : [
    'better-sqlite3',
    'mysql2',
    'mysql',
    'tedious',
    'sqlite3',
    'pg-query-stream',
    'oracledb',
    'canvas'
  ],
  loader: {
    '.json': 'copy',
  },
  plugins: [
    excludeVendorFromSourceMap(),
    copy({
      resolveFrom: 'cwd',
      assets: [
        {
          from: './src/api/locales/*.json',
          to: './.serverless/build/src/locales/',
        },
        {
          from: './src/api/locales/*.json',
          to: './.serverless/build/src/workers/locales/',
        },
        {
          from: './src/assets/**',
          to: './.serverless/build/src/assets/',
        },
        {
          from: './src/assets/**',
          to: './.serverless/build/src/workers/assets/',
        }
      ],
    }),
  ]
});

Output inside .serverless:

build
  - locales
    - X.json
  - api-XXXXXXX.json
  - db-XXXXXXX.json
  - handler.js
  - handler.js.map

Some JSON and other asset files are manually copied via esbuild (locales, for example). This used to work fine with serverless-esbuild, however when I switch to the native esbuild plugin, they're no longer copied.

Is there a way to ensure everything from the build output is copied? I've attempted to add include patterns to serverless.yml to no avail:

package:
  individually: true
  excludeDevDependencies: true
  exclude:
    - .vscode/**
    - .git/**
    - .gitignore
  patterns:
    - 'src/api/locales/**'
    - '.serverless/build/src/**' # The folder gets included, but instead of being inside /src/* it keeps the .serverless path

What approach should I take from here?
From what I've seen, the _package and _packageAll functions in the native plugin don't support this. I was thinking about just monkey-patching them with another plugin, but it doesn't seem optimal.

Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions