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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ Default: 600 (10 minutes)
WebSocket port to listen on.<br />
Default: 3001

#### preLoadModules

Pre-load specified modules in the main thread to avoid crashes when importing in worker threads. Provide module names as a comma-separated list (e.g., "sharp,canvas").<br />
Default: ''

Any of the CLI options can be added to your `serverless.yml`. For example:

```yml
Expand Down
18 changes: 18 additions & 0 deletions src/ServerlessOffline.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export default class ServerlessOffline {
async start() {
this.#mergeOptions()

this.#preLoadModules()

if (this.#cliOptions.noSponsor) {
log.notice()
} else {
Expand Down Expand Up @@ -423,6 +425,18 @@ export default class ServerlessOffline {
}
}

#preLoadModules() {
const modules = this.#options.preLoadModules.split(",")

modules.forEach((module) => {
try {
import(module)
} catch (error) {
log.error(`Error importing module ${module}: ${error}`)
}
})
}

// TODO FIXME
// TEMP quick fix to expose for testing, look for better solution
internals() {
Expand All @@ -446,6 +460,10 @@ export default class ServerlessOffline {
mergeOptions: () => {
this.#mergeOptions()
},

preLoadModules: () => {
this.#preLoadModules()
},
}
}
}
4 changes: 4 additions & 0 deletions src/config/commandOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,8 @@ export default {
type: "string",
usage: "Websocket port to listen on. Default: 3001.",
},
preLoadModules: {
type: "string",
usage: "A comma separated list of modules to preload on the main thread",
},
}
1 change: 1 addition & 0 deletions src/config/defaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export default {
webSocketHardTimeout: 7200,
webSocketIdleTimeout: 600,
websocketPort: 3001,
preLoadModules: '',
}
Loading