Cryrivers / manta-style

🚀 The futuristic API Mock Server for Frontend

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[suggestion] build manta-style code into memory

tanhauhau opened this issue · comments

Can we build manta-style code into memory and eval it in memory instead of build code into .mantastyle-tmp?

this was my first thought. but later i found somehow there might be external references in the config file. for example, you might need to import some modules from your projects. then it would be tricky to build it into memory, especially when your files indirectly references something in node_modules.

maybe we could move the temporary folder to /tmp?

hmm. what kind of external references? i guess i need to read/use more manta-style to discover these kinds of "holes".

external references as in you import some types from your projects. this is our common scenario as most of our types are in our project.

so TypeScript actually supports in-memory compilation. It provides transpileModule method to have string -> string compilation. However it doesn't support import iirc, you need to resolve import by yourself, that's why we are using the current way.

Another "hole" is TypeScript doesn't rename (or maybe i don't know how) variables in the expressions created by the transformer. Say you create such statements:

import MantaStyle from '@manta-style/runtime';
// ...
MantaStyle.doSomething(...);

If our module target is commonjs, then it would be compiled as something like following:

var _a = require('@manta-style/runtime').default;
// ...
MantaStyle.doSomething(...);

MantaStyle here is supposed to be _a but TypeScript won't rename that.

So to solve this problem, i compile config files to ES Module version, then further to commonjs version with Babel.

Probably they are not the same identifier node when you create the statement in the ast.

Yep. Sad.