fix(vite): serve css imported in islands (#3272)
Fixes https://github.com/denoland/fresh/issues/3271
This commit is contained in:
@@ -102,7 +102,8 @@
|
||||
"base",
|
||||
"noscript",
|
||||
"template"
|
||||
]
|
||||
],
|
||||
"types": ["./global.d.ts"]
|
||||
},
|
||||
"lint": {
|
||||
"rules": {
|
||||
|
||||
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
declare module "*.module.css" {
|
||||
const CssMod: Record<string, string>;
|
||||
export default CssMod;
|
||||
}
|
||||
@@ -97,6 +97,7 @@ export class IslandPreparer {
|
||||
mod: Record<string, unknown>,
|
||||
chunkName: string,
|
||||
modName: string,
|
||||
css: string[],
|
||||
) {
|
||||
for (const [name, value] of Object.entries(mod)) {
|
||||
if (typeof value !== "function") continue;
|
||||
@@ -110,6 +111,7 @@ export class IslandPreparer {
|
||||
file: chunkName,
|
||||
fn,
|
||||
name: uniqueName,
|
||||
css,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ export interface Island {
|
||||
name: string;
|
||||
exportName: string;
|
||||
fn: ComponentType;
|
||||
css: string[];
|
||||
}
|
||||
|
||||
export type ServerIslandRegistry = Map<ComponentType, Island>;
|
||||
|
||||
@@ -314,6 +314,7 @@ export class Builder<State = any> {
|
||||
name,
|
||||
server: spec,
|
||||
browser: null,
|
||||
css: [],
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ export interface IslandModChunk {
|
||||
name: string;
|
||||
server: string;
|
||||
browser: string | null;
|
||||
css: string[];
|
||||
}
|
||||
|
||||
export type FsRouteFileNoMod<State> = Omit<FsRouteFile<State>, "mod"> & {
|
||||
@@ -206,7 +207,7 @@ export class MemoryBuildCache<State> implements DevBuildCache<State> {
|
||||
throw new Error(`Unexpected missing browser chunk`);
|
||||
}
|
||||
|
||||
preparer.prepare(this.islandRegistry, mod, chunk.browser, name);
|
||||
preparer.prepare(this.islandRegistry, mod, chunk.browser, name, []);
|
||||
},
|
||||
),
|
||||
);
|
||||
@@ -461,7 +462,8 @@ export async function generateSnapshotServer(
|
||||
const islandMarkers = islands.map((item) => {
|
||||
const browser = JSON.stringify(item.browser);
|
||||
const name = JSON.stringify(item.name);
|
||||
return `islandPreparer.prepare(islands, ${item.name}, ${browser}, ${name});`;
|
||||
const css = JSON.stringify(item.css);
|
||||
return `islandPreparer.prepare(islands, ${item.name}, ${browser}, ${name}, ${css});`;
|
||||
}).join("\n");
|
||||
|
||||
const serializedFsRoutes = fsRoutesFiles
|
||||
|
||||
@@ -254,8 +254,9 @@ options[OptionsType.DIFF] = (vnode) => {
|
||||
RENDER_STATE!.renderedHtmlHead = true;
|
||||
|
||||
const entryAssets = RENDER_STATE.buildCache.getEntryAssets();
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const items: VNode<any>[] = [];
|
||||
if (entryAssets.length > 0) {
|
||||
const items: VNode[] = [];
|
||||
for (let i = 0; i < entryAssets.length; i++) {
|
||||
const id = entryAssets[i];
|
||||
|
||||
@@ -266,32 +267,22 @@ options[OptionsType.DIFF] = (vnode) => {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (Array.isArray(vnode.props.children)) {
|
||||
vnode.props.children.push(...items);
|
||||
} else if (
|
||||
vnode.props.children !== null &&
|
||||
typeof vnode.props.children === "object"
|
||||
) {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
items.push(vnode.props.children as any);
|
||||
vnode.props.children = items;
|
||||
} else {
|
||||
vnode.props.children = items;
|
||||
}
|
||||
}
|
||||
|
||||
// Append component to render remaining head nodes
|
||||
const remaining = h(RemainingHead, null);
|
||||
// deno-lint-ignore no-explicit-any
|
||||
items.push(h(RemainingHead, null) as VNode<any>);
|
||||
|
||||
if (Array.isArray(vnode.props.children)) {
|
||||
vnode.props.children.push(remaining);
|
||||
vnode.props.children.push(...items);
|
||||
} else if (
|
||||
vnode.props.children !== null &&
|
||||
typeof vnode.props.children === "object"
|
||||
) {
|
||||
vnode.props.children = [vnode.props.children, remaining];
|
||||
// deno-lint-ignore no-explicit-any
|
||||
items.unshift(vnode.props.children as any);
|
||||
vnode.props.children = items;
|
||||
} else {
|
||||
vnode.props.children = remaining;
|
||||
vnode.props.children = items;
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -412,8 +403,25 @@ options[OptionsType.DIFFED] = (vnode) => {
|
||||
};
|
||||
|
||||
function RemainingHead() {
|
||||
if (RENDER_STATE !== null && RENDER_STATE.headComponents.size > 0) {
|
||||
return h(Fragment, null, ...RENDER_STATE.headComponents.values());
|
||||
if (RENDER_STATE !== null) {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const items: VNode<any>[] = [];
|
||||
if (RENDER_STATE.headComponents.size > 0) {
|
||||
items.push(...RENDER_STATE.headComponents.values());
|
||||
}
|
||||
|
||||
RENDER_STATE.islands.forEach((island) => {
|
||||
if (island.css.length > 0) {
|
||||
for (let i = 0; i < island.css.length; i++) {
|
||||
const css = island.css[i];
|
||||
items.push(h("link", { rel: "stylesheet", href: css }));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (items.length > 0) {
|
||||
return h(Fragment, null, items);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
.root {
|
||||
color: red;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// @ts-ignore upstream issue https://github.com/denoland/deno/issues/30560
|
||||
import styles from "./CssModules.module.css";
|
||||
|
||||
export function CssModules() {
|
||||
return <h1 class={styles.root}>red text</h1>;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { CssModules } from "../../islands/CssModules.tsx";
|
||||
|
||||
export default function Page() {
|
||||
return <CssModules />;
|
||||
}
|
||||
@@ -10,7 +10,8 @@
|
||||
},
|
||||
"compilerOptions": {
|
||||
"jsx": "precompile",
|
||||
"jsxImportSource": "preact"
|
||||
"jsxImportSource": "preact",
|
||||
"types": ["../../global.d.ts"]
|
||||
},
|
||||
"tasks": {
|
||||
"demo": "vite demo",
|
||||
|
||||
@@ -153,6 +153,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
||||
name: def.name,
|
||||
server: id,
|
||||
browser: def.chunk ?? `/@id/fresh-island::${def.name}`,
|
||||
css: [],
|
||||
};
|
||||
});
|
||||
} else {
|
||||
@@ -206,11 +207,14 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
||||
spec = spec.slice(1);
|
||||
}
|
||||
|
||||
const chunkCss = mod.css?.map((id) => `/${id}`) ?? [];
|
||||
|
||||
const name = namer.getUniqueName(specToName(id));
|
||||
islandMods.push({
|
||||
name,
|
||||
browser: spec,
|
||||
server: serverPath,
|
||||
css: chunkCss,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,3 +326,27 @@ Deno.test({
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "vite build - island css modules",
|
||||
fn: async () => {
|
||||
await launchProd(
|
||||
{ cwd: viteResult.tmp },
|
||||
async (address) => {
|
||||
await withBrowser(async (page) => {
|
||||
await page.goto(`${address}/tests/css_modules`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
const color = await page
|
||||
.locator("h1")
|
||||
// deno-lint-ignore no-explicit-any
|
||||
.evaluate((el) => window.getComputedStyle(el as any).color);
|
||||
expect(color).toEqual("rgb(255, 0, 0)");
|
||||
});
|
||||
},
|
||||
);
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
@@ -268,3 +268,24 @@ Deno.test({
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "vite dev - island css modules",
|
||||
fn: async () => {
|
||||
await launchDevServer(tmp.dir, async (address) => {
|
||||
await withBrowser(async (page) => {
|
||||
await page.goto(`${address}/tests/css_modules`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
const color = await page
|
||||
.locator("h1")
|
||||
// deno-lint-ignore no-explicit-any
|
||||
.evaluate((el) => window.getComputedStyle(el as any).color);
|
||||
expect(color).toEqual("rgb(255, 0, 0)");
|
||||
});
|
||||
});
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user