| 5 | import packageJson from './package.json' with { type: 'json' } |
| 6 | |
| 7 | function ensureImportFileExtension({ |
| 8 | content, |
| 9 | extension, |
| 10 | }: { |
| 11 | content: string |
| 12 | extension: string |
| 13 | }) { |
| 14 | // replace e.g. `import { foo } from './foo'` with `import { foo } from './foo.js'` |
| 15 | content = content.replace( |
| 16 | /(im|ex)port\s[\w{}/*\s,]+from\s['"](?:\.\.?\/)+?[^'"]+(?=['"];?)/gm, |
| 17 | (specifier) => |
| 18 | /\.(?:[cm]?js|json)$/.test(specifier) |
| 19 | ? specifier |
| 20 | : `${specifier}.${extension}`, |
| 21 | ) |
| 22 | |
| 23 | // replace e.g. `import('./foo')` with `import('./foo.js')` |
| 24 | content = content.replace( |
| 25 | /import\(['"](?:\.\.?\/)+?[^'"]+(?=['"];?)/gm, |
| 26 | (specifier) => |
| 27 | /\.(?:[cm]?js|json)$/.test(specifier) |
| 28 | ? specifier |
| 29 | : `${specifier}.${extension}`, |
| 30 | ) |
| 31 | return content |
| 32 | } |
| 33 | |
| 34 | const externalDependencies = Object.keys({ |
| 35 | ...packageJson.dependencies, |