|
| 1 | +# Refactor: Split plugin.ts (~2520 lines) |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +`packages/plugin-rsc/src/plugin.ts` is too large (~25k tokens) to fit in context. |
| 6 | + |
| 7 | +## Current Structure |
| 8 | + |
| 9 | +### Named functions (already defined but inline) |
| 10 | + |
| 11 | +| Function | Lines | Size | |
| 12 | +| ------------------------------- | --------- | ---------- | |
| 13 | +| `vitePluginUseClient` | 1320-1647 | ~330 lines | |
| 14 | +| `vitePluginRscCss` | 2125-2520 | ~400 lines | |
| 15 | +| `vitePluginUseServer` | 1803-1940 | ~140 lines | |
| 16 | +| `vitePluginDefineEncryptionKey` | 1736-1800 | ~65 lines | |
| 17 | +| `customOptimizerMetadataPlugin` | 1647-1736 | ~90 lines | |
| 18 | +| `globalAsyncLocalStoragePlugin` | 1292-1318 | ~25 lines | |
| 19 | +| Asset utils | 1995-2120 | ~130 lines | |
| 20 | + |
| 21 | +### Inline plugins in `vitePluginRsc` return array (405-1287) |
| 22 | + |
| 23 | +| Plugin name | Lines | Size | |
| 24 | +| ---------------------------------- | --------- | ---------- | |
| 25 | +| config/environments/buildApp | 500-629 | ~130 lines | |
| 26 | +| configureServer/hotUpdate | 645-862 | ~220 lines | |
| 27 | +| react-server-dom-webpack-alias | 864-886 | ~25 lines | |
| 28 | +| load-environment-module | 887-981 | ~100 lines | |
| 29 | +| load-module-dev-proxy + rpc-client | 982-1059 | ~80 lines | |
| 30 | +| assets-manifest | 1060-1173 | ~115 lines | |
| 31 | +| bootstrap-script-content | 1174-1275 | ~100 lines | |
| 32 | + |
| 33 | +## Extraction Plan |
| 34 | + |
| 35 | +### Phase 1: Extract largest named functions (~730 lines) |
| 36 | + |
| 37 | +1. `plugins/use-client.ts` - `vitePluginUseClient` (~330 lines) |
| 38 | +2. `plugins/rsc-css.ts` - `vitePluginRscCss` + `transformRscCssExport` + CSS utils (~400 lines) |
| 39 | + |
| 40 | +### Phase 2: Extract remaining named functions (~320 lines) |
| 41 | + |
| 42 | +3. `plugins/use-server.ts` - `vitePluginUseServer` (~140 lines) |
| 43 | +4. `plugins/assets.ts` - `assetsURL`, `assetsURLOfDeps`, `mergeAssetDeps`, `collectAssetDeps`, `collectAssetDepsInner`, `RuntimeAsset`, `serializeValueWithRuntime` (~130 lines) |
| 44 | +5. `plugins/encryption.ts` - `vitePluginDefineEncryptionKey` (~65 lines) |
| 45 | + |
| 46 | +### Phase 3: Extract inline plugins (~550 lines) |
| 47 | + |
| 48 | +6. `plugins/hmr.ts` - configureServer, configurePreviewServer, hotUpdate (~220 lines) |
| 49 | +7. `plugins/assets-manifest.ts` - virtual:vite-rsc/assets-manifest plugin (~115 lines) |
| 50 | +8. `plugins/load-module.ts` - load-environment-module + dev-proxy + rpc-client (~180 lines) |
| 51 | +9. `plugins/bootstrap.ts` - bootstrap-script-content plugins (~100 lines) |
| 52 | + |
| 53 | +### Phase 4: Consider further splits |
| 54 | + |
| 55 | +10. `plugins/config.ts` - environment config generation (~130 lines) |
| 56 | +11. Move `RscPluginManager` class to `manager.ts` |
| 57 | + |
| 58 | +## Dependencies to Watch |
| 59 | + |
| 60 | +- Most plugins need `manager: RscPluginManager` |
| 61 | +- Some plugins need `rscPluginOptions` |
| 62 | +- `vitePluginUseClient` needs `customOptimizerMetadataPlugin` (extract together or keep dep) |
| 63 | +- Asset utils used by assets-manifest plugin |
| 64 | + |
| 65 | +## Target Structure |
| 66 | + |
| 67 | +``` |
| 68 | +src/ |
| 69 | + plugin.ts (~500 lines - main exports, manager, buildApp orchestration) |
| 70 | + plugins/ |
| 71 | + use-client.ts (~330 lines) |
| 72 | + use-server.ts (~140 lines) |
| 73 | + rsc-css.ts (~400 lines) |
| 74 | + assets.ts (~130 lines) |
| 75 | + assets-manifest.ts(~115 lines) |
| 76 | + hmr.ts (~220 lines) |
| 77 | + load-module.ts (~180 lines) |
| 78 | + bootstrap.ts (~100 lines) |
| 79 | + encryption.ts (~65 lines) |
| 80 | + ... (existing files) |
| 81 | +``` |
| 82 | + |
| 83 | +## Estimated Reduction |
| 84 | + |
| 85 | +- Phase 1: ~730 lines (29%) |
| 86 | +- Phase 2: ~320 lines (13%) |
| 87 | +- Phase 3: ~550 lines (22%) |
| 88 | +- Total: ~1600 lines (64%), leaving ~900 lines in plugin.ts |
0 commit comments