Autogenerated Named AMD module names
Describe the feature
Problem
Currently if I want to emit an AMD module, the emitted AMD module will be anonymous unless I set the "moduleId" config option in the .swcrc which will emit a named AMD module with the moduleId as the name.
The problem with this is that this means I need to generate a different .swcrc for each named module which feels very unnecessary.
Proposal
I'd like to propose making it possible to autogenerate the AMD module name based on a moduleRoot (or maybe moduleNameRoot) config option, and generate the AMD module name from the path of the current file relative to the root.
Some example moduleRoot values when transpiling /foo/bar/baz.ts:
"moduleRoot": "/"->define("foo/bar/baz", ...)"moduleRoot": "/foo"->define("bar/baz", ...)
And obviously I'd want the source maps to work properly too.
If this is as straightforward as I think it is then I'd be willing to learn some Rust and try to implement this with some guidance.
Babel plugin or link to the feature description
No response
Additional context
The TypeScript compiler does something very similar when compiling to AMD with --outFile.
@kdy1 friendly bump in case you missed this!
We do read issues, it may takes time to respond or we may do internal discussions.
This seems overlap slightly between https://github.com/swc-project/swc/issues/4122 while technically not exactly same. Do you suggest to have both, or it'll be feasible if #4122 is available?
Sorry about the impatience (it's been over a week and I saw activity on the other issues) - I appreciate the response!
I suggest to have both (obviously I'm more interested in this issue) since I'd prefer to not have to add the /// <amd-module/> to all my TypeScript files (and not have to adjust them if files get moved around) and just drop in swc as a replacement for tsc --outFile.
If this issue is as simple as a little bit of string manipulation with moduleRoot and the current file path, I'm hoping it's easy enough change even for someone like me who would be new to Rust
Any thoughts on this? At the least would you accept a PR if I'm able to open one?
@binoche9 Yes, of course! Thank you!
Great! Since I'm new to Rust, mind giving me some pointers? Specifically if you can point to the lines of code where:
- AMD transpilation starts happening
- Where swc reads
moduleIdfrom the.swcrc - The function call where source map generation happens
that should save me a lot of time to get started and hopefully I can engineer the rest from there. Thanks!
UMD generate exported_name from the path which is similar to what you need.
https://github.com/swc-project/swc/blob/9addef6fc8672854b52a1b71668795474a6f3fec/crates/swc_ecma_transforms_module/src/umd.rs#L93-L94
Once you have generated the name, you can simply call amd function with it.
amd(
unresolved_mark,
config: amd::Config {
module_id:: Some(module_id_from_path),
config,
},
Default::default()
)
Also, I think this task can be done in a custom plugin
- check this #4867
Core team may accept PR but won't have immediate time to work on this.
sounds good - fwiw this is still on my mind to open a PR for even if it's been a while...