Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preLoadModules option to import modules on main thread #1777

Merged
merged 3 commits into from
Aug 30, 2024

Conversation

astron8t-voyagerx
Copy link
Contributor

@astron8t-voyagerx astron8t-voyagerx commented Apr 23, 2024

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:
#1603

And 'sharp' recommends importing the module on the main thread to prevent such problems.
lovell/sharp#2263 (comment)

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.

Copy link
Owner

@dherault dherault left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. The code is good, but it's missing documentation.

@astron8t-voyagerx
Copy link
Contributor Author

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

@dherault
Copy link
Owner

I will merge this soon, sorry for the delay.

@dherault dherault merged commit f49f10f into dherault:master Aug 30, 2024
0 of 9 checks passed
dherault added a commit that referenced this pull request Aug 30, 2024
Add preLoadModules option to import modules on main thread
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants