Skip to content

Commit

Permalink
Merge branch 'main' into feat-configurable-out-dir
Browse files Browse the repository at this point in the history
  • Loading branch information
waigel authored Aug 2, 2024
2 parents e05fe41 + fd4b2d6 commit af4f6b6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pkg-pr-new.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ jobs:
packages/react-server \
packages/react-server-next \
packages/transforms \
packages/vite-plugin-ssr-middleware
packages/vite-plugin-ssr-middleware \
packages/vite-plugin-ssr-css
4 changes: 4 additions & 0 deletions packages/ssr-css/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.0.1 (2024-07-31)

- fix: handle css virtual module via `ssrLoadModule` ([#589](https://github.com/hi-ogawa/vite-plugins/pull/589))

## v0.0.0

- fix: invalidate virtual module on each direct request ([#304](https://github.com/hi-ogawa/vite-plugins/pull/304))
Expand Down
5 changes: 2 additions & 3 deletions packages/ssr-css/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hiogawa/vite-plugin-ssr-css",
"version": "0.0.0",
"version": "0.0.1",
"homepage": "https://github.com/hi-ogawa/vite-plugins/tree/main/packages/ssr-css",
"repository": {
"type": "git",
Expand All @@ -19,8 +19,7 @@
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"prepack": "tsup --clean",
"release": "pnpm publish --no-git-checks --access public"
"prepack": "tsup --clean"
},
"peerDependencies": {
"vite": "*"
Expand Down
11 changes: 9 additions & 2 deletions packages/ssr-css/src/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ import type { ViteDevServer } from "vite";
// style collection
// https://github.com/remix-run/remix/blob/1a8a5216106bd8c3073cc3e5e5399a32c981db74/packages/remix-dev/vite/styles.ts
// https://github.com/vikejs/vike/blob/f9a91f3c47cab9c2871526ef714cc0f87a41fda0/vike/node/runtime/renderPage/getPageAssets/retrieveAssetsDev.ts
// https://github.com/sveltejs/kit/blob/998edb26d431e4ee4d3b4dc792a86960a85a5b45/packages/kit/src/exports/vite/dev/index.js#L186-L219
// https://github.com/withastro/astro/blob/6aaeec5034cabf6a83e1949ec1ca8f50e7978cc1/packages/astro/src/vite-plugin-astro-server/css.ts

export async function collectStyle(server: ViteDevServer, entries: string[]) {
const urls = await collectStyleUrls(server, entries);
const codes = await Promise.all(
urls.map(async (url) => {
const res = await server.transformRequest(url + "?direct");
return [`/* [collectStyle] ${url} */`, res?.code];
const res = url.includes("\0")
? await server.ssrLoadModule(url).then((m) => m["default"])
: await server
.transformRequest(url + "?direct")
.then((result) => result?.code);

return [`/* [collectStyle] ${url} */`, res];
}),
);
return codes.flat().filter(Boolean).join("\n\n");
Expand Down
14 changes: 9 additions & 5 deletions packages/ssr-css/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ export function vitePluginSsrCss(pluginOpts: { entries: string[] }): Plugin {
},
async load(id, _options) {
if (id.startsWith("\0" + VIRTUAL_ENTRY)) {
const url = new URL(id.slice(1), "https://test.local");
let code = await collectStyle(server, pluginOpts.entries);
if (!url.searchParams.has("direct")) {
code = `export default ${JSON.stringify(code)}`;
try {
const url = new URL(id.slice(1), "https://test.local");
let code = await collectStyle(server, pluginOpts.entries);
if (!url.searchParams.has("direct")) {
code = `export default ${JSON.stringify(code)}`;
}
return code;
} catch (e) {
console.error(e);
}
return code;
}
return;
},
Expand Down

0 comments on commit af4f6b6

Please sign in to comment.