Skip to content

Commit

Permalink
fix: remove default compiler cache clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Oct 16, 2024
1 parent b2924d9 commit 420405d
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 23 deletions.
8 changes: 8 additions & 0 deletions .changeset/forty-eagles-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@marko/translator-tags": patch
"@marko/compiler": patch
"marko": patch
---

Remove the default cache auto clearing behavior.
Previously the default compiler "cache" was cleared every setImmediate. This was to support server hot reloading in apps using `Lasso` (and `browser-refresh`). Since we brought back support for `browser-refresh` in the Marko package we now clear this cache when browser-refresh triggers a change making the default cache clearing redundant.
4 changes: 0 additions & 4 deletions packages/compiler/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ const config = {
/**
* Compiling a Marko template may require other (used) Marko templates to compile.
* To prevent compiling templates more than once, most of the compilation is cached.
*
* The default cache strategy is to clear the cache on every macrotask.
* If the default cache is overwritten it is up to the user to determine when the
* cache is cleared.
*/
cache: new Map(),

Expand Down
17 changes: 0 additions & 17 deletions packages/compiler/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ export async function compile(src, filename, config) {
const markoConfig = loadMarkoConfig(config);
const babelConfig = await loadBabelConfig(filename, markoConfig);
const babelResult = await babel.transformAsync(src, babelConfig);
scheduleDefaultClear(markoConfig);
return buildResult(src, filename, markoConfig.errorRecovery, babelResult);
}

export function compileSync(src, filename, config) {
const markoConfig = loadMarkoConfig(config);
const babelConfig = loadBabelConfigSync(filename, markoConfig);
const babelResult = babel.transformSync(src, babelConfig);
scheduleDefaultClear(markoConfig);
return buildResult(src, filename, markoConfig.errorRecovery, babelResult);
}

Expand Down Expand Up @@ -154,25 +152,10 @@ function buildResult(src, filename, errorRecovery, babelResult) {
return { ast, map, code, meta };
}

let clearingDefaultCache = false;
function scheduleDefaultClear(config) {
if (
!clearingDefaultCache &&
(clearingDefaultCache = isDefaultCache(config))
) {
setImmediate(_clearDefaults);
}
}

export function _clearDefaults() {
clearingDefaultCache = false;
globalConfig.cache.clear();
}

function isDefaultCache(config) {
return !config.cache || config.cache === globalConfig.cache;
}

function getFs(config) {
return config.fileSystem || globalConfig.fileSystem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ Error: Change handler '_y_change' cannot change from a function to null
at signal (/packages/runtime-tags/src/dom/signals.ts:94:13)
at runBatch (/packages/runtime-tags/src/dom/queue.ts:107:5)
at run (/packages/runtime-tags/src/dom/queue.ts:52:5)
at csr (/packages/translator-tags/src/__tests__/main.test.ts:280:17)
at csr (/packages/translator-tags/src/__tests__/main.test.ts:282:17)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at /packages/translator-tags/src/__tests__/main.test.ts:367:37
at /packages/translator-tags/src/__tests__/main.test.ts:369:37
at resolveFixture (/node_modules/mocha-snap/dist/util/resolve-fixture.js:64:16)
at snap (/node_modules/mocha-snap/dist/snap.js:46:20)
```
2 changes: 2 additions & 0 deletions packages/translator-tags/src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const baseConfig: compiler.Config = {

const htmlConfig: compiler.Config = { ...baseConfig, output: "html" };
const domConfig: compiler.Config = { ...baseConfig, output: "dom" };
const snapCache = new Map<unknown, unknown>();

describe("translator-tags", () => {
before(() => {
Expand Down Expand Up @@ -107,6 +108,7 @@ describe("translator-tags", () => {
});
const finalConfig: compiler.Config = {
...compilerConfig,
cache: snapCache, // these need a different cache since they `resolveVirtualDependency` is relevant to the compile cache.
resolveVirtualDependency(_filename, { code, virtualPath }) {
return `virtual:${virtualPath} ${code}`;
},
Expand Down
2 changes: 2 additions & 0 deletions packages/translator-tags/src/__tests__/utils/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function bundle(
entryTemplate: string,
compilerConfig: compiler.Config,
) {
const cache = new Map<unknown, unknown>();
const optimizeKnownTemplates: string[] = await glob(
path.join(path.dirname(entryTemplate), "**/*.marko"),
{ absolute: true },
Expand Down Expand Up @@ -77,6 +78,7 @@ export async function bundle(
return (
await compiler.compile(code, id, {
...compilerConfig,
cache,
optimize: true,
optimizeKnownTemplates,
output: isHydrate ? "hydrate" : "dom",
Expand Down

0 comments on commit 420405d

Please sign in to comment.