Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for auto renaming nodes with the same name in the bundle #116

Closed
gfmio opened this issue May 11, 2020 · 5 comments · Fixed by #268 or #270
Closed

Support for auto renaming nodes with the same name in the bundle #116

gfmio opened this issue May 11, 2020 · 5 comments · Fixed by #268 or #270
Assignees
Labels
Milestone

Comments

@gfmio
Copy link

gfmio commented May 11, 2020

Bug report

Input code

// template1.ts
const TEMPLATE = "template1";
export default TEMPLATE;
// template2.ts
const TEMPLATE = "template2";
export default TEMPLATE;
// index.ts
export {default as TEMPLATE1} from "./template1";
export {default as TEMPLATE2} from "./template2";

Expected output

// Generated by dts-bundle-generator v4.3.0

export declare const TEMPLATE1 = "TEMPLATE1";
export declare const TEMPLATE2 = "TEMPLATE2";

export {};

or

// Generated by dts-bundle-generator v4.3.0

declare const TEMPLATE$1 = "TEMPLATE1"
declare const TEMPLATE$2 = "TEMPLATE2"

export declare const TEMPLATE1 = TEMPLATE$1;
export declare const TEMPLATE2 = TEMPLATE$2;

export {};

Actual output

// Generated by dts-bundle-generator v4.3.0

export declare const TEMPLATE = "TEMPLATE1";
export declare const TEMPLATE = "TEMPLATE2";

export {};

Console output:

$ dts-bundle-generator ./src/index.ts
Compiling input files...
Processing ./src/index.ts
Writing ./src/index.ts -> src/index.d.ts
Checking generated files...
src/index.d.ts(3,22): error TS2451: Cannot redeclare block-scoped variable 'TEMPLATE'.
src/index.d.ts(4,22): error TS2451: Cannot redeclare block-scoped variable 'TEMPLATE'.

Error: Compiled with errors
error Command failed with exit code 1.

Additional context
Add any other context about the problem here (CLI options, etc)

dts-bundle-generator will throw errors when you use the same variable name more than once. This is presumably, because the script only concatenates the files and does not perform an analysis of name collisions.

However, the API surface of the module is also incorrect, because the wrong names are exported. I'm not sure if this is just due to the name clash or because of an underlying issue.

I'm using dts-bundle-generator 4.3.0 and typescript 3.8.3.

@gfmio
Copy link
Author

gfmio commented May 11, 2020

I just renamed my variables and the bundle is constructed properly, but the exported names are still wrong.

Input code

// template1.ts
const TEMPLATE$1 = "template1";
export default TEMPLATE$1;
// template2.ts
const TEMPLATE$2 = "template2";
export default TEMPLATE$2;
// index.ts
export {default as TEMPLATE1} from "./template1";
export {default as TEMPLATE2} from "./template2";

Actual output:

// Generated by dts-bundle-generator v4.3.0

export declare const TEMPLATE$1 = "TEMPLATE1"
export declare const TEMPLATE$2 = "TEMPLATE2"

export {};

@timocov
Copy link
Owner

timocov commented May 12, 2020

It looks like the second limitation, isn't it? I'll check what we can do here, but for now you can use workaround:

// index.ts
import TEMPLATE1 from "./template1";
import TEMPLATE2 from "./template2";

export { TEMPLATE1, TEMPLATE2 };

(and the same names in template files)

@timocov timocov added the Bug label May 12, 2020
@gfmio
Copy link
Author

gfmio commented May 12, 2020

Maybe I misunderstood the 2nd limitation, but I thought that only applies when re-exporting a default export as a default export.

The workaround worked though, and it actually still works, even if both variables are named TEMPLATE as long as they are imported using and exported with different names.

@timocov
Copy link
Owner

timocov commented May 12, 2020

Maybe I misunderstood the 2nd limitation, but I thought that only applies when re-exporting a default export as a default export.

Yeah, I've thought it is so 😂 Looks like the issue is with default exports in common.

From the beginning dts-bundle-generator never changed the original names of classes/variables/etc, that's why you faced this issue (and I personally never though that it's a big deal to fix it in your code because it's just renaming and you can handle error while generating the bundle). Maybe it needs to be changed and start renaming them somehow, but I guess it isn't easy to do (I'd like to use TypeScript API here, but afaik there is no API for that).

It might be I can (easily) fix the issue from #116 (comment) in the way:

declare const TEMPLATE$1 = "TEMPLATE1";
declare const TEMPLATE$2 = "TEMPLATE2";

export { TEMPLATE$1 as TEMPLATE1, TEMPLATE$2 as TEMPLATE2 };

export {};

but I can't tell you for sure.

@timocov timocov self-assigned this Jul 1, 2020
@timocov timocov added this to the 5.0 milestone Jul 1, 2020
@timocov timocov changed the title Variables with same names from different modules exported using the same name. Support for auto renaming nodes with the same name in the bundle Jul 1, 2020
@timocov timocov removed this from the 5.0 milestone Jul 1, 2020
@timocov timocov removed their assignment Jul 1, 2020
@timocov timocov mentioned this issue Apr 9, 2023
@timocov timocov added this to the 9.0 milestone Nov 19, 2023
@timocov timocov self-assigned this Nov 19, 2023
timocov added a commit that referenced this issue Nov 19, 2023
timocov added a commit that referenced this issue Nov 26, 2023
@timocov
Copy link
Owner

timocov commented Nov 27, 2023

The fix has been released in v9.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants