Skip to content

Commit

Permalink
fix(rollup): fix stub export resolution issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 10, 2022
1 parent 0b39345 commit 63961e4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"magic-string": "^0.26.2",
"mkdirp": "^1.0.4",
"mkdist": "^0.3.13",
"mlly": "^0.5.8",
"mlly": "^0.5.9",
"mri": "^1.2.0",
"pathe": "^0.3.3",
"pkg-types": "^0.3.3",
Expand Down
15 changes: 10 additions & 5 deletions pnpm-lock.yaml

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

15 changes: 10 additions & 5 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { shebangPlugin, makeExecutable, getShebang } from './plugins/shebang'
// @ts-ignore https://github.com/unjs/unbuild/issues/23
const esbuild = _esbuild.default || _esbuild

const DEFAULT_EXTENSIONS = ['.ts', '.tsx', '.mjs', '.cjs', '.js', '.jsx', '.json']

export async function rollupBuild (ctx: BuildContext) {
if (ctx.options.stub) {
const jitiPath = await resolvePath('jiti', { url: import.meta.url })
Expand All @@ -41,7 +43,12 @@ export async function rollupBuild (ctx: BuildContext) {

// MJS Stub
// Try to analyze exports
const namedExports = await resolveModuleExportNames(resolvedEntry)
const namedExports = await resolveModuleExportNames(resolvedEntry, {
extensions: DEFAULT_EXTENSIONS
}).catch((err) => {
warn(ctx, `Cannot analyze ${resolvedEntry} for exports:` + err)
return []
})
await writeFile(output + '.mjs', `${shebang}import jiti from ${JSON.stringify(pathToFileURL(jitiPath).href)};\nconst _module = jiti(null, { interopDefault: true, esmResolve: true })('${resolvedEntry}');\n\nexport default _module;\n\n${namedExports.map(name => `export const ${name} = _module.${name};`).join('\n')}`)

// DTS Stub
Expand Down Expand Up @@ -103,8 +110,6 @@ export async function rollupBuild (ctx: BuildContext) {
}

export function getRollupOptions (ctx: BuildContext): RollupOptions {
const extensions = ['.ts', '.tsx', '.mjs', '.cjs', '.js', '.jsx', '.json']

return {
context: ctx.options.rootDir,

Expand Down Expand Up @@ -176,7 +181,7 @@ export function getRollupOptions (ctx: BuildContext): RollupOptions {
}),

ctx.options.rollup.resolve && nodeResolve({
extensions,
extensions: DEFAULT_EXTENSIONS,
...ctx.options.rollup.resolve
}),

Expand All @@ -191,7 +196,7 @@ export function getRollupOptions (ctx: BuildContext): RollupOptions {
}),

ctx.options.rollup.commonjs && commonjs({
extensions,
extensions: DEFAULT_EXTENSIONS,
...ctx.options.rollup.commonjs
}),

Expand Down

0 comments on commit 63961e4

Please sign in to comment.