|
1 | 1 | import path from "path";
|
2 | 2 | import type { Plugin } from "esbuild";
|
3 | 3 | import fse from "fs-extra";
|
4 |
| -import LRUCache from "lru-cache"; |
5 | 4 | import { parse, type ParserOptions } from "@babel/parser";
|
6 | 5 | import traverse from "@babel/traverse";
|
7 | 6 | import generate from "@babel/generator";
|
@@ -56,28 +55,52 @@ export const cssSideEffectImportsPlugin = (
|
56 | 55 | build.onLoad(
|
57 | 56 | { filter: allJsFilesFilter, namespace: "file" },
|
58 | 57 | 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) { |
63 | 98 | return null;
|
64 | 99 | }
|
65 | 100 |
|
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 |
| - |
78 | 101 | return {
|
79 |
| - contents, |
80 |
| - loader, |
| 102 | + contents: cacheValue.contents, |
| 103 | + loader: cacheValue.loader, |
81 | 104 | };
|
82 | 105 | }
|
83 | 106 | );
|
@@ -129,20 +152,10 @@ const babelPluginsForLoader: Record<Loader, ParserOptions["plugins"]> = {
|
129 | 152 | tsx: ["typescript", "jsx", ...additionalLanguageFeatures],
|
130 | 153 | };
|
131 | 154 |
|
132 |
| -const cache = new LRUCache<string, string>({ max: 1000 }); |
133 |
| -const getCacheKey = (loader: Loader, code: string) => `${loader}:${code}`; |
134 |
| - |
135 | 155 | export function addSuffixToCssSideEffectImports(
|
136 | 156 | loader: Loader,
|
137 | 157 | code: string
|
138 | 158 | ): string {
|
139 |
| - let cacheKey = getCacheKey(loader, code); |
140 |
| - let cachedResult = cache.get(cacheKey); |
141 |
| - |
142 |
| - if (cachedResult) { |
143 |
| - return cachedResult; |
144 |
| - } |
145 |
| - |
146 | 159 | let ast = parse(code, {
|
147 | 160 | sourceType: "module",
|
148 | 161 | plugins: babelPluginsForLoader[loader],
|
@@ -192,7 +205,5 @@ export function addSuffixToCssSideEffectImports(
|
192 | 205 | compact: false,
|
193 | 206 | }).code;
|
194 | 207 |
|
195 |
| - cache.set(cacheKey, result); |
196 |
| - |
197 | 208 | return result;
|
198 | 209 | }
|
0 commit comments