macrome-js / macrome

The in-tree build system
MIT License
7 stars 1 forks source link

Feature: suffixed generation #36

Open conartist6 opened 2 years ago

conartist6 commented 2 years ago

This would be a default mode that would transform lib/foo.js to lib/foo.gen.js. This feature would prevent you needing to do something like renaming an existing foo.js to foo.src.js and would speed wholesale adoption.

conartist6 commented 2 years ago

I had been thinking I could bake some custom resolver logic (#35) into the code I generate, perhaps mangling import paths, but I'm realizing that such a setup would be too difficult to watch. Thus perhaps there is also no way to do this? I'll have to think about it some more.

conartist6 commented 2 years ago

I could just insert a suffix after an imported name but before its extension. For example:

import 'file';
import 'file.ts';
import 'file.test.ts';

would be transpiled to:

import 'file.gen';
import 'file.gen.ts';
import 'file.gen.test.ts';

This is a pure transform, and preserves the properties of the source. If the source was an esmodule with no resolver the output will still have that property. If the input did require a resolver to work, it still will.

conartist6 commented 2 years ago

Buuut for that to work, every single path has to get a .gen version. That's a lot to ask for, given that it's an extra thing on top of .ts => .js, and then would you also end up with .gen.ts and .gen.js?

It seems like this is a parallel solution to what I've been considering for re-entrant macros. In this version macro evaluation is written to the .gen file. In the re-entrant version the source itself is overwritten, and must be a polyglot which can execute at build time (where the macro reference will trigger build logic) as well as at runtime, where the macro module would export passthroughs.