UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

1 lines • 8.2 kB
{"version":3,"file":"SSRCSSPlugin.js","names":[],"sources":["esm/vite/plugins/SSRCSSPlugin.js"],"sourcesContent":["import { VIRTUAL_SSR_CSS_ENTRY, VIRTUAL_SSR_CSS_HREF } from \"../../constants\";\nfunction SSRCSSPlugin(pluginOpts) {\n let server;\n return {\n name: `one-plugin-ssr-css`,\n apply: \"serve\",\n configureServer(server_) {\n server = server_;\n server.middlewares.use(async (req, res, next) => {\n if (req.url?.includes(VIRTUAL_SSR_CSS_HREF)) {\n invalidateModule(server, \"\\0\" + VIRTUAL_SSR_CSS_ENTRY + \"?direct\");\n const code = await collectStyle(server, pluginOpts.entries);\n res.setHeader(\"Content-Type\", \"text/css\");\n res.setHeader(\"Cache-Control\", \"no-store\");\n res.setHeader(\"Vary\", \"*\");\n res.write(code);\n res.end();\n return;\n }\n next();\n });\n },\n // virtual module\n // (use `startsWith` since Vite adds `?direct` for raw css request)\n resolveId(source, _importer, _options) {\n return source.startsWith(VIRTUAL_SSR_CSS_ENTRY) ? \"\\0\" + source : void 0;\n },\n async load(id, _options) {\n if (id.startsWith(\"\\0\" + VIRTUAL_SSR_CSS_ENTRY)) {\n const url = new URL(id.slice(1), \"https://test.local\");\n let code = await collectStyle(server, pluginOpts.entries);\n if (!url.searchParams.has(\"direct\")) {\n code = `export default ${JSON.stringify(code)}`;\n }\n return code;\n }\n return;\n },\n // also expose via transformIndexHtml\n transformIndexHtml: {\n handler: async () => {\n return [\n {\n tag: \"link\",\n // prepend so SSR CSS comes BEFORE individual style tags injected by JS\n // this ensures individual styles (later in DOM) win in CSS cascade\n injectTo: \"head-prepend\",\n attrs: {\n rel: \"stylesheet\",\n href: VIRTUAL_SSR_CSS_HREF,\n \"data-ssr-css\": true\n }\n },\n {\n tag: \"script\",\n injectTo: \"head\",\n attrs: { type: \"module\" },\n children: (\n /* js */\n `\n import { createHotContext } from \"/@vite/client\";\n const hot = createHotContext(\"/__clear_ssr_css\");\n hot.on(\"vite:beforeUpdate\", () => {\n document\n .querySelectorAll(\"[data-ssr-css]\")\n .forEach(node => { node.disabled = true; });\n });\n `\n )\n }\n ];\n }\n }\n };\n}\nfunction invalidateModule(server, id) {\n const mod = server.moduleGraph.getModuleById(id);\n if (mod) {\n server.moduleGraph.invalidateModule(mod);\n }\n}\nasync function collectStyle(server, entries) {\n const { transform } = await import(\"lightningcss\");\n const urls = await collectStyleUrls(server, entries);\n const codes = await Promise.all(\n urls.map(async (url) => {\n const res = await server.transformRequest(url + \"?direct\");\n const code = res?.code || \"\";\n const prefix = `/* [collectStyle] ${url} */`;\n try {\n const buffer = Buffer.from(code);\n const codeOut = new Uint8Array(\n buffer.buffer,\n buffer.byteOffset,\n buffer.byteLength\n );\n const processed = transform({\n filename: \"code.css\",\n code: codeOut,\n ...server.config.css.lightningcss\n }).code.toString();\n return [prefix, processed];\n } catch (err) {\n console.error(` [one] Error post-processing CSS, leaving un-processed: ${err}`);\n return [prefix, code];\n }\n })\n );\n let out = codes.flat().filter(Boolean).join(\"\\n\\n\");\n try {\n const buffer = Buffer.from(out);\n const codeOut = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);\n out = transform({\n filename: \"code.css\",\n code: codeOut,\n ...server.config.css.lightningcss\n }).code.toString();\n } catch (err) {\n console.error(` [one] Error post-processing merged CSS, leaving un-processed`);\n }\n return out;\n}\nasync function collectStyleUrls(server, entries) {\n const visited = /* @__PURE__ */ new Set();\n function walk(root) {\n const stack = [root];\n while (stack.length) {\n const mod = stack.pop();\n if (!mod.id || mod.url.includes(\".server.\") || visited.has(mod.id)) continue;\n visited.add(mod.id);\n for (const childMod of mod.importedModules) stack.push(childMod);\n }\n }\n async function traverse(url) {\n if (url.includes(\".server.\")) return;\n const [, id] = await server.moduleGraph.resolveUrl(url);\n if (visited.has(id)) {\n return;\n }\n visited.add(id);\n const mod = server.moduleGraph.getModuleById(id);\n if (!mod) {\n return;\n }\n for (const childMod of mod.importedModules) walk(childMod);\n }\n await Promise.all(entries.map((e) => server.transformRequest(e)));\n await Promise.all(entries.map((url) => traverse(url)));\n return [...visited].filter((url) => url.match(CSS_LANGS_RE));\n}\nconst CSS_LANGS_RE = /\\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\\?)/;\nexport {\n SSRCSSPlugin,\n collectStyle\n};\n//# sourceMappingURL=SSRCSSPlugin.js.map\n"],"mappings":";;;AACA,SAAS,aAAa,YAAY;CAChC,IAAI;CACJ,OAAO;EACL,MAAM;EACN,OAAO;EACP,gBAAgB,SAAS;GACvB,SAAS;GACT,OAAO,YAAY,IAAI,OAAO,KAAK,KAAK,SAAS;IAC/C,IAAI,IAAI,KAAK,SAAS,oBAAoB,GAAG;KAC3C,iBAAiB,QAAQ,OAAO,wBAAwB,SAAS;KACjE,MAAM,OAAO,MAAM,aAAa,QAAQ,WAAW,OAAO;KAC1D,IAAI,UAAU,gBAAgB,UAAU;KACxC,IAAI,UAAU,iBAAiB,UAAU;KACzC,IAAI,UAAU,QAAQ,GAAG;KACzB,IAAI,MAAM,IAAI;KACd,IAAI,IAAI;KACR;IACF;IACA,KAAK;GACP,CAAC;EACH;EAGA,UAAU,QAAQ,WAAW,UAAU;GACrC,OAAO,OAAO,WAAW,qBAAqB,IAAI,OAAO,SAAS,KAAK;EACzE;EACA,MAAM,KAAK,IAAI,UAAU;GACvB,IAAI,GAAG,WAAW,OAAO,qBAAqB,GAAG;IAC/C,MAAM,MAAM,IAAI,IAAI,GAAG,MAAM,CAAC,GAAG,oBAAoB;IACrD,IAAI,OAAO,MAAM,aAAa,QAAQ,WAAW,OAAO;IACxD,IAAI,CAAC,IAAI,aAAa,IAAI,QAAQ,GAAG;KACnC,OAAO,kBAAkB,KAAK,UAAU,IAAI;IAC9C;IACA,OAAO;GACT;GACA;EACF;EAEA,oBAAoB,EAClB,SAAS,YAAY;GACnB,OAAO,CACL;IACE,KAAK;IAGL,UAAU;IACV,OAAO;KACL,KAAK;KACL,MAA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