Repository navigation
feat(migrate): generate src/lib/* shims lazily — only the ones actually imported - #110
Merged
Merged
Conversation
…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
Contributor
Author
|
Pushed an additional commit with vitest coverage for the lazy pipeline ( 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 ( Coverage:
The |
|
🎉 This PR is included in version 2.6.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
2 of 3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
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
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:
`from "~/lib/{fetch-utils,http-utils,graphql-utils}"`
`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:
Better than silently emitting a broken site.
Test plan
Follow-up not in this PR
Made with Cursor