Skip to content

feat(migrate): generate src/lib/* shims lazily — only the ones actually imported - #110

Merged
vibe-dex merged 2 commits into
mainfrom
feat/migrate-lazy-shim-generation
May 1, 2026
Merged

vibe-dex merged 2 commits into
mainfrom
feat/migrate-lazy-shim-generation

Conversation

@vibe-dex

@vibe-dex vibe-dex commented May 1, 2026

Copy link
Copy Markdown
Contributor

Summary

The migration script used to write all 11 `src/lib/*.ts` shim files eagerly during `phase-scaffold`, regardless of whether the migrated codebase actually imported any of them. In practice, most sites end up importing zero of these shims — apps-start exports direct equivalents for nearly every VTEX utility (verified against baggagio-tanstack: 0 shims used out of 11 generated, ~235 LOC of dead code).

This PR makes generation lazy: only the shims that the migrated code actually imports get written.

Changes

File Change
`templates/lib-utils.ts` `generateLibUtils(ctx)` (eager) → `LIB_TEMPLATES` registry + `selectImportedLibTemplates(specifiers)` filter helper
`phase-scaffold.ts` No longer calls `generateLibUtils`. Comment points at where lazy pass runs
`phase-cleanup.ts` New final step `writeImportedLibShims(ctx)` — scans final `src/**` for `from "~/lib/"` imports, writes only matching templates

Why end-of-cleanup, not end-of-transform

`phase-cleanup` itself adds `from "~/lib/vtex-segment"` etc. via inline-stub hoisting. Running the lazy writer at the very end of cleanup captures those late additions.

Concrete impact

Site Before this PR After this PR
Clean migration (no `apps/utils/*` imports) 11 shim files written, all dead No `src/lib/` directory at all
Site with a few `apps/utils/*` imports 11 shim files written, ~3 used Only the 3 imported shims written
baggagio-tanstack (re-migrated under this PR) Would generate 11; #7 deletes them all Generates 0

Verification

```
$ npm run build
exit 0

$ npx vitest run
Test Files 9 passed (9)
Tests 104 passed (104)
```

Manual trace confirms the regex catches both:

  • First-pass rewrites in `transforms/imports.ts:92-94` that produce
    `from "~/lib/{fetch-utils,http-utils,graphql-utils}"`
  • Inline-stub hoisting in `phase-cleanup` that produces
    `from "~/lib/{vtex-segment,fetch-utils,vtex-intelligent-search}"`

Defensive: warns when an import has no registered template

If a site (or some future cleanup pass) produces a `from "~/lib/"` import, the lazy writer logs:

`Warning: ~/lib/{unknown} imported but no template registered. Write the file by hand or add a template to scripts/migrate/templates/lib-utils.ts.`

Better than silently emitting a broken site.

Test plan

  • CI passes
  • Run migration on a copy of `casaevideo` (or any deco-cx site) and confirm `src/lib/` either doesn't exist or contains only files corresponding to actual imports
  • If `src/lib/` is empty, confirm the migration's verify phase still passes

Follow-up not in this PR

  • Add vitest config for `scripts/` so we can write regression tests for this scan-and-write pipeline (instead of relying on manual smoke runs).

Made with Cursor

vibe-dex added 2 commits May 1, 2026 10:11
…ly imported

The migration script used to write all 11 `src/lib/*.ts` shim files
eagerly during `phase-scaffold`, regardless of whether the migrated
codebase imported any of them. In practice, most sites end up importing
zero shims — apps-start exports direct equivalents for nearly every
VTEX utility (verified against baggagio in #7: 0 shims used out of 11
generated, ~235 LOC of dead code).

This PR makes generation lazy:

- `templates/lib-utils.ts`: Replaces the `generateLibUtils(ctx)` function
  (eager, returns all files) with a `LIB_TEMPLATES` registry
  (`{ "src/lib/<name>.ts": "...content..." }`) and a
  `selectImportedLibTemplates(specifiers)` filter helper.
- `phase-scaffold.ts`: No longer calls `generateLibUtils`. Comment
  explains the lazy strategy and points at where the new pass runs.
- `phase-cleanup.ts`: New final step `writeImportedLibShims(ctx)` that
  walks `src/**/*.{ts,tsx}` after every other transform settles, scans
  for `from "~/lib/<name>"` imports, and writes only the corresponding
  templates. Logs a warning if a `~/lib/X` import has no registered
  template.

Why end-of-cleanup, not end-of-transform: phase-cleanup itself adds
`from "~/lib/vtex-segment"` etc. via inline-stub hoisting (line ~1092
of the old file). Running the lazy writer at the very end of cleanup
captures those late additions.

Concrete impact:
- Sites with zero `~/lib/*` imports (all clean migrations going
  forward) will have NO `src/lib/` directory at all.
- Sites with a few imports get just those shims — not all 11.
- baggagio-tanstack#7 type cleanups become unnecessary on future
  migrations.

Verification:
- `npm run build` exits 0
- vitest: 104/104 pass
- Manual trace: import rewrite rules `apps/utils/{fetch,http,graphql}` →
  `~/lib/{fetch-utils,http-utils,graphql-utils}` are all matched by
  the new scan regex `/from\s+["']~\/lib\/([^"']+?)(?:\.ts)?["']/g`
- Inline-stub hoisting paths (`from "~/lib/vtex-segment"`,
  `from "~/lib/vtex-intelligent-search"`, `from "~/lib/fetch-utils"`)
  also matched.

Follow-up not in this PR:
- Add vitest config for `scripts/` so we can write regression tests for
  this scan-and-write pipeline (instead of relying on manual smoke runs).

Made-with: Cursor
Adds vitest coverage for the lazy shim pipeline introduced in this PR.
The first round of code review caught zero bugs because there were
zero tests; the new integration test caught one immediately (see
"dry-run mode" test below).

Changes:

- `vitest.config.ts`: includes `scripts/**/*.test.ts` and uses
  `environmentMatchGlobs` so script tests run in node env
  (filesystem) while src tests stay in jsdom (React).

- `scripts/migrate/templates/lib-utils.test.ts` (10 unit tests):
  - `LIB_TEMPLATES` registry shape (paths, non-empty contents,
    uniqueness).
  - `selectImportedLibTemplates`: empty/single/multiple/unknown
    specifier handling, no mutation, full coverage of every
    well-known migration target the writer might emit.

- `scripts/migrate/phase-cleanup.test.ts` (7 integration tests against
  a real tmp directory):
  - Writes nothing when no `~/lib` imports are present.
  - Writes only the matching shim files for found imports.
  - Dry-run mode writes nothing (caught a real bug — the original
    impl was creating an empty `src/lib/` dir before the dry-run
    skip; fixed in this commit).
  - Strips trailing `.ts` from import specifiers.
  - Ignores imports inside the lib dir itself (no self-amplification).
  - Scans `.tsx` files too.
  - No-op when `src/` does not exist.

- `phase-cleanup.ts`:
  - Bug fix surfaced by the dry-run test: gate `mkdirSync` on
    `!ctx.dryRun` so a stale empty directory isn't left on disk.
  - Export `writeImportedLibShims` for unit testability.

Verification:
- `npx vitest run scripts/` → 17 tests, all pass.
- `npx vitest run` → 121 tests, all pass (was 104, now 17 more).
- `npm run build` → exit 0.

Total cumulative impact of #110:
- +297 / -22 (was +155 / -22 before tests added)
- One pre-existing bug found and fixed thanks to writing the test.

Made-with: Cursor
@vibe-dex

vibe-dex commented May 1, 2026

Copy link
Copy Markdown
Contributor Author

Pushed an additional commit with vitest coverage for the lazy pipeline (scripts/migrate/templates/lib-utils.test.ts + scripts/migrate/phase-cleanup.test.ts).

Why a follow-up commit on the same branch instead of a separate PR: tests ship with the feature, and writing them caught one real bug (mkdirSync ran before the dryRun skip, leaving an empty src/lib/ on disk in dry-run mode — fixed in this commit).

$ npx vitest run scripts/
 Test Files  2 passed (2)
      Tests  17 passed (17)

$ npx vitest run
 Test Files  11 passed (11)
      Tests  121 passed (121)

Coverage:

  • LIB_TEMPLATES: registry shape (paths, non-empty contents, uniqueness)
  • selectImportedLibTemplates: empty / single / multiple / unknown specifiers, immutability, full coverage of every well-known migration target
  • writeImportedLibShims: 7 integration tests against a real tmpdir — no-op when no imports, writes-only-matching, dry-run safety, .ts-extension stripping, lib/ self-skip, .tsx scanning, missing-src no-op

The vitest.config.ts change adds environmentMatchGlobs so script tests run in the node env (filesystem) while src tests stay in jsdom (React). One config, two suites.

@vibe-dex
vibe-dex merged commit fcbbe0c into main May 1, 2026
1 check passed
@github-actions

github-actions Bot commented May 1, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 2.6.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant