manta-style icon indicating copy to clipboard operation
manta-style copied to clipboard

[suggestion] build manta-style code into memory

Open tanhauhau opened this issue 7 years ago • 6 comments

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

tanhauhau avatar Aug 12 '18 14:08 tanhauhau

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?

Cryrivers avatar Aug 12 '18 15:08 Cryrivers

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

tanhauhau avatar Aug 13 '18 02:08 tanhauhau

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.

Cryrivers avatar Aug 13 '18 02:08 Cryrivers

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.

Cryrivers avatar Aug 13 '18 02:08 Cryrivers

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

tanhauhau avatar Aug 13 '18 04:08 tanhauhau

Yep. Sad.

Cryrivers avatar Aug 13 '18 11:08 Cryrivers