|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const path = require('node:path'); |
| 4 | + |
| 5 | +async function findDeps(vite, node, deps) { |
| 6 | + // since `ssrTransformResult.deps` contains URLs instead of `ModuleNode`s, this process is asynchronous. |
| 7 | + // instead of using `await`, we resolve all branches in parallel. |
| 8 | + const branches = []; |
| 9 | + |
| 10 | + async function add(node) { |
| 11 | + if (!deps.has(node)) { |
| 12 | + deps.add(node); |
| 13 | + await findDeps(vite, node, deps); |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + async function add_by_url(url) { |
| 18 | + const node = await vite.moduleGraph.getModuleByUrl(url); |
| 19 | + |
| 20 | + if (node) { |
| 21 | + await add(node); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + if (node.ssrTransformResult) { |
| 26 | + if (node.ssrTransformResult.deps) { |
| 27 | + node.ssrTransformResult.deps.forEach(url => |
| 28 | + branches.push(add_by_url(url)) |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + // if (node.ssrTransformResult.dynamicDeps) { |
| 33 | + // node.ssrTransformResult.dynamicDeps.forEach(url => branches.push(add_by_url(url))); |
| 34 | + // } |
| 35 | + } else { |
| 36 | + node.importedModules.forEach(node => branches.push(add(node))); |
| 37 | + } |
| 38 | + |
| 39 | + await Promise.all(branches); |
| 40 | +} |
| 41 | + |
| 42 | +// Vite doesn't expose this so we just copy the list for now |
| 43 | +// https://github.com/vitejs/vite/blob/3edd1af56e980aef56641a5a51cf2932bb580d41/packages/vite/src/node/plugins/css.ts#L96 |
| 44 | +const STYLE_ASSET_REGEX = /\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/; |
| 45 | +const MODULE_STYLE_ASSET_REGEX = |
| 46 | + /\.module\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/; |
| 47 | + |
| 48 | +async function collectStyles(devServer, match) { |
| 49 | + const styles = {}; |
| 50 | + const deps = new Set(); |
| 51 | + try { |
| 52 | + for (const file of match) { |
| 53 | + const resolvedId = await devServer.pluginContainer.resolveId(file); |
| 54 | + |
| 55 | + if (!resolvedId) { |
| 56 | + console.log('not found'); |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + const id = resolvedId.id; |
| 61 | + |
| 62 | + const normalizedPath = path.resolve(id).replace(/\\/g, '/'); |
| 63 | + let node = devServer.moduleGraph.getModuleById(normalizedPath); |
| 64 | + if (!node) { |
| 65 | + const absolutePath = path.resolve(file); |
| 66 | + await devServer.ssrLoadModule(absolutePath); |
| 67 | + node = await devServer.moduleGraph.getModuleByUrl(absolutePath); |
| 68 | + |
| 69 | + if (!node) { |
| 70 | + console.log('not found'); |
| 71 | + return; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + await findDeps(devServer, node, deps); |
| 76 | + } |
| 77 | + } catch (e) { |
| 78 | + console.error(e); |
| 79 | + } |
| 80 | + |
| 81 | + for (const dep of deps) { |
| 82 | + const parsed = new URL(dep.url, 'http://localhost/'); |
| 83 | + const query = parsed.searchParams; |
| 84 | + |
| 85 | + if (STYLE_ASSET_REGEX.test(dep.file ?? '')) { |
| 86 | + try { |
| 87 | + const mod = await devServer.ssrLoadModule(dep.url); |
| 88 | + // if (module_STYLE_ASSET_REGEX.test(dep.file)) { |
| 89 | + // styles[dep.url] = env.cssModules?.[dep.file]; |
| 90 | + // } else { |
| 91 | + styles[dep.url] = mod.default; |
| 92 | + // } |
| 93 | + } catch { |
| 94 | + // this can happen with dynamically imported modules, I think |
| 95 | + // because the Vite module graph doesn't distinguish between |
| 96 | + // static and dynamic imports? TODO investigate, submit fix |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + return styles; |
| 101 | +} |
| 102 | + |
| 103 | +module.exports = {collectStyles}; |
0 commit comments