swc-project/swc

Autogenerated Named AMD module names

Open

#4,014 opened on Mar 14, 2022

 (9 comments) (0 reactions) (0 assignees)Rust (1,389 forks)batch import
enhancementhelp wanted

Repository metrics

Stars
 (33,411 stars)
PR merge metrics
 (Avg merge 1d 18h) (37 merged PRs in 30d)

Description

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.

Contributor guide