From 758bf9a9c63ee11dcbc80618c2dc4bcce88e4dec Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Wed, 28 Sep 2022 09:22:49 -0700 Subject: [PATCH] feat: use Marko's internal caching --- src/transform/create-transform.ts | 49 +++++++++++++------------------ 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/src/transform/create-transform.ts b/src/transform/create-transform.ts index 8ab01be..449f8c9 100644 --- a/src/transform/create-transform.ts +++ b/src/transform/create-transform.ts @@ -1,6 +1,4 @@ import fs from "fs"; -import path from "path"; -import crypto from "crypto"; import { Buffer } from "buffer"; import mergeMaps from "merge-source-map"; import ConcatMap from "concat-with-sourcemaps"; @@ -15,7 +13,7 @@ const compiler = isMarko4 : require("@marko/compiler"); const taglib = isMarko4 ? compiler.taglibFinder : compiler.taglib; const compileSync = isMarko4 ? compiler.compile : compiler.compileSync; -const THIS_FILE = fs.readFileSync(__filename); +const cache = new Map(); let configuredGlobals = false; // Allows for resolving `.marko` files during compilation. @@ -25,32 +23,6 @@ if (!(".marko" in require.extensions)) { export default ({ browser }: { browser: boolean }) => { const transformer: Transformer = { - getCacheKey( - sourceText, - sourcePath, - transformOptions, - config = transformOptions.config - ) { - return crypto - .createHash("md5") - .setEncoding("utf-8") - .update(THIS_FILE) - .update("\0") - .update(sourceText) - .update("\0") - .update(path.relative(config.rootDir, sourcePath)) - .update("\0") - .update( - transformOptions.instrument || (config as any).instrument - ? "instrument" - : "" - ) - .update("\0") - .update(process.env.NODE_ENV || "") - .update("\0") - .update(process.env.MARKO_DEBUG || "") - .digest("hex"); - }, process(src, filename, transformOptions) { const config = transformOptions.config || transformOptions; const globalMarkoConfig = config.globals.marko as any; @@ -63,9 +35,11 @@ export default ({ browser }: { browser: boolean }) => { output, } : { + fileSystem: createVirtualFS(transformOptions.cacheFS), sourceMaps: true, modules: "cjs", output, + cache, }; if (globalMarkoConfig) { @@ -166,3 +140,20 @@ export default ({ browser }: { browser: boolean }) => { return transformer; }; + +function createVirtualFS(map: Map | undefined) { + if (!map) return fs; + + return { + ...fs, + readFileSync(...args: Parameters) { + const path = args[0] as any; + const source = map.get(path); + if (source !== undefined) { + return source; + } + + return fs.readFileSync(...args); + }, + }; +}