| 575 | // For example, if only a .mjs was generated, also |
| 576 | // create the .js file that just calls the .mjs file |
| 577 | const patchModuleFormat = async (bundeName: string) => { |
| 578 | try { |
| 579 | const bundleFileName = sys.path.basename(bundeName); |
| 580 | const ext = sys.path.extname(bundleFileName); |
| 581 | const isEntryFile = |
| 582 | bundleFileName.startsWith('entry.') || bundleFileName.startsWith('entry_'); |
| 583 | if ( |
| 584 | isEntryFile && |
| 585 | !bundleFileName.includes('preview') && |
| 586 | (ext === '.mjs' || ext === '.cjs') |
| 587 | ) { |
| 588 | const extlessName = sys.path.basename(bundleFileName, ext); |
| 589 | const js = `${extlessName}.js`; |
| 590 | const moduleName = extlessName + ext; |
| 591 | |
| 592 | const hasJsScript = outputs.some((f) => sys.path.basename(f) === js); |
| 593 | if (!hasJsScript) { |
| 594 | // didn't generate a .js script |
| 595 | // create a .js file that just import()s their script |
| 596 | const bundleOutDir = sys.path.dirname(bundeName); |
| 597 | const fs: typeof import('fs') = await sys.dynamicImport('node:fs'); |
| 598 | |
| 599 | const folder = sys.path.join(opts.outDir, bundleOutDir); |
| 600 | await fs.promises.mkdir(folder, { recursive: true }); |
| 601 | await fs.promises.writeFile( |
| 602 | sys.path.join(folder, js), |
| 603 | `export * from "./${moduleName}";` |
| 604 | ); |
| 605 | } |
| 606 | } |
| 607 | } catch (e) { |
| 608 | console.error('patchModuleFormat', e); |
| 609 | } |
| 610 | }; |
| 611 | |
| 612 | await Promise.all(outputs.map(patchModuleFormat)); |
| 613 | } |