You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In some cases, we will want to selectively evaluate some expressions in one module, but those expressions are depending on other modules. So, we need to either bundle all files into one or evaluate recursively.
Use the following example, How to evaluate the result if it is a constant expression?
Here is the recursive evaluation. This way we can evaluate result if all the paths are evaluated successfully.
To evaluate `result`, evaluate `a1b1` and `a2b2`
-> To evaluate `a1b1`, evaluate `a1` and `b1`
-> Evaluate `a1`
-> Evaluate `b1`
-> To evaluate `a2b2`, evaluate `a2` and `b2`
-> Evaluate `a2`
-> Evaluate `b2`
wyw-in-js also approaches this way, they create a new Entry and process only the imported ones. By the way, they use Babel to first try to do evaluation statically then use node:vm for more eagerly evaluation.
Bundle the whole project so we have one large file. Then we can parse it with SWC and selectively evaluate what we care. Finally, we need to map things back to our original path with SourceMap.
This approach only need to bundle once, but parse one very large file into AST could lead to memory overflow. And our process could become very slow.
Approach 5 - ...
The text was updated successfully, but these errors were encountered:
Cross-Module Selective Evaluator
In some cases, we will want to selectively evaluate some expressions in one module, but those expressions are depending on other modules. So, we need to either bundle all files into one or evaluate recursively.
Use the following example, How to evaluate the result if it is a constant expression?
Approach 1 - Bundler + Compressor
Use bundler like
Rollup
and set the entry asmain.js
, we can get:Then, use compressor like
Terser
we can get:With bundler and compressor, we can evaluate the result to
"a1b1a2b2"
. So, to achieve selective evaluation:Approach 2 - Bundler + SWC's Evaluator
SWC actually implemented its own minifiler with the help of terser's maintainer (swc-project/swc#1302). And there is a
Evaluator
we can leverage on.Evaluator::eval
Approach 3 - SWC's Evaluator + Recursive eval()
Paste our original example here again for easier reference.
Here is the recursive evaluation. This way we can evaluate result if all the paths are evaluated successfully.
wyw-in-js
also approaches this way, they create a newEntry
and process only the imported ones. By the way, they useBabel
to first try to do evaluation statically then usenode:vm
for more eagerly evaluation.Approach 4 - Bundler + SourceMap + SWC's Evaluator
Bundle the whole project so we have one large file. Then we can parse it with SWC and selectively evaluate what we care. Finally, we need to map things back to our original path with SourceMap.
This approach only need to bundle once, but parse one very large file into AST could lead to memory overflow. And our process could become very slow.
Approach 5 - ...
The text was updated successfully, but these errors were encountered: