Skip to content

Commit aa896a4

Browse files
fix(dev): cache CSS side-effects transform for HMR (#6622)
1 parent 43ec6af commit aa896a4

File tree

4 files changed

+47
-37
lines changed

4 files changed

+47
-37
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@remix-run/dev": patch
3+
---
4+
5+
Cache CSS side-effect imports transform when using HMR

packages/remix-dev/compiler/plugins/cssSideEffectImports.ts

+42-31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import path from "path";
22
import type { Plugin } from "esbuild";
33
import fse from "fs-extra";
4-
import LRUCache from "lru-cache";
54
import { parse, type ParserOptions } from "@babel/parser";
65
import traverse from "@babel/traverse";
76
import generate from "@babel/generator";
@@ -56,28 +55,52 @@ export const cssSideEffectImportsPlugin = (
5655
build.onLoad(
5756
{ filter: allJsFilesFilter, namespace: "file" },
5857
async (args) => {
59-
let code = await fse.readFile(args.path, "utf8");
60-
61-
// Don't process file if it doesn't contain any references to CSS files
62-
if (!code.includes(".css")) {
58+
let cacheKey = `css-side-effect-imports-plugin:${args.path}&hmr=${hmr}`;
59+
let { cacheValue } = await ctx.fileWatchCache.getOrSet(
60+
cacheKey,
61+
async () => {
62+
let fileDependencies = new Set([args.path]);
63+
64+
let code = await fse.readFile(args.path, "utf8");
65+
66+
// Don't process file if it doesn't contain any references to CSS files
67+
if (!code.includes(".css")) {
68+
return {
69+
fileDependencies,
70+
cacheValue: null,
71+
};
72+
}
73+
74+
let loader =
75+
loaderForExtension[path.extname(args.path) as Extension];
76+
let contents = addSuffixToCssSideEffectImports(loader, code);
77+
78+
if (args.path.startsWith(ctx.config.appDirectory) && hmr) {
79+
contents = await applyHMR(
80+
contents,
81+
args,
82+
ctx.config,
83+
!!build.initialOptions.sourcemap
84+
);
85+
}
86+
87+
return {
88+
fileDependencies,
89+
cacheValue: {
90+
contents,
91+
loader,
92+
},
93+
};
94+
}
95+
);
96+
97+
if (!cacheValue) {
6398
return null;
6499
}
65100

66-
let loader = loaderForExtension[path.extname(args.path) as Extension];
67-
let contents = addSuffixToCssSideEffectImports(loader, code);
68-
69-
if (args.path.startsWith(ctx.config.appDirectory) && hmr) {
70-
contents = await applyHMR(
71-
contents,
72-
args,
73-
ctx.config,
74-
!!build.initialOptions.sourcemap
75-
);
76-
}
77-
78101
return {
79-
contents,
80-
loader,
102+
contents: cacheValue.contents,
103+
loader: cacheValue.loader,
81104
};
82105
}
83106
);
@@ -129,20 +152,10 @@ const babelPluginsForLoader: Record<Loader, ParserOptions["plugins"]> = {
129152
tsx: ["typescript", "jsx", ...additionalLanguageFeatures],
130153
};
131154

132-
const cache = new LRUCache<string, string>({ max: 1000 });
133-
const getCacheKey = (loader: Loader, code: string) => `${loader}:${code}`;
134-
135155
export function addSuffixToCssSideEffectImports(
136156
loader: Loader,
137157
code: string
138158
): string {
139-
let cacheKey = getCacheKey(loader, code);
140-
let cachedResult = cache.get(cacheKey);
141-
142-
if (cachedResult) {
143-
return cachedResult;
144-
}
145-
146159
let ast = parse(code, {
147160
sourceType: "module",
148161
plugins: babelPluginsForLoader[loader],
@@ -192,7 +205,5 @@ export function addSuffixToCssSideEffectImports(
192205
compact: false,
193206
}).code;
194207

195-
cache.set(cacheKey, result);
196-
197208
return result;
198209
}

packages/remix-dev/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
"json5": "^2.2.2",
5050
"lodash": "^4.17.21",
5151
"lodash.debounce": "^4.0.8",
52-
"lru-cache": "^7.14.1",
5352
"minimatch": "^9.0.0",
5453
"node-fetch": "^2.6.9",
5554
"ora": "^5.4.1",

yarn.lock

-5
Original file line numberDiff line numberDiff line change
@@ -9487,11 +9487,6 @@ lru-cache@^6.0.0:
94879487
dependencies:
94889488
yallist "^4.0.0"
94899489

9490-
lru-cache@^7.14.1:
9491-
version "7.14.1"
9492-
resolved "https://registry.npmjs.org/lru-cache/-/lru-cache-7.14.1.tgz"
9493-
integrity sha512-ysxwsnTKdAx96aTRdhDOCQfDgbHnt8SK0KY8SEjO0wHinhWOFTESbjVCMPbU1uGXg/ch4lifqx0wfjOawU2+WA==
9494-
94959490
lz-string@^1.4.4:
94969491
version "1.4.4"
94979492
resolved "https://registry.npmjs.org/lz-string/-/lz-string-1.4.4.tgz"

0 commit comments

Comments
 (0)