Skip to content

Commit 539a7e8

Browse files
committed
Generate combined CSS
1 parent bfd211b commit 539a7e8

4 files changed

Lines changed: 42 additions & 11 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.starMe:hover {
2+
box-shadow: 0 0 10px red;
3+
}

lib/esbuild-plugin-react18-css-example/src/server/fork-me/fork-me.module.css renamed to lib/esbuild-plugin-react18-css-example/src/server/fork-me/fork-me.module.scss

File renamed without changes.

lib/esbuild-plugin-react18-css-example/src/server/fork-me/fork-me.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react";
2-
import cssClasses from "./fork-me.module.css";
2+
import cssClasses from "./fork-me.module.scss";
33

44
interface ForkMeProps {
55
gitHubUrl: string;

lib/esbuild-plugin-react18-css/src/index.ts

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin, PluginBuild } from "esbuild";
1+
import { BuildResult, Plugin, PluginBuild } from "esbuild";
22
import fs from "node:fs";
33
import path from "node:path";
44
import postcss from "postcss";
@@ -17,6 +17,38 @@ interface CSSModulePluginOptions {
1717
globalPrefix?: string;
1818
}
1919

20+
function generateCombinedCSS(result: BuildResult) {
21+
/** generate combined server and client CSS */
22+
const serverRegExp = new RegExp(`server\\${path.sep}index\\.css`);
23+
const serverCSSFile = result.outputFiles?.filter(f => f.path.match(serverRegExp))[0];
24+
25+
const clientRegExp = new RegExp(`client\\${path.sep}index\\.css`);
26+
const clientCSSFile = result.outputFiles?.filter(f => f.path.match(clientRegExp))[0];
27+
28+
if (!serverCSSFile && !clientCSSFile) return;
29+
30+
const combinedCSS = (clientCSSFile?.text ?? "") + (serverCSSFile?.text ?? "");
31+
let indexCSSFile;
32+
let indexCSSFilePath: string;
33+
if (clientCSSFile) {
34+
indexCSSFilePath = clientCSSFile.path.replace(`client${path.sep}`, "");
35+
indexCSSFile = result.outputFiles?.filter(f => f.path === indexCSSFilePath)[0];
36+
} else {
37+
indexCSSFilePath = serverCSSFile?.path.replace(`server${path.sep}`, "") ?? "";
38+
}
39+
40+
console.log({ indexCSSFilePath });
41+
42+
if (indexCSSFile) indexCSSFile.contents = new TextEncoder().encode(combinedCSS);
43+
else
44+
result.outputFiles?.push({
45+
path: indexCSSFilePath,
46+
contents: new TextEncoder().encode(combinedCSS),
47+
text: combinedCSS,
48+
hash: uuid(),
49+
});
50+
}
51+
2052
function applyAutoPrefixer(build: PluginBuild, options: CSSModulePluginOptions, write?: boolean) {
2153
build.onEnd(async result => {
2254
if (!options.skipAutoPrefixer) {
@@ -26,6 +58,8 @@ function applyAutoPrefixer(build: PluginBuild, options: CSSModulePluginOptions,
2658
}
2759
}
2860

61+
generateCombinedCSS(result);
62+
2963
/** assume true if undefined */
3064
if (write === undefined || write) {
3165
result.outputFiles?.forEach(file => {
@@ -43,13 +77,9 @@ function handleScss(build: PluginBuild) {
4377
}));
4478
}
4579

46-
function handleModules(
47-
build: PluginBuild,
48-
{ generateScopedName }: CSSModulePluginOptions,
49-
type: "css" | "scss" | "sass" = "css",
50-
) {
51-
const namespace = `${type}-module`;
52-
const filter = new RegExp(`\\.module\\.${type}$`);
80+
function handleModules(build: PluginBuild, { generateScopedName }: CSSModulePluginOptions) {
81+
const namespace = "scss-module";
82+
const filter = /\.module\.(sc|sa|c)ss/;
5383
build.onResolve({ filter, namespace: "file" }, args => ({
5484
path: `${args.path}#${namespace}`,
5585
namespace,
@@ -104,8 +134,6 @@ const cssPlugin: (options?: CSSModulePluginOptions) => Plugin = (options = {}) =
104134
`${path.basename(filename).split(".")[0]}__${name}`;
105135
}
106136
handleModules(build, options);
107-
handleModules(build, options, "scss");
108-
handleModules(build, options, "sass");
109137
handleScss(build);
110138
applyAutoPrefixer(build, options, write);
111139
},

0 commit comments

Comments
 (0)