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

[transform-modules-umd] Better exported name for main module #5003

Closed
julien-f opened this issue Dec 15, 2016 · 6 comments
Closed

[transform-modules-umd] Better exported name for main module #5003

julien-f opened this issue Dec 15, 2016 · 6 comments
Labels
area: modules i: enhancement outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@julien-f
Copy link

It would be nice if babel-plugin-transform-modules-umd would use the package name as exported name if the current file is main module of the package.

I would allow me to add this transform into all my modules and have them being usable in browsers (via unpkg.com for instance) without special config :)

@xtuc
Copy link
Member

xtuc commented Dec 15, 2016

Could you please provide "Expected Behavior" and "Current Behavior" sections.

@julien-f
Copy link
Author

julien-f commented Dec 15, 2016

Expected Behavior

The index.js module of my foo-bar package should be expose as global.fooBar.

Of course this is just an example because it should follow module resolution algorithm, such as the main entry in package.json

Current Behavior

The index.js module of my foo-bar package is exposed as global.index.

@xtuc
Copy link
Member

xtuc commented Dec 16, 2016

We need a generic approach, which doesn't depend on a given directory structure.

One possible solution could be to expose an option to be able to custom the final module name and use #4630 (.babelrc.js) to resolve the package.json name.

@julien-f
Copy link
Author

Hmm, I'm not sure it would be necessary, my use case is only for the entry point of the package.

@nicolo-ribaudo
Copy link
Member

This can be easily done by using the moduleId option:

const path = require("path");

module.exports = {
  // Your normal config
  presets: [
    ["@babel/preset-env", { modules: "umd" }],
  ],

  overrides: [{
    // Only apply these options to the entry point
    include: path.resolve(__dirname, "src/index.js"),

    moduleId: require("./package.json").name,
    // If you prefer, you can specify the exact global identifier:
    moduleId: "myLibrary",
  }],
};

By doing so, Babel transforms don't have to understand node's resolution algorithm and it doesn't have to read non-babel options from package.json.

@julien-f
Copy link
Author

@nicolo-ribaudo It was a long time ago so I don't remember my exact needs anymore but the whole point was to avoid specific configuration for each package.

Anyway, I don't have this need anymore so I won't reopen.

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 27, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jun 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: modules i: enhancement outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

4 participants