serverless-esbuild icon indicating copy to clipboard operation
serverless-esbuild copied to clipboard

Way to disable packagerOptions when running serverless offline

Open andidev opened this issue 3 years ago • 1 comments

Is there a way to supply or disable packagerOptions in
serverless.yml

  esbuild:
    packagerOptions:
      scripts:
        - rm -rf node_modules/sharp
        - SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install --arch=x64 --platform=linux --libc=glibc sharp

when running sls offline?

Problem and solution is described in sharp repo here. But the solution is just commenting the packagerOptions in serverless.yml whenever you run your code with sls offline and that really feels like a bad solution. Is there a way to solve this issue?

andidev avatar Sep 20 '22 14:09 andidev

A solution would maybe to add one more packagerOptions called disableScripts and then its possible to disable it with sls offilne --param="offline=true" and set it based on the command line param

  esbuild:
    packagerOptions:
      scripts:
        - rm -rf node_modules/sharp
        - SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install --arch=x64 --platform=linux --libc=glibc sharp
      disableScripts: ${params: offline}

andidev avatar Sep 20 '22 14:09 andidev

Closing this since managed to solve it with two different config files and then call sls deploy --param=ESBUILD_CONFIG=esbuild.config.js when deploying it and sls offline --param=ESBUILD_CONFIG=esbuild.offline.config.js

serverless.yml

custom
  esbuild:
    config: ${param:ESBUILD_CONFIG}

esbuild.config.js

module.exports = (serverless) => ({
    minify: true,
    exclude: ['@aws/sdk'],
    bundle: true,
    sourcemap: true,
    keepNames: true,
    packagerOptions: {
        scripts: [
            "rm -rf node_modules/sharp",
            "SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install --arch=x64 --platform=linux --libc=glibc sharp" // see https://sharp.pixelplumbing.com/install#esbuild
        ]
    },
    external: ['sharp'],  // Solves "No loader is configured for ".node" files: node_modules/sharp/build/Release/sharp-linux-x64.node" for sharp dependency, see https://sharp.pixelplumbing.com/install#esbuild
});

esbuild.offline.config.js

const { nodeExternalsPlugin } = require('esbuild-node-externals');

module.exports = (serverless) => ({
    minify: false,
    exclude: [],
    bundle: true,
    sourcemap: true,
    keepNames: true,
    plugins: [nodeExternalsPlugin()],
});

andidev avatar Sep 11 '24 03:09 andidev