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

Add preLoadModules option to import modules on main thread

Open astron8t-voyagerx opened this issue 10 months ago • 2 comments

Description

This commit adds 'preLoadModules' option that allows the main thread to load specified modules.

Motivation and Context

Some Node.js modules, such as 'sharp', are not safe to use with worker threads. Importing them in a worker thread can lead to crashes. There are several reported issues regarding this, for example, see: https://github.com/dherault/serverless-offline/issues/1603

And 'sharp' recommends importing the module on the main thread to prevent such problems. https://github.com/lovell/sharp/issues/2263#issuecomment-645523303

While the 'useInProcess' option allows us to avoid using threading, it also disables hot reloading, which significantly reduces convenience. This commit implements the 'preLoadModules' option (e.g., preLoadModules: "sharp, canvas") to import these modules before creating worker threads.

How Has This Been Tested?

I've tested in my own project using serverless offline, and also conducted tests with Mocha in this repository.

astron8t-voyagerx avatar Apr 23 '24 17:04 astron8t-voyagerx

I've added an explanation document about the cli parameter in the README. Could you please check this again? @dherault

astron8t-voyagerx avatar Aug 19 '24 14:08 astron8t-voyagerx

I will merge this soon, sorry for the delay.

dherault avatar Aug 29 '24 15:08 dherault