Skip to content

Commit b0976b2

Browse files
authored
feat expose cache (#47)
* expose parts of the cache * changeset
1 parent 6ba3116 commit b0976b2

File tree

6 files changed

+48
-8
lines changed

6 files changed

+48
-8
lines changed

.changeset/slow-pants-eat.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@navita/webpack-plugin": minor
3+
"@navita/next-plugin": minor
4+
"@navita/engine": minor
5+
"@navita/core": minor
6+
---
7+
8+
This enables more usage of the caches in the rendering engine. It allows external tooling to hook into the navita process to do analysis or extraction for other tools

packages/core/src/createRenderer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export function createRenderer({
6969
return {
7070
result: newSource.toString(),
7171
dependencies,
72-
usedIds: engine.getUsedCacheIds([filePath]),
72+
usedIds: engine.getCacheIds([filePath]),
7373
sourceMap: newSource.generateMap(),
7474
};
7575
}

packages/engine/src/index.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export class Engine {
207207
fontFace: fontFaceCache,
208208
static: staticCache,
209209
rule: ruleCache
210-
} = usedIds ?? this.getUsedCacheIds(filePaths ?? Object.keys(this.usedIds));
210+
} = usedIds ?? this.getCacheIds(filePaths ?? Object.keys(this.usedIds));
211211

212212
const { atRules, lowPrioRules, rules } = splitStyleBlocks(this.caches.rule.items(ruleCache));
213213

@@ -258,8 +258,16 @@ export class Engine {
258258
}
259259

260260
serialize() {
261-
const { caches, usedIds, identifierCount, sourceMapReferences } = this;
262-
return JSON.stringify({ caches, usedIds, identifierCount, sourceMapReferences });
261+
const { caches, usedIds, identifierCount, sourceMapReferences: sourceMapReferencesData } = this;
262+
263+
const sourceMapReferences = this.options.enableSourceMaps ? sourceMapReferencesData : undefined;
264+
265+
return JSON.stringify({
266+
caches,
267+
usedIds,
268+
identifierCount,
269+
sourceMapReferences,
270+
});
263271
}
264272

265273
async deserialize(buffer: Buffer | string) {
@@ -284,6 +292,17 @@ export class Engine {
284292
this.filePath = filePath;
285293
}
286294

295+
getUsedFilePaths() {
296+
return Object.keys(this.usedIds);
297+
}
298+
299+
getItems(caches: UsedIdCache) {
300+
return Object.keys(caches).reduce((acc, key) => ({
301+
...acc,
302+
[key]: this.caches[key as CacheKeys].items(caches[key as CacheKeys]),
303+
}), {});
304+
}
305+
287306
clearUsedIds(filePath: string) {
288307
if (filePath === undefined) {
289308
return;
@@ -318,7 +337,7 @@ export class Engine {
318337
];
319338
}
320339

321-
getUsedCacheIds(filePaths: string[] = []) {
340+
getCacheIds(filePaths: string[] = []) {
322341
return filePaths.reduce((acc, filePath) => ({
323342
...acc,
324343
...Object.keys(this.usedIds[filePath] || []).reduce((cache, key) => ({

packages/engine/tests/src/index.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ describe('Engine', () => {
328328
static: [1],
329329
},
330330
});
331+
332+
expect(engine.getUsedFilePaths()).toEqual(['file1.ts']);
331333
});
332334

333335
it('unsets usedIds on clearUsedIds', () => {
@@ -351,11 +353,17 @@ describe('Engine', () => {
351353
},
352354
});
353355

356+
expect(engine.getItems(engine.getCacheIds(engine.getUsedFilePaths()))).toEqual({
357+
rule: ['a1'],
358+
});
359+
354360
engine.clearUsedIds('file1.ts');
355361

356362
expect(engine['usedIds']).toEqual({
357363
'file1.ts': {},
358364
});
365+
366+
expect(engine.getItems(engine.getCacheIds(engine.getUsedFilePaths()))).toEqual({});
359367
});
360368

361369
describe('identifiers', () => {

packages/next-plugin/src/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type MiniCssExtractPluginType from "mini-css-extract-plugin";
66
import type { NextConfig } from "next";
77
import NextMiniCssExtractPluginDefault from 'next/dist/build/webpack/plugins/mini-css-extract-plugin';
88

9+
export type { Renderer };
910
import { findPagesDir } from "next/dist/lib/find-pages-dir";
1011
import type { Configuration } from "webpack";
1112
import { optimizeCSSOutput } from "./optimizeCSSOutput";
@@ -15,7 +16,7 @@ let lastCache: string;
1516

1617
const MiniCssExtractPlugin = NextMiniCssExtractPluginDefault['default'] as typeof MiniCssExtractPluginType;
1718

18-
interface Config extends Omit<Options, 'useWebpackCache' | 'onRenderInitialized'> {
19+
interface Config extends Omit<Options, 'useWebpackCache'> {
1920
singleCssFile?: boolean;
2021
}
2122

@@ -124,6 +125,10 @@ export const createNavitaStylePlugin = (navitaConfig: Config = {}) =>
124125
// This will happen if the user doesn't have write access to the cache directory.
125126
// But the same should happen with the webpack cache.
126127
}
128+
129+
// If the user has provided their own onRenderInitialized function,
130+
// we'll do it after the cache is loaded.
131+
return navitaConfig.onRenderInitialized?.(renderer);
127132
};
128133

129134
config.plugins?.push({
@@ -149,9 +154,9 @@ export const createNavitaStylePlugin = (navitaConfig: Config = {}) =>
149154
config.plugins?.push(
150155
new NavitaPlugin({
151156
useWebpackCache: false,
152-
onRenderInitialized,
153157
outputCss,
154158
...navitaConfig,
159+
onRenderInitialized,
155160
optimizeCSSOutput,
156161
})
157162
);

packages/webpack-plugin/src/prepareCssOutput.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export function prepareCssOutput({
103103
}
104104

105105
for (const [chunk, value] of map) {
106-
value.usedIds = engine.getUsedCacheIds(value.filePaths);
106+
value.usedIds = engine.getCacheIds(value.filePaths);
107107
value.parents = Array.from(chunk.groupsIterable)
108108
.flatMap((x) => x.getParents())
109109
.flatMap((x) => x.chunks)

0 commit comments

Comments
 (0)