Skip to content

Commit

Permalink
refactor: use jiti.import for esm stubs and improve templates (#300)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
antfu and pi0 authored Oct 30, 2023
1 parent 2e487c3 commit 974caff
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"esbuild": "^0.19.5",
"globby": "^13.2.2",
"hookable": "^5.5.3",
"jiti": "^1.20.0",
"jiti": "^1.21.0",
"magic-string": "^0.30.5",
"mkdist": "^1.3.0",
"mlly": "^1.4.2",
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,17 @@ export async function rollupBuild(ctx: BuildContext) {
if (ctx.options.rollup.emitCJS) {
await writeFile(
output + ".cjs",
`${shebang}module.exports = require(${JSON.stringify(
jitiPath,
)})(null, ${serializedJitiOptions})(${JSON.stringify(
resolvedEntry,
)})`,
shebang +
[
`const jiti = require(${JSON.stringify(jitiPath)})`,
"",
`const _jiti = jiti(null, ${serializedJitiOptions})`,
"",
`/** @type {import(${JSON.stringify(
resolvedEntryWithoutExt,
)})} */`,
`module.exports = _jiti(${JSON.stringify(resolvedEntry)})`,
].join("\n"),
);
}

Expand All @@ -106,8 +112,10 @@ export async function rollupBuild(ctx: BuildContext) {
[
`import jiti from ${JSON.stringify(pathToFileURL(jitiPath).href)};`,
"",
`const _jiti = jiti(null, ${serializedJitiOptions})`,
"",
`/** @type {import(${JSON.stringify(resolvedEntryWithoutExt)})} */`,
`const _module = jiti(null, ${serializedJitiOptions})(${JSON.stringify(
`const _module = await _jiti.import(${JSON.stringify(
resolvedEntry,
)});`,
hasDefaultExport ? "\nexport default _module;" : "",
Expand Down

0 comments on commit 974caff

Please sign in to comment.