@@ -10,7 +10,7 @@ import type {
1010import fs from 'node:fs' ;
1111import { cpus as nodeCpus } from 'node:os' ;
1212import { Worker } from 'node:worker_threads' ;
13- import { isAbsolute , resolve } from 'node:path' ;
13+ import { dirname , extname , isAbsolute , join , resolve } from 'node:path' ;
1414import { ensureDir } from './node-system' ;
1515import { normalizePath } from '../../utils/fs' ;
1616import { createSingleThreadWorker } from '../worker-thread' ;
@@ -78,14 +78,23 @@ export async function createNodeMainProcess(sys: System, opts: SsgOptions) {
7878
7979 let workerFilePath : string | URL ;
8080
81+ // Launch the worker using the package's index module, which bootstraps the worker thread.
8182 if ( typeof __filename === 'string' ) {
82- workerFilePath = __filename ;
83+ // CommonJS path
84+ const ext = extname ( __filename ) || '.js' ;
85+ workerFilePath = join ( dirname ( __filename ) , `index${ ext } ` ) ;
8386 } else {
84- workerFilePath = import . meta. url ;
85- }
87+ // ESM path (import.meta.url)
88+ const thisUrl = new URL ( import . meta. url ) ;
89+ const pathname = thisUrl . pathname || '' ;
90+ let ext = '.js' ;
91+ if ( pathname . endsWith ( '.ts' ) ) {
92+ ext = '.ts' ;
93+ } else if ( pathname . endsWith ( '.mjs' ) ) {
94+ ext = '.mjs' ;
95+ }
8696
87- if ( typeof workerFilePath === 'string' && workerFilePath . startsWith ( 'file://' ) ) {
88- workerFilePath = new URL ( workerFilePath ) ;
97+ workerFilePath = new URL ( `./index${ ext } ` , thisUrl ) ;
8998 }
9099
91100 const nodeWorker = new Worker ( workerFilePath , { workerData : opts } ) ;
@@ -146,7 +155,7 @@ export async function createNodeMainProcess(sys: System, opts: SsgOptions) {
146155 } ) ;
147156
148157 nodeWorker . on ( 'exit' , ( code ) => {
149- if ( code !== 1 ) {
158+ if ( code !== 0 ) {
150159 console . error ( `worker exit ${ code } ` ) ;
151160 }
152161 } ) ;
0 commit comments