Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/rolldown-plugin-dts. A Rolldown plugin to generate and bundle dts files.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346import { b } from 'yuku'import { is } from 'yuku/utils'import { filename_dts_to, RE_DTS } from '../filename.ts'import { isInfer, nameOf } from './utils.ts'import type { ChunkExportPlan } from './types.ts'import type * as t from 'yuku'
/** * patch `.d.ts` suffix in import source to `.js` */export function patchImportExport( node: t.ProgramStatement, exportPlan: ChunkExportPlan, cjsDefault: boolean,): t.ProgramStatement | false | undefined { if ( node.type === 'ExportNamedDeclaration' && !node.declaration && !node.source && !node.specifiers.length && !node.attributes?.length ) { return false }
if (node.type === 'ImportDeclaration' && node.specifiers.length) { for (const specifier of node.specifiers) { if (isInfer(specifier.local)) { specifier.local.name = '__Infer' } } }
if ( !is.oneOf(node, [ 'ImportDeclaration', 'ExportAllDeclaration', 'ExportNamedDeclaration', ]) ) { return }
if ( node.type === 'ExportAllDeclaration' && node.source && exportPlan.typeOnlyExportAllSources.has(node.source.value) ) { node.exportKind = 'type' }
if (node.type === 'ExportNamedDeclaration' && exportPlan.typeOnlyNames.size) { for (const spec of node.specifiers) { const name = nameOf(spec.exported) if (exportPlan.typeOnlyNames.has(name)) { if (spec.type === 'ExportSpecifier') { spec.exportKind = 'type' } else { node.exportKind = 'type' } } } normalizeTypeOnlyExport(node) }
if (node.source?.value && RE_DTS.test(node.source.value)) { node.source.value = filename_dts_to(node.source.value, 'js') return node }
// eslint-disable-next-line unicorn/prefer-early-return if ( cjsDefault && exportPlan.allowExportAssignment && node.type === 'ExportNamedDeclaration' && !node.source && node.specifiers.length === 1 && node.specifiers[0].type === 'ExportSpecifier' && nameOf(node.specifiers[0].exported) === 'default' ) { const defaultExport = node.specifiers[0] return b.TSExportAssignment({ expression: defaultExport.local, }) }}
function normalizeTypeOnlyExport(node: t.ExportNamedDeclaration): void { if (node.declaration || !node.specifiers.length) return
for (const specifier of node.specifiers) { if ( specifier.type !== 'ExportSpecifier' || specifier.exportKind !== 'type' ) { return } }
node.exportKind = 'type' for (const specifier of node.specifiers) { if (specifier.type === 'ExportSpecifier') { specifier.exportKind = 'value' } }}
/** * Handle `__exportAll` call */export function patchTsNamespace( nodes: t.ProgramStatement[],): t.ProgramStatement[] { const removed = new Set<t.Node>()
for (const [i, node] of nodes.entries()) { const result = getExportAllNamespace(node) if (!result) continue
const [binding, exports] = result if (!exports.properties.length) continue
const namespaceExport = b.ExportNamedDeclaration({ specifiers: exports.properties .filter((property) => property.type === 'Property') .map((property) => { const local = (property.value as t.ArrowFunctionExpression) .body as t.Identifier const exported = property.key as t.Identifier return b.ExportSpecifier({ local, exported }) }), }) nodes[i] = b.TSModuleDeclaration({ id: binding, body: b.TSModuleBlock({ body: [namespaceExport] }), kind: 'namespace', declare: true, }) }
return nodes.filter((node) => !removed.has(node))}
function getExportAllNamespace( node: t.ProgramStatement,): false | [t.Identifier, t.ObjectExpression] { if ( node.type !== 'VariableDeclaration' || node.declarations.length !== 1 || node.declarations[0].id.type !== 'Identifier' || node.declarations[0].init?.type !== 'CallExpression' || node.declarations[0].init.callee.type !== 'Identifier' || node.declarations[0].init.callee.name !== '__exportAll' || // rolldown >= 1.3 passes a flags argument for modules with an external `export *` node.declarations[0].init.arguments.length < 1 || node.declarations[0].init.arguments[0].type !== 'ObjectExpression' ) { return false }
const source = node.declarations[0].id
const exports = node.declarations[0].init.arguments[0] return [source, exports] as const}
/** * Handle `__reExport` call */export function patchReExport( nodes: t.ProgramStatement[],): t.ProgramStatement[] { const exportsNames = new Map<string, string>()
for (const [i, node] of nodes.entries()) { if ( node.type === 'ImportDeclaration' && node.specifiers.length === 1 && node.specifiers[0].type === 'ImportSpecifier' && node.specifiers[0].local.type === 'Identifier' && node.specifiers[0].local.name.endsWith('_exports') ) { // record: import { t as a_exports } from "..." exportsNames.set( node.specifiers[0].local.name, node.specifiers[0].local.name, ) } else if ( node.type === 'ExpressionStatement' && node.expression.type === 'CallExpression' && is.Identifier(node.expression.callee) && node.expression.callee.name === '__reExport' ) { // record: __reExport(a_exports, import_lib)
const args = node.expression.arguments exportsNames.set( (args[0] as t.Identifier).name, (args[1] as t.Identifier).name, ) } else if ( node.type === 'VariableDeclaration' && node.declarations.length === 1 && node.declarations[0].init?.type === 'MemberExpression' && node.declarations[0].init.object.type === 'Identifier' && exportsNames.has(node.declarations[0].init.object.name) ) { // var B = a_exports.A // to // type B = [mapping].A // TODO how to support value import? currently only type import is supported
nodes[i] = b.TSTypeAliasDeclaration({ id: b.Identifier({ name: (node.declarations[0].id as t.Identifier).name, }), typeAnnotation: b.TSTypeReference({ typeName: b.TSQualifiedName({ left: b.Identifier({ name: exportsNames.get(node.declarations[0].init.object.name)!, }), right: b.Identifier({ name: (node.declarations[0].init.property as t.Identifier).name, }), }), }), }) } else if ( node.type === 'ExportNamedDeclaration' && node.specifiers.length === 1 && node.specifiers[0].type === 'ExportSpecifier' && node.specifiers[0].local.type === 'Identifier' && exportsNames.has(node.specifiers[0].local.name) ) { // export { a_exports as t } // to // export { [mapping] as t } node.specifiers[0].local.name = exportsNames.get( node.specifiers[0].local.name, )! } }
return nodes}
// fix:// - import type { ... } from '...'// - import { type ... } from '...'// - export type { ... }// - export { type ... }// - export type * as x '...'// - import Foo = require("./bar")// - export = Foo// - export default xexport function rewriteImportExport( node: t.Node, set: (node: t.ProgramStatement) => void, appendStmts: t.ProgramStatement[],): node is t.ImportDeclaration | t.ExportAllDeclaration | t.TSImportEqualsDeclaration { if ( node.type === 'ImportDeclaration' || (node.type === 'ExportNamedDeclaration' && !node.declaration) ) { for (const specifier of node.specifiers) { if (specifier.type === 'ImportSpecifier') { specifier.importKind = 'value' } else if (specifier.type === 'ExportSpecifier') { specifier.exportKind = 'value' } }
if (node.type === 'ImportDeclaration') { node.importKind = 'value' } else if (node.type === 'ExportNamedDeclaration') { node.exportKind = 'value' }
return true } else if (node.type === 'ExportAllDeclaration') { node.exportKind = 'value' return true } else if ( node.type === 'TSImportEqualsDeclaration' || (node.type === 'ExportNamedDeclaration' && node.declaration?.type === 'TSImportEqualsDeclaration') ) { const decl = node.type === 'ExportNamedDeclaration' ? (node.declaration as t.TSImportEqualsDeclaration) : node
if (decl.moduleReference.type === 'TSExternalModuleReference') { set( b.ImportDeclaration({ specifiers: [b.ImportNamespaceSpecifier({ local: decl.id })], source: decl.moduleReference.expression, }), ) if (node.type === 'ExportNamedDeclaration') { appendStmts.push( b.ExportNamedDeclaration({ specifiers: [ b.ExportSpecifier({ local: decl.id, exported: decl.id }), ], }), ) } return true } } else if ( node.type === 'TSExportAssignment' && node.expression.type === 'Identifier' ) { set( b.ExportNamedDeclaration({ specifiers: [ b.ExportSpecifier({ local: node.expression, exported: b.Identifier({ name: 'default' }), }), ], }), ) return true } else if ( node.type === 'ExportDefaultDeclaration' && node.declaration.type === 'Identifier' ) { set( b.ExportNamedDeclaration({ specifiers: [ b.ExportSpecifier({ local: node.declaration, exported: b.Identifier({ name: 'default' }), }), ], }), ) return true }
return false}