-
Notifications
You must be signed in to change notification settings - Fork 18
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
Adds option for running plugin through babel-preset-env pipeline #31
Conversation
@mcMickJuice sorry it took me so long to get to this. I've mostly finished it up, leaving the option name the same as you had it. I added a test to ensure |
No problem and all looks good. Ship it! |
Cool, still some things I need to do first:
|
For typescript, should we have a separate issue to tackle that? along with doc updates and tests? |
Yeah, I’ll make a ticket for TypeScript. |
Updated readme, explaining how to use new switch and when to use |
Currently working on this and my test is failing. Seems like imported modules are not being transpiled... |
Yeah, I didn’t really explain it in my comment, but that’s something that I was reasonably sure wouldn’t work correctly with the way I implemented it. The problem is that the function I pass to babel-register to determine which files should be transpiled only whitelists the path that was A better approach might be to do the |
Yep, removing the ignore function fixes this issue. Are you looking to avoid transpiling node_modules for purposes of speed? If so, babel-register does this by default. |
We could also just accept an ignore regex in the event that someone is using an npm package that is not transpiled. Essentially exposing the same config setting that babel-register accepts |
By default babel-register also only transpiles files in packages that have babel-register as a dependency, right? I think that’s one issue I was trying to avoid eight the approach I took. |
I'm not sure about that. But could we consider this an edge case and not alter babel-register's default behavior? If there are future issues we can have a bug opened and add further extension points (e.g. exposing babel-register config to cli). If you're cool with this, I can remove the whitelisting and push the test I added. |
Sure, let’s start with this and file bugs for other issues. |
What do you think about making this on by default? |
Completely agree. I just updated docs to say this functionality is on by default. I'll move forward with turning this on by default and exposing a switch to use babel-register. Since this project now has a dependency on babel-register, should the switch be |
Not sure why my pushes are failing in the CI env 🤷♂️ |
Updated to use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looking good. I will wait to merge until I get home and can test it out a bit on a development computer (I’m writing this on an iPad). Thanks for helping to get this feature in!
src/index.ts
Outdated
@@ -15,6 +15,7 @@ OPTIONS | |||
-o, --plugin-options PLUGIN=OPTS JSON-encoded OPTS for PLUGIN (allows multiple). | |||
-r, --require PATH Require PATH before transform (allows multiple). | |||
--extensions EXTS Comma-separated extensions to process (default: "${Array.from(DEFAULT_EXTENSIONS).join(',')}"). | |||
--use-local-babel Run plugins through babel plugins/presets specified in local .babelrc file instead of babel-preset-env |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: end this statement in a period.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, should we add a --no-transpile-plugins
option? Using babel-register
does slow things down a bit and it might be nice to allow opting out of that. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this something we can have as a separate, dependent issue on this feature? Just to keep this PR focused?
@@ -0,0 +1,7 @@ | |||
import path from 'path' | |||
|
|||
const i = path.join(__dirname, 'hello.js') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this here?
const i = path.join(__dirname, 'hello.js') | ||
|
||
export default function incrementValue(value) { | ||
return value + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: 2-space indent, use semicolons. (I should set up prettier on this project)
|
||
export default function incrementValue(value) { | ||
return value + 1 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: use trailing newlines
import incrementValue from './increment-value' | ||
|
||
export default function() { | ||
return { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: use 2-space indent
test/CLITest.ts
Outdated
let afile = await createTemporaryFile('a-file.js', '3 + 4;'); | ||
let { status } = await runCodemodCLI([afile, '-p', plugin('increment-export-default'), '--use-local-babel']); | ||
|
||
const syntaxError = 255; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get where you’re coming from with having this as const
, but let’s just use let
.
test/CLITest.ts
Outdated
|
||
it('fails when specifying --use-local-babel as there are no plugins loaded', async function() { | ||
let afile = await createTemporaryFile('a-file.js', '3 + 4;'); | ||
let { status } = await runCodemodCLI([afile, '-p', plugin('increment-export-default'), '--use-local-babel']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also make some sort of assertion about the output. Ideally we could even catch SyntaxError
and provide a better error message telling users they may need to use a custom .babelrc
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's difficult to find where to place the try catch to determine if the SyntaxError is a result of the plugin or the code that we're modding. We could also look at the stack trace to determine where the syntax error occurred?:
/Users/z002s72/dev/git-clones/babel-codemod/test/fixtures/plugin/increment-export-default.js:1
(function (exports, require, module, __filename, __dirname) { export default function() {
^^^^^^
SyntaxError: Unexpected token export
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:599:28)
at loader (/Users/z002s72/dev/git-clones/babel-codemod/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/z002s72/dev/git-clones/babel-codemod/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like wrapping the following in try/catch captures the specific plugin related SyntaxError:
// index.ts, line 76
let plugins = options.getBabelPlugins();
However the error is undefined...im little lose here 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ll take a look when I get home.
src/Options.ts
Outdated
@@ -83,6 +92,16 @@ export default class Options { | |||
} | |||
} | |||
|
|||
loadBabelTranspile() { | |||
let pluginOptions; | |||
if(!this.useLocalBabel) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: space between if
and (
README.md
Outdated
|
||
`babel-codemod` also supports non-standard/future language features that are not currently supported by the latest version of node. It does this by leveraging `babel-preset-env` which loads the [latest babel plugins](https://github.com/babel/babel/tree/master/packages/babel-preset-env#support-all-plugins-in-babel-that-are-considered-latest). This feature is on by default. | ||
|
||
This feature should support most use cases when writing plugins in advanced Javascript syntax. However, if you are writing plugins with syntax that is beyond "latest", or you would like to use your own set of plugins and presets, you can pass in the `--use-local-babel` switch in combination with a local `.babelrc` file that lists the presets/plugins you want applied to your plugin code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: capitalize as “JavaScript”
README.md
Outdated
|
||
### Transpiling using typescript |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: capitalize as “TypeScript”
Adds `--[no-]transpile-plugins` option to control whether babel should be used to transpile plugins before loading them. This will allow plugins to be written using language features unsupported by the current version of nodejs. Typically and by default simply using `babel-preset-env` will transpile the language features that people will want to use. However, if required, support for loading a custom babel config is controlled by `--[no-]find-babel-config`. Thanks to @mcMickJuice for his work on the feature in #31.
Released in v1.3.0 with the final CLI options being |
#29
Points of discussion/questions: