|
1 |
| -export function createStyles(styles) { |
2 |
| - return styles.map((style, i) => { |
3 |
| - if (style.isDev) { |
4 |
| - return ( |
5 |
| - <style key={style.id} type="text/css" data-vite-dev-id={style.id}> |
6 |
| - {style.content} |
7 |
| - </style> |
8 |
| - ); |
9 |
| - } else { |
10 |
| - return ( |
11 |
| - <style key={i} id={style?.id} type="text/css"> |
12 |
| - {style.content} |
13 |
| - </style> |
| 1 | +import { ModuleNode, type ViteDevServer } from "vite"; |
| 2 | + |
| 3 | +function replaceStrings(text: string, record: Record<string, string>): string { |
| 4 | + const escapedKeys = Object.keys(record) |
| 5 | + .sort((a, b) => b.length - a.length) |
| 6 | + .map((key) => key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")); |
| 7 | + |
| 8 | + // Create a single regex with all keys |
| 9 | + const regex = new RegExp(escapedKeys.join("|"), "g"); |
| 10 | + |
| 11 | + return text.replace(regex, (match) => record[match]); |
| 12 | +} |
| 13 | + |
| 14 | +export async function createDevStyles( |
| 15 | + appDir: string, |
| 16 | + vite: ViteDevServer, |
| 17 | + currentViews: string[], |
| 18 | +) { |
| 19 | + const views = [ |
| 20 | + ...currentViews.map((view) => `${appDir}/views/${view}.tsx`), |
| 21 | + `${appDir}/views/RootLayout.tsx`, |
| 22 | + ]; |
| 23 | + |
| 24 | + let modules = new Set<ModuleNode>(); |
| 25 | + for (const view of views) { |
| 26 | + const mod = vite.moduleGraph.getModulesByFile(view); |
| 27 | + if (mod) { |
| 28 | + modules = modules.union(mod); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + const styles = []; |
| 33 | + const cssModules = []; |
| 34 | + const cssModuleContent: Record<string, string> = {}; |
| 35 | + for (const mod of modules as any) { |
| 36 | + if (mod) { |
| 37 | + for (const imported of mod.importedModules) { |
| 38 | + if (imported.file.includes("module.css")) { |
| 39 | + cssModuleContent[imported.file] = |
| 40 | + imported.ssrTransformResult.map.sourcesContent.join(""); |
| 41 | + } |
| 42 | + if (imported.file.includes(".css")) { |
| 43 | + console.log(imported.file); |
| 44 | + cssModules.push(imported.file); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + for (const cssModulePath of cssModules) { |
| 51 | + const transform = await vite.ssrLoadModule(cssModulePath, { |
| 52 | + fixStacktrace: true, |
| 53 | + }); |
| 54 | + |
| 55 | + const isCssModule = cssModulePath.includes("module.css"); |
| 56 | + |
| 57 | + let transformedCssModule = ""; |
| 58 | + |
| 59 | + if (isCssModule) { |
| 60 | + transformedCssModule = replaceStrings( |
| 61 | + cssModuleContent[cssModulePath], |
| 62 | + transform.default, |
14 | 63 | );
|
15 | 64 | }
|
| 65 | + |
| 66 | + styles.push({ |
| 67 | + isDev: true, |
| 68 | + id: cssModulePath, |
| 69 | + content: isCssModule ? transformedCssModule : transform.default, |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + return styles.map((style, i) => { |
| 74 | + return ( |
| 75 | + <style key={i} type="text/css" data-vite-dev-id={style.id}> |
| 76 | + {style.content} |
| 77 | + </style> |
| 78 | + ); |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +export async function createStyles(styles = []) { |
| 83 | + return styles.map((style, i) => { |
| 84 | + return ( |
| 85 | + <style key={i} id={style?.id} type="text/css"> |
| 86 | + {style.content} |
| 87 | + </style> |
| 88 | + ); |
16 | 89 | });
|
17 | 90 | }
|
0 commit comments