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 hint to add babel-register prior to using it #25

Closed
wants to merge 1 commit into from
Closed

Add hint to add babel-register prior to using it #25

wants to merge 1 commit into from

Conversation

sarupbanskota
Copy link
Contributor

No description provided.

@eventualbuddha
Copy link
Collaborator

eventualbuddha commented Jun 19, 2017

Thanks for your PR. I rebased it to fix the build error. If the instructions you added work, we should add it to the codemod -h output too. In my experience, globally-installed packages aren't typically requireable though right? I get this when I try:

~ yarn global add babel-register
yarn global v0.24.4
warning No license field
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 📃  Building fresh packages...
warning "[email protected]" has no binaries
warning No license field
✨  Done in 2.76s.
~ touch foo.js
~ codemod -r babel-register foo.js
Error: Cannot find module 'babel-register' from '/Users/donovan'
    at Function.module.exports [as sync] (/Users/donovan/.config/yarn/global/node_modules/resolve/lib/sync.js:40:15)
    at getRequirableModulePath (/Users/donovan/.config/yarn/global/node_modules/babel-codemod/src/Options.js:188:26)
    at Function.parse (/Users/donovan/.config/yarn/global/node_modules/babel-codemod/src/Options.js:141:35)
    at /Users/donovan/.config/yarn/global/node_modules/babel-codemod/src/index.js:60:41
    at Generator.next (<anonymous>)
    at /Users/donovan/.config/yarn/global/node_modules/babel-codemod/src/index.js:7:71
    at __awaiter (/Users/donovan/.config/yarn/global/node_modules/babel-codemod/src/index.js:3:12)
    at run (/Users/donovan/.config/yarn/global/node_modules/babel-codemod/src/index.js:59:12)
    at Object.<anonymous> (/Users/donovan/.config/yarn/global/node_modules/babel-codemod/bin/codemod:5:1)
    at Module._compile (module.js:571:32)

If we did want this to work I suppose we could use something like requireg.

@sarupbanskota
Copy link
Contributor Author

In my experience, globally-installed packages aren't typically requireable though right? I get this when I try:

yeah you're prob right.

Backstory:
I was coding a plugin within the babel repo, and the --require babel-register worked fine. When I recovered from laziness, I moved my plugin to its own repo hoping to release an npm package: https://github.com/sarupbanskota/babel-plugin-add-name-to-plugin. I got an error when I ran codemod --require babel-register .... I hastily assumed installing globally would fix it, so I sent a PR first and forgot about it, but when I did run it locally myself later, I ran into the same error.

However, I think we still need to do something here, because if you pull from my plugin repo, install babel-register(not globally) and try to run e.g codemod --require babel-register --plugin src/ test/unnamed_plugins/babel-plugin-syntax-async-functions.js you'll run into:

~/babel-plugin-add-name-to-plugin/src/index.js:18
export default function ({ types: t }) {
^^^^^^

... which is not what we want. I haven't had a chance to dig in more in detail on how to resolve it, but maybe you'd have tips for me? (and I can update this PR accordingly with the appropriate thing once we have the right way)

thanks btw for the cool project!

@mcMickJuice
Copy link
Contributor

@sarupbanskota if you run your codemod call as an npm script, does it work?

@sarupbanskota
Copy link
Contributor Author

@mcMickJuice nope, same error :(

I did the following:

  1. npm link babel-register (without this it can't find `babel-register)
  2. codemod --require babel-register --plugin src/ test/unnamed_plugins/babel-plugin-syntax-async-functions.js - same error I shared above about not recognising export
  3. plugin a script called codemod within package.json scripts, followed by npm run codemod, same error as on 2

@mcMickJuice
Copy link
Contributor

Do you have a .babelrc that has the appropriate plugins/presets to transpile your code? babel-register looks for this file and appropriate plugins to modify your code.

@sarupbanskota
Copy link
Contributor Author

sarupbanskota commented Jun 20, 2017

@mcMickJuice That was it :-)

I still feel we should make the following clearer. In my case I had to do:

  1. npm install babel-register --save-dev
  2. npm install babel-preset-env --save-dev
  3. npm link babel-register
  4. create a .babel.rc file with the following:
{
  "presets": ["env"]
}

@eventualbuddha
Copy link
Collaborator

It seems like that's too much to expect people to do. Maybe having an option that specifically tries to just make it work would be good? Something like codemod --with-babel-plugin-loader --plugin ./path/to/es2017/plugin.js. You could imagine a similar codemod --with-typescript-plugin-loader ….

@mcMickJuice
Copy link
Contributor

mcMickJuice commented Jun 20, 2017

so eschew babel-register in favor of directly referencing a babel plugin(s)? Would it be reasonable to just look for a .babelrc file and apply those presets/plugins if it exists?

My impression (and how I use babel-register with this library) is I have a separate project where I write my codemods. I then am then modding files that are in an adjacent project. My codemod script is run via npm scripts in this codemod project and all dependencies are installed locally and there are no globals. Is the point of this PR/thread to include documentation around how to use babel-codemod in this way?

@eventualbuddha
Copy link
Collaborator

In my opinion, that's the right way to do this. Your codemods should be in a separate project from the code being modified. This --require babel-register thing is just a way to allow easier development testing of your codemods. If you already have your separate project for codemods, you can make babel-register a dependency and run codemod from that directory referencing the files to rewrite outside the project.

However, a different setup – or a one-off codemod – makes this more annoying. What I was suggesting is having this project understand that you may want to load plugins written with future JS syntax and that it will just figure out how to make that happen, presumably using babel-register and babel-preset-env internally.

@mcMickJuice
Copy link
Contributor

I think that'd work great. So to clarify, in this situation the codemod command would be:

  • invoked globally
  • with a path to the codemod plugin and the path of files or directories to be modded
  • with a switch to run files through babel plugin pipeline (babel-preset-env)

This feature would then be that switch, essentially and it wouldn't be necessary for users to have a project setup with those plugins installed and a .babelrc file defined (which, as you said, can be annoying for one off codemods)

Is that what you're after?

@eventualbuddha
Copy link
Collaborator

Is that what you're after?

Yep! Sounds right to me. I'm not sure what the right way to do that is, but I'd love to hear suggestions.

@mcMickJuice
Copy link
Contributor

Cool. Should we create a feature issue and move the discussion over there?

Sorry to hijack your PR thread @sarupbanskota :smiley

@eventualbuddha
Copy link
Collaborator

Should we create a feature issue and move the discussion over there?

Yep, just created #29. I'll close this since there isn't really a clear way forward.

@sarupbanskota
Copy link
Contributor Author

haha, no worries folks. thanks for taking this forward! i'll try to contribute code towards this if I can manage time this week and if isn't already taken care of!

@sarupbanskota sarupbanskota deleted the patch-1 branch June 21, 2017 00:34
@sarupbanskota sarupbanskota restored the patch-1 branch June 21, 2017 00:35
@eventualbuddha
Copy link
Collaborator

Since #31 was just merged and babel-register is used internally, this issue is now moot. Hopefully you find it easier to develop codemods with auto transpilation of plugins!

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

Successfully merging this pull request may close these issues.

3 participants