Skip to content

Commit

Permalink
chore: backup export
Browse files Browse the repository at this point in the history
  • Loading branch information
yejimeiming committed Oct 8, 2023
1 parent 2eefcbc commit 63324c2
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/generate-export-v0.9.0.ts
@@ -0,0 +1,42 @@
import type { Analyzed } from './analyze'

export interface ExportsRuntime {
polyfill: string
exportDeclaration: string
}

export function generateExport(analyzed: Analyzed): ExportsRuntime | null {
if (!analyzed.exports.length) {
return null
}

const memberDefault = analyzed.exports
// Find `module.exports` or `exports.default`
.find(exp => exp.token.left === 'module' || exp.token.right === 'default')

let members = analyzed.exports
// Exclude `module.exports` and `exports.default`
.filter(exp => exp.token.left !== 'module' && exp.token.right !== 'default')
.map(exp => exp.token.right)
// Remove duplicate export
members = [...new Set(members)]

const membersDeclaration = members.map(
m => `const __CJS__export_${m}__ = (module.exports == null ? {} : module.exports).${m}`,
)
const membersExport = members.map(m => `__CJS__export_${m}__ as ${m}`)
if (memberDefault) {
membersDeclaration.unshift(`const __CJS__export_default__ = (module.exports == null ? {} : module.exports).default || module.exports`)
membersExport.unshift('__CJS__export_default__ as default')
}

return {
polyfill: 'var module = { exports: {} }; var exports = module.exports;',
exportDeclaration: `
${membersDeclaration.join(';\n')};
export {
${membersExport.join(',\n ')},
}
`.trim(),
}
}

0 comments on commit 63324c2

Please sign in to comment.