Navigation Menu

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

Add --out-file-extension option to babel-cli #9144

Merged
merged 6 commits into from Jan 10, 2020

Conversation

eps1lon
Copy link
Contributor

@eps1lon eps1lon commented Dec 7, 2018

Q                       A
Fixed Issues? Fixes #6222
Patch: Bug Fix? No
Major: Breaking Change? No
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Webpack 4 automatically prioritizes foo.mjs over foo.js when resolving import foo from './foo'. With this change we can publish a flat package with esmodules and commonjs side by side that allows imports such as import foo from package/bar that webpack can easily tree shake with help of esmodules.

Settled on out-file-extension to be consistent with other flags (out-dir, out-file etc.)

Alternatives for flag name:

  • --output-file-extension
  • --with-file-extension

Should I add a warning if both --keep-file-extension and --use-file-extension are used?

Yeah, it should throw.

It throws now

@ljharb
Copy link
Member

ljharb commented Dec 8, 2018

It might be nicer to allow the config itself specify an output extension - then we could run twice, one with each config, to output for each extension (each of which has a different set of target environments)

@eps1lon
Copy link
Contributor Author

eps1lon commented Dec 8, 2018

@ljharb I think so too so that you could use a single cli command and change the output extension based on some environment variable.

I think this is a better starting point though since --keep-file-extension is also a cli flag.

@karlhorky
Copy link

Small nit in the PR description:

Webpack 4 automatically prioritizes foo.mjs over foo.js when resolving import foo from './bar'

Shouldn't the filenames be bar.mjs and bar.js (because of the path ./bar in the import example)?

@babel-bot
Copy link
Collaborator

babel-bot commented Feb 26, 2019

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/11285/

@nicolo-ribaudo nicolo-ribaudo added PR: New Feature 🚀 A type of pull request used for our changelog categories pkg: cli labels Feb 26, 2019
@nicolo-ribaudo
Copy link
Member

Should I add a warning if both --keep-file-extension and --use-file-extension are used?

Yeah, it should throw.

WDYT about naming it --out-file-extension, for consistency with --out-file and --out-dir?

@eps1lon
Copy link
Contributor Author

eps1lon commented Feb 28, 2019

@nicolo-ribaudo

  • refactored file extension logic (looked very brittle to me, path module already has everything we need: 680cab6
  • renamed use-file-extension -> --out-file-extension: 003d658
  • added error if both flags are used (do test these?): e12d5e5

@eps1lon eps1lon changed the title Add --use-file-extension option to babel-cli Add --out-file-extension option to babel-cli Feb 28, 2019
@nicolo-ribaudo
Copy link
Member

@eps1lon
Copy link
Contributor Author

eps1lon commented Feb 28, 2019

@nicolo-ribaudo Done d5dc562. Initially forgot to add a trailing newline to stderr.txt and options.json. Added them later and squashed but github doesn't pick it up.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that it is working anyway?

@IvanGoncharov
Copy link

IvanGoncharov commented Mar 26, 2019

I would love to see this PR merged since it will allow us to remove ugly shell scripts from our package.json:
https://github.com/graphql/graphql-js/blob/0b5e95556ef8334416a1dcb97084c1cc0a21ff5e/package.json#L41-L42
and make our project more Windows-friendly.

ATM we have a choice of either waiting until this feature gets merged or drop babel-cli and write our build scripts using babel-core.
Can someone from Babel team please clarify the status of this PR?

@eps1lon eps1lon force-pushed the feat/cli/flag-use-extension branch from d5dc562 to 34db0a0 Compare March 26, 2019 17:09
@jaydenseric
Copy link

jaydenseric commented May 28, 2019

Is there something blocking a merge?

Really looking forward to this shipping; so we can output separate .mjs and .cjs files with package scripts like this:

{
  "prepare:mjs": "BABEL_ESM=1 babel src -d . --out-file-extension .mjs",
  "prepare:cjs": "babel src -d . --out-file-extension .cjs"
}

@eps1lon eps1lon force-pushed the feat/cli/flag-use-extension branch from 34db0a0 to dfa4a31 Compare July 24, 2019 19:20
@eps1lon eps1lon force-pushed the feat/cli/flag-use-extension branch from dfa4a31 to 1415c95 Compare August 5, 2019 11:21
Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Docs is needed anyway.

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Apr 17, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: cli PR: New Feature 🚀 A type of pull request used for our changelog categories PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to output mjs extension when -d is specified