Skip to content

feat(es/modules): autogenerate AMD module names (#4014)#11822

Draft
MukundaKatta wants to merge 2 commits into
swc-project:mainfrom
MukundaKatta:feat/amd-named-modules
Draft

feat(es/modules): autogenerate AMD module names (#4014)#11822
MukundaKatta wants to merge 2 commits into
swc-project:mainfrom
MukundaKatta:feat/amd-named-modules

Conversation

@MukundaKatta

Copy link
Copy Markdown

Summary

Closes #4014. Adds auto_module_id flag and [name]/[path] template placeholders to amd::Config.module_id, mirroring TypeScript --outFile behavior. Today swc emits anonymous define([...], function(...) {...}); this PR lets users opt in to named AMD modules derived from file paths.

What's added

  • crates/swc_ecma_transforms_module/src/amd.rsmodule_id template interpolation, auto_module_id flag, helpers base_name_and_path, interpolate_module_id, derive_auto_module_id.
  • 4 unit tests covering auto-id, template interpolation, custom filenames, and default-resolver fallback.

Behavior

  • module_id: "[name]" — emits define("foo", ...) for foo.ts
  • module_id: "my-lib/[path]" — emits define("my-lib/src/foo", ...) for src/foo.ts
  • auto_module_id: true (without module_id) — derives from file path the way TypeScript does
  • Defaults preserve existing anonymous define behavior. Backwards-compatible.

Test plan

  • cargo test -p swc_ecma_transforms_module: 4 new + 262 amd + 261 umd + 17 path_node + 63 common_js + 262 sys_js — all green
  • cargo fmt --check clean, cargo clippy --all-targets -- -D warnings clean

Adds an `auto_module_id` flag and template placeholders (`[name]`,
`[path]`) to `amd::Config.module_id`, so swc can name AMD `define`
calls from the file path the way TypeScript's `--outFile` does without
requiring a per-file `/// <amd-module name=".../>` directive.

- `module_id` accepts templates with `[name]` (file stem) and `[path]`
  (project-relative path without extension); plain strings still pass
  through verbatim.
- `auto_module_id: true` derives the id from the resolver base when no
  static id or `<amd-module>` directive is present.
- Falls back to the prior anonymous `define([...], fn)` behavior when
  the resolver is the default no-op one.

Closes swc-project#4014.
@CLAassistant

CLAassistant commented Apr 25, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@changeset-bot

changeset-bot Bot commented Apr 25, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 71a2fab

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

github-actions Bot commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

Binary Sizes

File Size
swc.linux-x64-gnu.node 27M (27788232 bytes)

Commit: ca4dd70

@codspeed-hq

codspeed-hq Bot commented Apr 26, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 219 untouched benchmarks
⏩ 31 skipped benchmarks1


Comparing MukundaKatta:feat/amd-named-modules (71a2fab) with main (3ed7243)

Open in CodSpeed

Footnotes

  1. 31 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Autogenerated Named AMD module names

2 participants