swc icon indicating copy to clipboard operation
swc copied to clipboard

Autogenerated Named AMD module names

Open binoche9 opened this issue 3 years ago • 7 comments

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.

binoche9 avatar Mar 14 '22 22:03 binoche9

@kdy1 friendly bump in case you missed this!

binoche9 avatar Mar 23 '22 15:03 binoche9

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?

kwonoj avatar Mar 23 '22 16:03 kwonoj

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

binoche9 avatar Mar 23 '22 16:03 binoche9

Any thoughts on this? At the least would you accept a PR if I'm able to open one?

binoche9 avatar Apr 23 '22 16:04 binoche9

@binoche9 Yes, of course! Thank you!

kdy1 avatar Apr 23 '22 16:04 kdy1

Great! Since I'm new to Rust, mind giving me some pointers? Specifically if you can point to the lines of code where:

  1. AMD transpilation starts happening
  2. Where swc reads moduleId from the .swcrc
  3. 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!

binoche9 avatar Apr 24 '22 01:04 binoche9

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

magic-akari avatar Jul 02 '22 16:07 magic-akari

Core team may accept PR but won't have immediate time to work on this.

kwonoj avatar Jul 24 '23 18:07 kwonoj

sounds good - fwiw this is still on my mind to open a PR for even if it's been a while...

binoche9 avatar Jul 24 '23 20:07 binoche9