Way to disable packagerOptions when running serverless offline
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?
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}
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()],
});