babel-plugin-preval icon indicating copy to clipboard operation
babel-plugin-preval copied to clipboard

Add support for evaluating basic typescript

Open TikiTDO opened this issue 4 years ago • 4 comments

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

TikiTDO avatar Sep 28 '20 22:09 TikiTDO

Codecov Report

Merging #82 into master will not change coverage. The diff coverage is n/a.

Impacted file tree graph

@@            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.

codecov[bot] avatar Sep 28 '20 22:09 codecov[bot]

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 avatar Oct 05 '20 22:10 kentcdodds

@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.

TikiTDO avatar Oct 05 '20 22:10 TikiTDO

That would be fine with me. Got any ideas?

kentcdodds avatar Oct 05 '20 22:10 kentcdodds