Skip to content

Commit

Permalink
feat!: always enable esmResolve for jiti (stub and config)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 10, 2022
1 parent ffc0d7c commit b87c8df
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/builder/rollup.ts
Expand Up @@ -34,10 +34,10 @@ export async function rollupBuild (ctx: BuildContext) {

await mkdir(dirname(output), { recursive: true })
if (ctx.options.rollup.emitCJS) {
await writeFile(output + '.cjs', `${shebang}module.exports = require(${JSON.stringify(jitiPath)})(null, { interopDefault: true })('${entry.input}')`)
await writeFile(output + '.cjs', `${shebang}module.exports = require(${JSON.stringify(jitiPath)})(null, { interopDefault: true, esmResolve: true })('${entry.input}')`)
}
// Use file:// protocol for windows compatibility
await writeFile(output + '.mjs', `${shebang}import jiti from ${JSON.stringify(pathToFileURL(jitiPath).href)};\nexport default jiti(null, { interopDefault: true })('${entry.input}');`)
await writeFile(output + '.mjs', `${shebang}import jiti from ${JSON.stringify(pathToFileURL(jitiPath).href)};\nexport default jiti(null, { interopDefault: true, esmResolve: true })('${entry.input}');`)
await writeFile(output + '.d.ts', `export * from '${entry.input}';\nexport { default } from '${entry.input}';`)

if (shebang) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Expand Up @@ -63,7 +63,7 @@ export function listRecursively (path: string) {
}

export function tryRequire (id: string, rootDir: string = process.cwd()) {
const _require = jiti(rootDir, { interopDefault: true })
const _require = jiti(rootDir, { interopDefault: true, esmResolve: true })
try {
return _require(id)
} catch (err: any) {
Expand All @@ -75,7 +75,7 @@ export function tryRequire (id: string, rootDir: string = process.cwd()) {
}

export function tryResolve (id: string, rootDir: string = process.cwd()) {
const _require = jiti(rootDir, { interopDefault: true })
const _require = jiti(rootDir, { interopDefault: true, esmResolve: true })
try {
return _require.resolve(id)
} catch (err: any) {
Expand Down

0 comments on commit b87c8df

Please sign in to comment.