Skip to content

Commit

Permalink
feat expose cache (#47)
Browse files Browse the repository at this point in the history
* expose parts of the cache

* changeset
  • Loading branch information
Zn4rK committed Apr 14, 2024
1 parent 6ba3116 commit b0976b2
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .changeset/slow-pants-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@navita/webpack-plugin": minor
"@navita/next-plugin": minor
"@navita/engine": minor
"@navita/core": minor
---

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
2 changes: 1 addition & 1 deletion packages/core/src/createRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function createRenderer({
return {
result: newSource.toString(),
dependencies,
usedIds: engine.getUsedCacheIds([filePath]),
usedIds: engine.getCacheIds([filePath]),
sourceMap: newSource.generateMap(),
};
}
Expand Down
27 changes: 23 additions & 4 deletions packages/engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class Engine {
fontFace: fontFaceCache,
static: staticCache,
rule: ruleCache
} = usedIds ?? this.getUsedCacheIds(filePaths ?? Object.keys(this.usedIds));
} = usedIds ?? this.getCacheIds(filePaths ?? Object.keys(this.usedIds));

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

Expand Down Expand Up @@ -258,8 +258,16 @@ export class Engine {
}

serialize() {
const { caches, usedIds, identifierCount, sourceMapReferences } = this;
return JSON.stringify({ caches, usedIds, identifierCount, sourceMapReferences });
const { caches, usedIds, identifierCount, sourceMapReferences: sourceMapReferencesData } = this;

const sourceMapReferences = this.options.enableSourceMaps ? sourceMapReferencesData : undefined;

return JSON.stringify({
caches,
usedIds,
identifierCount,
sourceMapReferences,
});
}

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

getUsedFilePaths() {
return Object.keys(this.usedIds);
}

getItems(caches: UsedIdCache) {
return Object.keys(caches).reduce((acc, key) => ({
...acc,
[key]: this.caches[key as CacheKeys].items(caches[key as CacheKeys]),
}), {});
}

clearUsedIds(filePath: string) {
if (filePath === undefined) {
return;
Expand Down Expand Up @@ -318,7 +337,7 @@ export class Engine {
];
}

getUsedCacheIds(filePaths: string[] = []) {
getCacheIds(filePaths: string[] = []) {
return filePaths.reduce((acc, filePath) => ({
...acc,
...Object.keys(this.usedIds[filePath] || []).reduce((cache, key) => ({
Expand Down
8 changes: 8 additions & 0 deletions packages/engine/tests/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ describe('Engine', () => {
static: [1],
},
});

expect(engine.getUsedFilePaths()).toEqual(['file1.ts']);
});

it('unsets usedIds on clearUsedIds', () => {
Expand All @@ -351,11 +353,17 @@ describe('Engine', () => {
},
});

expect(engine.getItems(engine.getCacheIds(engine.getUsedFilePaths()))).toEqual({
rule: ['a1'],
});

engine.clearUsedIds('file1.ts');

expect(engine['usedIds']).toEqual({
'file1.ts': {},
});

expect(engine.getItems(engine.getCacheIds(engine.getUsedFilePaths()))).toEqual({});
});

describe('identifiers', () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/next-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type MiniCssExtractPluginType from "mini-css-extract-plugin";
import type { NextConfig } from "next";
import NextMiniCssExtractPluginDefault from 'next/dist/build/webpack/plugins/mini-css-extract-plugin';

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

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

interface Config extends Omit<Options, 'useWebpackCache' | 'onRenderInitialized'> {
interface Config extends Omit<Options, 'useWebpackCache'> {
singleCssFile?: boolean;
}

Expand Down Expand Up @@ -124,6 +125,10 @@ export const createNavitaStylePlugin = (navitaConfig: Config = {}) =>
// This will happen if the user doesn't have write access to the cache directory.
// But the same should happen with the webpack cache.
}

// If the user has provided their own onRenderInitialized function,
// we'll do it after the cache is loaded.
return navitaConfig.onRenderInitialized?.(renderer);
};

config.plugins?.push({
Expand All @@ -149,9 +154,9 @@ export const createNavitaStylePlugin = (navitaConfig: Config = {}) =>
config.plugins?.push(
new NavitaPlugin({
useWebpackCache: false,
onRenderInitialized,
outputCss,
...navitaConfig,
onRenderInitialized,
optimizeCSSOutput,
})
);
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-plugin/src/prepareCssOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function prepareCssOutput({
}

for (const [chunk, value] of map) {
value.usedIds = engine.getUsedCacheIds(value.filePaths);
value.usedIds = engine.getCacheIds(value.filePaths);
value.parents = Array.from(chunk.groupsIterable)
.flatMap((x) => x.getParents())
.flatMap((x) => x.chunks)
Expand Down

0 comments on commit b0976b2

Please sign in to comment.