diff --git a/src/service/transformer/declaration-bundler/transformers/module-merger/visitor/visit-export-declaration.ts b/src/service/transformer/declaration-bundler/transformers/module-merger/visitor/visit-export-declaration.ts index 04b6e940..f77d2009 100644 --- a/src/service/transformer/declaration-bundler/transformers/module-merger/visitor/visit-export-declaration.ts +++ b/src/service/transformer/declaration-bundler/transformers/module-merger/visitor/visit-export-declaration.ts @@ -7,6 +7,8 @@ import {cloneLexicalEnvironment} from "../../../util/clone-lexical-environment"; import {ensureNoDeclareModifierTransformer} from "../../ensure-no-declare-modifier-transformer/ensure-no-declare-modifier-transformer"; import {statementMerger} from "../../statement-merger/statement-merger"; import {inlineNamespaceModuleBlockTransformer} from "../../inline-namespace-module-block-transformer/inline-namespace-module-block-transformer"; +import {addBindingToLexicalEnvironment} from "../../../util/add-binding-to-lexical-environment"; +import {getOriginalSourceFile} from "../../../util/get-original-source-file"; export interface GenerateExportDeclarationsOptions extends Omit, "node"> {} @@ -75,7 +77,7 @@ function generateExportDeclarations(options: GenerateExportDeclarationsOptions, } export function visitExportDeclaration(options: ModuleMergerVisitorOptions): VisitResult { - const {node, factory, typescript} = options; + const {node, factory, typescript, sourceFile} = options; const moduleSpecifier = node.moduleSpecifier == null || !typescript.isStringLiteralLike(node.moduleSpecifier) ? undefined : node.moduleSpecifier.text; const updatedModuleSpecifier = moduleSpecifier == null @@ -87,7 +89,6 @@ export function visitExportDeclaration(options: ModuleMergerVisitorOptions { + const bundle = await generateRollupBundle( + [ + { + entry: true, + fileName: "index.ts", + text: `\ + import Foo from "./foo"; + + export { Foo }; + ` + }, + { + entry: false, + fileName: "foo.ts", + text: `\ + export { default } from "./bar"; + ` + }, + { + entry: false, + fileName: "bar.ts", + text: `\ + export default 2; + ` + } + ], + { + typescript, + debug: false + } + ); + const { + declarations: [file] + } = bundle; + + t.deepEqual( + formatCode(file.code), + formatCode(`\ + declare const _default: 2; + declare const Foo: typeof _default; + export { Foo }; + `) + ); +}); + +test.serial("Handles default exports inside ExportSpecifiers. #6", withTypeScript, async (t, {typescript}) => { + const bundle = await generateRollupBundle( + [ + { + entry: true, + fileName: "index.ts", + text: `\ + import Foo from "./foo"; + + export { Foo }; + ` + }, + { + entry: false, + fileName: "foo.ts", + text: `\ + export { A as default } from "./bar"; + ` + }, + { + entry: false, + fileName: "bar.ts", + text: `\ + const foo = 2; + export {foo as A}; + ` + } + ], + { + typescript, + debug: false + } + ); + const { + declarations: [file] + } = bundle; + + t.deepEqual( + formatCode(file.code), + formatCode(`\ + declare const foo = 2; + declare const Foo: typeof foo; + export { Foo }; + `) + ); +}); + +test.serial("Handles default exports inside ExportSpecifiers. #7", withTypeScript, async (t, {typescript}) => { + const bundle = await generateRollupBundle( + [ + { + entry: true, + fileName: "index.ts", + text: `\ + import {A} from "./foo"; + + export { A }; + ` + }, + { + entry: false, + fileName: "foo.ts", + text: `\ + export { A } from "./bar"; + ` + }, + { + entry: false, + fileName: "bar.ts", + text: `\ + const foo = 2; + export {foo as A}; + ` + } + ], + { + typescript, + debug: false + } + ); + const { + declarations: [file] + } = bundle; + + t.deepEqual( + formatCode(file.code), + formatCode(`\ + declare const foo = 2; + export { foo as A }; + `) + ); +}); + +test.serial("Handles default exports inside ExportSpecifiers. #8", withTypeScript, async (t, {typescript}) => { + const bundle = await generateRollupBundle( + [ + { + entry: true, + fileName: "index.ts", + text: `\ + import {A} from "./foo"; + + const B = A; + export { A as ARenamed, B }; + ` + }, + { + entry: false, + fileName: "foo.ts", + text: `\ + export { A } from "./bar"; + ` + }, + { + entry: false, + fileName: "bar.ts", + text: `\ + const foo = 2; + export {foo as A}; + ` + } + ], + { + typescript, + debug: false + } + ); + const { + declarations: [file] + } = bundle; + + t.deepEqual( + formatCode(file.code), + formatCode(`\ + declare const foo = 2; + declare const B = 2; + export { foo as ARenamed, B } + `) + ); +}); \ No newline at end of file