@@ -85,14 +85,21 @@ export function createEnhancedRequire(
8585 path.resolve('${ currentWorkingDir } ', id),
8686 path.resolve(process.cwd(), id),
8787 path.resolve('${ userProjectDir } ', id),
88- path.resolve('${ userProjectDir } ', 'examples', id),
89- path.resolve('${ userProjectDir } ', 'examples', 'core', 'goroutines', id),
9088 path.resolve('${ currentWorkingDir } ', 'core', 'goroutines', id),
9189 ];
9290
9391 for (const modulePath of possiblePaths) {
9492 try {
95- return originalRequire(modulePath);
93+ const module = originalRequire(modulePath);
94+ // Apply smart proxy for .default access
95+ return new Proxy(module, {
96+ get(target, prop) {
97+ if (prop === 'default') {
98+ return target.default !== undefined ? target.default : target;
99+ }
100+ return target[prop];
101+ }
102+ });
96103 } catch (localError) {
97104 // Continue to next path
98105 }
@@ -104,16 +111,23 @@ export function createEnhancedRequire(
104111 // For npm packages, try node_modules directories in this order:
105112 const possibleNodeModulesPaths = [
106113 path.resolve('${ userProjectDir } ', 'node_modules', id),
107- path.resolve('${ userProjectDir } ', 'examples', 'node_modules', id),
108114 path.resolve('${ currentWorkingDir } ', 'node_modules', id),
109115 path.resolve('${ currentWorkingDir } ', '..', 'node_modules', id),
110116 path.resolve('${ currentWorkingDir } ', '..', '..', 'node_modules', id),
111- path.resolve('${ currentWorkingDir } ', '..', '..', 'examples', 'node_modules', id),
112117 ];
113118
114119 for (const modulePath of possibleNodeModulesPaths) {
115120 try {
116- return originalRequire(modulePath);
121+ const module = originalRequire(modulePath);
122+ // Apply smart proxy for .default access
123+ return new Proxy(module, {
124+ get(target, prop) {
125+ if (prop === 'default') {
126+ return target.default !== undefined ? target.default : target;
127+ }
128+ return target[prop];
129+ }
130+ });
117131 } catch (moduleError) {
118132 // Continue to next path
119133 }
@@ -125,6 +139,24 @@ export function createEnhancedRequire(
125139 ` ;
126140}
127141
142+ /**
143+ * Transform import() calls to use our custom import function
144+ * This preserves the await import() syntax while making it work in worker threads
145+ */
146+ function transformImportCalls ( userFunction : string ) : string {
147+ // Replace await import('module') with await customImport('module')
148+ // This regex matches: await import('module') or await import("module")
149+ let transformed = userFunction ;
150+
151+ // Replace import() calls with customImport() calls
152+ transformed = transformed . replace (
153+ / a w a i t \s + i m p o r t \s * \( \s * [ ' " ` ] ( [ ^ ' " ` ] + ) [ ' " ` ] \s * \) / g,
154+ 'await customImport("$1")'
155+ ) ;
156+
157+ return transformed ;
158+ }
159+
128160/**
129161 * Create execution environment code
130162 */
@@ -133,12 +165,11 @@ export function createExecutionEnvironment(
133165 userProjectDir : string ,
134166 userFunction : string
135167) : string {
136- const enhancedRequire = createEnhancedRequire (
137- currentWorkingDir ,
138- userProjectDir
139- ) ;
140168 const contextProxy = createContextProxy ( ) ;
141169
170+ // Transform import() calls to require() calls
171+ const transformedFunction = transformImportCalls ( userFunction ) ;
172+
142173 return `
143174 (async function(...args) {
144175 // Ensure essential globals are available
@@ -311,14 +342,105 @@ export function createExecutionEnvironment(
311342 });
312343 };
313344
314- // Set up module resolution
315- ${ enhancedRequire }
345+
346+
347+ // Create a custom import function that works in worker threads
348+ // This mimics the behavior of ES module dynamic imports
349+ var customImport = async function(id) {
350+ const path = require('path');
351+
352+ // Handle Node.js built-in modules (node:fs, node:path, etc.)
353+ if (id.startsWith('node:')) {
354+ const moduleName = id.substring(5); // Remove 'node:' prefix
355+ try {
356+ const module = require(moduleName);
357+ // For built-in modules, return { default: module, ...module }
358+ return { default: module, ...module };
359+ } catch (error) {
360+ throw new Error('Built-in module not found: ' + moduleName);
361+ }
362+ }
363+
364+ // Handle local files (relative paths)
365+ if (id.startsWith('./') || id.startsWith('../') || id.endsWith('.js')) {
366+ const possiblePaths = [
367+ path.resolve('${ currentWorkingDir } ', id),
368+ path.resolve(process.cwd(), id),
369+ path.resolve('${ userProjectDir } ', id),
370+ path.resolve('${ currentWorkingDir } ', 'core', 'goroutines', id),
371+ ];
372+
373+ for (const modulePath of possiblePaths) {
374+ try {
375+ // For .js files, try to use dynamic import first (ES modules)
376+ if (id.endsWith('.js')) {
377+ try {
378+ // Use dynamic import for ES modules
379+ const module = await import(modulePath);
380+ return module;
381+ } catch (importError) {
382+ // If dynamic import fails, fall back to require (CommonJS)
383+ const module = require(modulePath);
384+ if (module.default === undefined) {
385+ return { default: module, ...module };
386+ } else {
387+ return { default: module.default, ...module };
388+ }
389+ }
390+ } else {
391+ // For .cjs files, use require (CommonJS)
392+ const module = require(modulePath);
393+ if (module.default === undefined) {
394+ return { default: module, ...module };
395+ } else {
396+ return { default: module.default, ...module };
397+ }
398+ }
399+ } catch (localError) {
400+ // Continue to next path
401+ }
402+ }
403+
404+ throw new Error('Module not found: ' + id);
405+ }
406+
407+ // For npm packages, try node_modules directories in this order:
408+ const possibleNodeModulesPaths = [
409+ path.resolve('${ userProjectDir } ', 'node_modules', id),
410+ path.resolve('${ currentWorkingDir } ', 'node_modules', id),
411+ path.resolve('${ currentWorkingDir } ', '..', 'node_modules', id),
412+ path.resolve('${ currentWorkingDir } ', '..', '..', 'node_modules', id),
413+ ];
414+
415+ for (const modulePath of possibleNodeModulesPaths) {
416+ try {
417+ const module = require(modulePath);
418+ // Always return an object with default property for consistency
419+ // This supports both: await import('moment') and (await import('moment')).default
420+ if (module.default === undefined) {
421+ // CommonJS module - return { default: module, ...module }
422+ return { default: module, ...module };
423+ } else {
424+ // ES module - return { default: module.default, ...module }
425+ return { default: module.default, ...module };
426+ }
427+ } catch (moduleError) {
428+ // Continue to next path
429+ }
430+ }
431+
432+ throw new Error('Package not found: ' + id);
433+ };
434+
435+ // Make the custom import function available globally
436+ // Users can call customImport('moment') instead of import('moment')
437+ globalThis.customImport = customImport;
316438
317439 // Resolve function arguments and handle context objects
318440 ${ contextProxy }
319441
320- // Create the user function
321- ${ userFunction }
442+ // Create the user function (with transformed import calls)
443+ ${ transformedFunction }
322444
323445 // Execute the function with the resolved arguments
324446 return await fn(...resolvedArgs);
0 commit comments