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

addNamed of @babel/helper-module-imports always generate alias #10328

Closed
nianiaJR opened this issue Aug 13, 2019 · 7 comments

Comments

@nianiaJR
Copy link

commented Aug 13, 2019

Bug Report

Additional context/Screenshots
As the doc says: https://babeljs.io/docs/en/next/babel-helper-module-imports.html
image

But when I use addNamed like this, the it always output like this:

import { named as _named } from "source"

I don't know why I can't remove the alias. could you help me? thanks

@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented Aug 13, 2019

Hey @nianiaJR! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

commented Aug 13, 2019

It's probably because we must always use scope.generateUid when injecting a new variable (to avoid possible conflicts), and it sees the named identifier as already used.
Out of curiosity, why do you need exactly { named } and not { named as _named }? I'd consider this just a bug in that exmaple in the documentation.

@nianiaJR

This comment has been minimized.

Copy link
Author

commented Aug 13, 2019

@nicolo-ribaudo Yes, I just also find the name = this._scope.generateUidIdentifier(name); in the source code. As for your question, it's that I am writting a babel plugin to translate code which uses my own lib:

// brefore
import { Button } from 'dwn-ui'
// after
import { Button } from 'dwn-ui/lib/Button'

This plugin can help to cut down the project code package. So if the translated code is { named } only,I won't do the rest code named indentifier translation. The _named alias don't exist in my original code.
Sorry about my poor English, can you get my idea ? Thanks for your fast reply😁.

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

commented Aug 13, 2019

In your case, wouldn't be enough to replace the import source, without replacing the whole import declaration?
Or you can create the AST using @babel/types. addNamed isn't meant as as raw utility to generate an import: it is needed also to handle identifier renames and commonjs requires.

@nianiaJR

This comment has been minimized.

Copy link
Author

commented Aug 13, 2019

@nicolo-ribaudo I get your idea. And "replacing the source" is proper for just one module import like this:

// before
import { Button } from 'dwn-ui' 
// after
import { Button } from 'dwn-ui/lib/Button'

But it may not work like that:

// before
import { Button, Modal } from 'dwn-ui'
// which needed
import { Button } from 'dwn-ui/lib/Button'
import { Modal } from 'dwen-ui/lib/Modal'

Is that right? thanks

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

commented Aug 13, 2019

Then I suggest directly building your imports, which is also way more performant:
https://astexplorer.net/#/gist/10743870fdf24f273010e6a7633ab78b/latest

@nianiaJR

This comment has been minimized.

Copy link
Author

commented Aug 14, 2019

Wow, I couldn't find the path.replaceWithMultiple before. It's so helpful. Thanks for your patience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.