Skip to content

feat: add dynamic_import_vars plugin - #1732

Merged
underfin merged 32 commits into
rolldown:mainfrom
kermanx:feat/plugin-dynamic-import-vars
Aug 1, 2024
Merged

underfin merged 32 commits into
rolldown:mainfrom
kermanx:feat/plugin-dynamic-import-vars

Conversation

@kermanx

@kermanx kermanx commented Jul 25, 2024 •

Copy link
Copy Markdown
Contributor

Listed in #819.

This PR implements Vite's dynamic-import-vars plugin as a built-in Rolldown plugin.

Todos

  1. The tests still fail because import.meta.glob in the source code can be transformed, but those generated by dynamic_import_vars_plugin cannot. I don't know how to address this.

  2. This PR has to copy nodes, but I can't find the right way to do this in Rust (related to CloneIn trait for AST nodes oxc-project/oxc#4284?). Currently, the AST is first generated to JS and then parsed, in order to be cloned.

  3. Other TODOs are rather less important and have comments starting with TODO: in the source code.

Notes

In Vite, the raw import expression is first resolved as a path and then converted to a glob pattern. However, because the raw import expression is in AST form in Rolldown, it is first converted to a glob pattern and then resolved as a path.

@netlify

netlify Bot commented Jul 25, 2024 •

Copy link
Copy Markdown

✅ Deploy Preview for rolldown-rs canceled.

Name Link
🔨 Latest commit 87efa4f
🔍 Latest deploy log https://app.netlify.com/sites/rolldown-rs/deploys/66ab393ab9188a00083a79b2

@hyfdev

hyfdev commented Jul 25, 2024

Copy link
Copy Markdown
Member

Thanks for the PR. We will review this when we plan to implement this feature.

