babel-plugin-preval
babel-plugin-preval copied to clipboard
Add support for evaluating basic typescript
What: Allows preval to parse typescript (and potentially any dialect supported by babel with some extra work)
Why: Preval currently chokes on typescript.
This PR serves as a proof-of-concept to show that a TS file can be transformed into plain JS before being handed off the the require-from-string
helper.
This could be generalized with a more extensive set of transformers, or potentially even a babel option for the plugin to allow an arbitrary set of extra plugins.
How: Check for typescript files when parsing a @preval
tagged file, and optionally parse it with the typescript transformer enabled.
Note, this example only handled the file comment approach, since that code already used babel for parsing the input file. Expanding this it to support the other three approaches would require some changes to use babel handlers such as transformFileSync or transformSync
Codecov Report
Merging #82 into master will not change coverage. The diff coverage is
n/a
.
@@ Coverage Diff @@
## master #82 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 4 4
Lines 106 106
Branches 22 22
=========================================
Hits 106 106
Impacted Files | Coverage Δ | |
---|---|---|
src/index.js | 100.00% <ø> (ø) |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact)
,ø = not affected
,? = missing data
Powered by Codecov. Last update 7b0e710...95f47b4. Read the comment docs.
Hi @TikiTDO,
Thanks for this. We used to transform the code (using babel), but it ended up causing issues. I can't remember what they were though. It was years ago. I guess I'm willing to merge this if you're willing to help maintain things if it breaks something 😬
@kentcdodds how about the idea of making this an babel option for this plugin? I hit a few problems with this patch myself (it worked for node 14, but did not work for node 10 due to how modules are handled), so I don't think it would be easy to get it working generically.
However, if we add a way tell babel what options to use when parsing the code then it would be be possible to adapt it to any particular environment by letting the users enable the set of plugins that work for them.
That would be fine with me. Got any ideas?