Skip to content

Commit

Permalink
feat(cli): Use ESM worker entry file only when import config is pro…
Browse files Browse the repository at this point in the history
…vided
  • Loading branch information
tommy351 committed Jun 10, 2024
1 parent 407890e commit 35b4773
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/cli/src/commands/generate/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ async function importEnvNode(cwd: string): Promise<Environment[]> {

const envs: Environment[] = [];

// Global `require` is only available in CommonJS environment. We don't use
// `createRequire` here because it will create a new instance of `require`,
// which doesn't share the same cache with the global `require`.
if (BUILD_FORMAT === "cjs") {
// eslint-disable-next-line @typescript-eslint/no-var-requires
envs.push(require(envPath));
Expand Down
14 changes: 12 additions & 2 deletions packages/cli/src/commands/generate/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,18 @@ async function runWithLoaders(options: WorkerOptions) {
...options.config.loaders.flatMap((loader) => ["--loader", loader]),
// ESM import
...options.config.import.flatMap((imp) => ["--import", imp]),

Check warning on line 98 in packages/cli/src/commands/generate/worker.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/commands/generate/worker.ts#L98

Added line #L98 was not covered by tests
// Entry file. Always use ESM entry file.
join(fileURLToPath(import.meta.url), "../worker-bin.node.mjs")
// Entry file. Always use ESM entry file when `import` is provided
// because of the following error, which seems to happen only when using
// `@swc-node/register/esm-register`.
//
// ReferenceError: require is not defined in ES module scope, you can use import instead
//
// Otherwise, use CJS entry file because it supports both `require` and
// `import`.
join(
fileURLToPath(import.meta.url),
"../worker-bin.node." + (options.config.import.length ? "mjs" : "cjs")
)
],
{
stdio: ["pipe", "inherit", "inherit"],
Expand Down

0 comments on commit 35b4773

Please sign in to comment.