@underfin underfin self-assigned this Jul 25, 2024
@kermanx
kermanx marked this pull request as draft July 25, 2024 16:47
*expr = self.ast_builder.expression_call(
import_expr.span,
self.ast_builder.vec1(
self.ast_builder.argument_expression(clone_expr(self.ast_builder, &import_expr.source)),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the &import_expr.source is not be used again, you could take it out form import_expr to avoid clone.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import_expr is a reference, thus it can't be moved into Argument 🤔

@underfin underfin Aug 1, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it is a &mut import_expr, i will using to move it out.

std::mem::replace(
            &mut import_expr.source,
            self.ast_builder.expression_null_literal(SPAN),
          )

@underfin

Copy link
Copy Markdown
Contributor

Wrong resolved path in dynamic imports

Yeah. It is our internal implementation for import path lookup problem, we using span to store some info with ast, we are try using astId instead of it. We could leave it at now.

For other to do list, we could add some TODO comment at code, it will leave something todo for others. Thank you.

@kermanx

kermanx commented Jul 29, 2024 •

Copy link
Copy Markdown
Contributor Author

The tests still fail - import.meta.glob in the source code can be transformed, but those generated by dynamic_import_vars_plugin cannot. I don't know how to address this.

Other TODOs mentioned above are now in code comments.

@kermanx
kermanx marked this pull request as ready for review July 29, 2024 09:23
@underfin

Copy link
Copy Markdown
Contributor

The tests still fail - import.meta.glob in the source code can be transformed, but those generated by dynamic_import_vars_plugin cannot. I don't know how to address this.

I will see it, thank you.

@IWANABETHATGUY

Copy link
Copy Markdown
Member

@underfin This pull request has been opened for about one week, maybe we could merge it first if it has no big issue? And polish it in the future?

@underfin

Copy link
Copy Markdown
Contributor

I could see it at next, please wait a while.

@underfin
underfin enabled auto-merge (squash) August 1, 2024 07:22
@underfin

underfin commented Aug 1, 2024

Copy link
Copy Markdown
Contributor

Thank your awesome contribute.

@underfin

underfin commented Aug 1, 2024

Copy link
Copy Markdown
Contributor

The tests still fail - import.meta.glob in the source code can be transformed, but those generated by dynamic_import_vars_plugin cannot. I don't know how to address this.

Other TODOs mentioned above are now in code comments.

Here because the global import plugin not handle nested call expression, i using walk_mut::walk_expression(self, expr); to resolve it.

auto-merge was automatically disabled August 1, 2024 07:28

Head branch was pushed to by a user without write access

@underfin
underfin merged commit 40cf58b into rolldown:main Aug 1, 2024
graphite-app Bot pushed a commit that referenced this pull request Oct 15, 2026
<!-- claude-projects-attribution: {"github_login":"Brooooooklyn"} -->
_Requested by **Brooklyn** · [project thread](https://claude.ai/code/project/chan_018s9RxRdtH424j2EhXdzV3N?thread=cmsg_018s9RxRdtH424j2EhXdzV3NQxbeR8Pt7qaqSQwoq9rEFo)_

<!-- routine:prune-tests:fold-rust-table-tests -->

Before: two Rust test modules were hand-unrolled lookup tables: 19 copy-pasted `dynamic_import_to_glob` tests, and three `relocate` tests that differed only in the target.

After: both are table-driven tests. Every row, comment and old test name is kept; the old name is the row label in the assertion message. Test code only: every hunk is inside a `#[cfg(test)] mod tests` block or under `tests/`.

Update after review: the two subset tests this PR first deleted (`source_utils` `test_contains_script_closing_tag_in_tagged_template` and `light_guess` `unknown_extensions`) are restored unchanged in e16c035, because they document that those cases are handled. The PR now only folds tables.

How: each row was defended by mutating product code with the most realistic regression only that row would catch, rebuilding, running, and reverting. Every row is the only guard for at least one mutation, so none was dropped, and evidence comments record that.

### Totals

| | Before | After |
|---|---|---|
| Files touched | – | 2 |
| Cases in the 2 touched test binaries | 67 | 48 |
| Cases deleted outright | – | 0 |
| Cases removed by folding (19 → 2 and 3 → 1, every row kept) | – | 19 |
| Net lines | – | −87 (+93 / −180) |
| Runtime saved (removed cases' summed duration) | – | ~0 s (every removed case reported `<0.000s>`) |

Whole suite at the merge-base could not be collected here (that needs the full workspace plus the NAPI binding). Counted instead: 522 `#[test]` / `#[tokio::test]` functions under `crates/`, plus fixture cases from 2,164 `_config.json` files (67 dot-prefixed, skipped); test262 and Vitest not counted.

### Per file

1. **`crates/rolldown_plugin_vite_dynamic_import_vars/src/dynamic_import_to_glob.rs`** (hand-unrolled lookup table, 19 → 2)
   - All 19 bodies were the same parse → `to_glob_pattern` → assert sequence (a port of `@rollup/plugin-dynamic-import-vars`). They become `converts_dynamic_import_expressions_to_globs` (16 rows) and `rejects_unsupported_dynamic_import_expressions` (3 rows).
   - M10–M13 each failed exactly one original test; after the fold the same mutations fail the folded test, and the message names the row.
2. **`crates/string_wizard/tests/magic_string.rs` › `relocate::refuses_to_move_a_selection_to_inside_itself{,2,3}`** (hand-unrolled lookup table, 3 → 1)
   - One loop over targets `[3, 4, 6]`. Each target alone guards one edge of `to >= start && to <= end` (M7, M8, M9), so all three are kept with an evidence comment. After the fold, M7, M8 and M9 all fail it.

### Kept after defense — do not re-nominate

- Every row of both `dynamic_import_to_glob` tables (M10 → `escapes_square_brackets`, M11 → `data_uri`, M12 → `throws_…non_add_operator`, M13 → `throws_…file_extension`).
- All three `refuses_to_move…` targets (3, 4, 6).
- `source_utils` `test_contains_script_closing_tag_in_tagged_template` and `_multiple_occurrences`, and `light_guess` `unknown_extensions` (kept on review: they document handled cases).

### Case counts

| File | Before | After | Delta | Expected delta |
|---|---|---|---|---|
| `dynamic_import_to_glob.rs` (crate lib binary) | 19 | 2 | −17 | −17 |
| `string_wizard/tests/magic_string.rs` | 48 | 46 | −2 | −2 |
| **Total** | **67** | **48** | **−19** | **−19** |

`rolldown_utils` (68) and `rolldown_ecmascript_utils` (18) lib binaries are back to their merge-base counts and pass. **0 new failures**, no pre-existing failures.

### Mutations

Each was applied alone, rebuilt, run, then reverted (`git status` clean afterwards).

| # | Location | Change | Failed before | Failed after |
|---|---|---|---|---|
| M7 | `crates/string_wizard/src/magic_string/movement.rs:35` | `to >= start` → `to > start` | `refuses…itself`, `zero_width_relocate_to_its_own_position_still_errors` | folded test, `zero_width…` |
| M8 | `movement.rs:35` | `to <= end` → `to < end` | `refuses…itself3`, `zero_width…` | folded test, `zero_width…` |
| M9 | `movement.rs:35` | `to == start \|\| to == end` | `refuses…itself2` | folded test |
| M10 | `dynamic_import_to_glob.rs:42` | stop escaping `[` and `]` | `escapes_square_brackets` | `converts_…` |
| M11 | `dynamic_import_to_glob.rs:9` | drop `data:` from `IGNORED_PROTOCOLS` | `data_uri` | `converts_…` |
| M12 | `dynamic_import_to_glob.rs:107` | accept any binary operator | `throws_…non_add_operator` | `rejects_…` |
| M13 | `dynamic_import_to_glob.rs:32` | drop the extension check | `throws_…file_extension` | `rejects_…` |
| M14 | `dynamic_import_to_glob.rs:89` | `arguments.is_empty()` → `len() < 2` | nothing (pre-existing gap, not touched here) | – |

M14 shows single-argument `.concat()` (e.g. `'./foo/'.concat(bar)`) is untested; this PR does not change that.

### What ran and what did not

- Ran, at origin/main (c9a8cda) and after: `cargo test --locked` for `rolldown_plugin_vite_dynamic_import_vars`, `string_wizard`, `rolldown_utils` and `rolldown_ecmascript_utils` (per-test durations via `--report-time`); `cargo clippy --locked -p <those crates> --all-targets -- --deny warnings`; `cargo fmt --all --check`; `git diff --check`. All clean, including after the restore commit.
- Not run: the full `just test-rust` (bundler fixtures and test262), the Node/Vitest suites (`just test-node`, needs the binding), `just lint-repo` / `just lint-node` (no non-Rust files changed).

### Excluded pull requests

All 162 open PRs on 2026-10-14 were fetched and every file they touch (1,889 files) was skipped; neither file here is touched by any of them.

### Age check (cutoff: 30 days, 2026-09-14)

| Test(s) | First added |
|---|---|
| `dynamic_import_to_glob` tests | 2024-08-01 (`40cf58b9b`, #1732; moved in `9cb5be8a4`, #7068) |
| `refuses_to_move…` tests | 2024-11-04 (`2ace0ebac`, #2572) |

None is in a permission, security or entitlement suite, and neither was added for a bug.

To revert: `git revert e16c035 6a233d9`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01PxVsmvB9xfZKfVnUonY8kf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants