From c1f04ea94b5a5a308ba6885b7af56098366fd0f6 Mon Sep 17 00:00:00 2001 From: dpiercey Date: Thu, 16 May 2024 10:16:26 -0700 Subject: [PATCH] fix: avoid storing shared data on compiler --- package-lock.json | 2 +- src/loader/index.ts | 38 ++++++++++++++++++++------------------ src/plugin/index.ts | 24 ++++++++++++++++-------- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8433cb4..36f53ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "@marko/webpack", - "version": "9.3.6", + "version": "9.3.7", "license": "MIT", "dependencies": { "escape-string-regexp": "^4.0.0", diff --git a/src/loader/index.ts b/src/loader/index.ts index 588f472..7865062 100644 --- a/src/loader/index.ts +++ b/src/loader/index.ts @@ -9,13 +9,6 @@ import type { MarkoMeta, Config } from "@marko/compiler"; import getAssetCode from "./get-asset-code"; import { MANIFEST_PLACEHOLDER } from "../shared/manifest"; -declare module "webpack" { - interface Compiler { - markoCompileCache?: Map; - markoVirtualSources?: Map; - } -} - type LoaderOptions = loaderUtils.OptionObject & Config & { target?: webpack.loader.LoaderContext["target"]; @@ -76,13 +69,23 @@ export default async function ( const loaderOptions: LoaderOptions = (this as any).getOptions ? (this as any).getOptions() : loaderUtils.getOptions(this); - const pluginOptions = compiler.markoPluginOptions || {}; + const { + runtimeId, + markoCompileCache, + markoVirtualSources + }: { + runtimeId?: string; + markoCompileCache: Map; + markoVirtualSources: Map; + } = (compiler.markoPluginOptions ||= { + markoCompileCache: new Map(), + markoVirtualSources: new Map() + }); const sourceMaps = loaderOptions.sourceMaps ?? this.sourceMap; const target = normalizeTarget(loaderOptions.target || this.target); // eslint-disable-next-line @typescript-eslint/no-var-requires const markoCompiler = require(loaderOptions.compiler || DEFAULT_COMPILER) as typeof import("@marko/compiler"); - const virtualSources = (compiler.markoVirtualSources ||= new Map()); this.cacheable(true); @@ -106,8 +109,8 @@ export default async function ( return `export default ${MANIFEST_PLACEHOLDER}`; } - if (virtualSources.has(this.resource)) { - const { code, map } = virtualSources.get(this.resource); + if (markoVirtualSources.has(this.resource)) { + const { code, map } = markoVirtualSources.get(this.resource); return this.callback(null, code, map); } @@ -119,11 +122,11 @@ export default async function ( hot: this.hot, fileSystem: this.fs, writeVersionComment: false, - runtimeId: pluginOptions.runtimeId, - cache: (compiler.markoCompileCache ||= new Map()), + runtimeId, + cache: markoCompileCache, resolveVirtualDependency(resourcePath, { code, map, virtualPath }) { const absoluteVirtualPath = `${resourcePath}?virtual=${virtualPath}`; - virtualSources.set(absoluteVirtualPath, { code, map }); + markoVirtualSources.set(absoluteVirtualPath, { code, map }); return `${virtualPath}!=!${__filename}!${absoluteVirtualPath}`; }, babelConfig: { @@ -150,7 +153,7 @@ export default async function ( const { code, map } = await markoCompiler.compile( getAssetCode( resourcePath, - pluginOptions.runtimeId, + runtimeId, compiler.options.output.publicPath ), resourcePath.replace(/\.marko$/, "-server-entry.marko"), @@ -181,9 +184,7 @@ export default async function ( output: "hydrate" }); - const mwpVar = `$mwp${ - pluginOptions.runtimeId ? `_${pluginOptions.runtimeId}` : "" - }`; + const mwpVar = `$mwp${runtimeId ? `_${runtimeId}` : ""}`; const mwpPrefix = compiler.options.output.publicPath === undefined ? `if (window.${mwpVar}) __webpack_public_path__ = ${mwpVar};\n` @@ -254,6 +255,7 @@ function getTrailingContent( function getCompiler(ctx: webpack.loader.LoaderContext) { let compiler = ctx._compiler; + if ((compiler as any).root) return (compiler as any).root; while ((compiler as any).parentCompilation) { compiler = (compiler as any).parentCompilation.compiler; diff --git a/src/plugin/index.ts b/src/plugin/index.ts index 7db9549..3ac09c1 100644 --- a/src/plugin/index.ts +++ b/src/plugin/index.ts @@ -15,8 +15,10 @@ declare module "webpack" { interface Compiler { watchMode?: boolean; watching?: webpack.Watching; - markoCompileCache?: Map; - markoPluginOptions?: MarkoWebpackPlugin["options"]; + markoPluginOptions?: MarkoWebpackPlugin["options"] & { + markoCompileCache?: Map; + markoVirtualSources?: Map; + }; markoAssetsPending?: ResolvablePromise; markoAssetsRead?: boolean; markoEntriesPending?: ResolvablePromise; @@ -25,7 +27,6 @@ declare module "webpack" { } export default class MarkoWebpackPlugin { - private compileCache = new Map(); private serverCompiler: webpack.Compiler; private browserCompilers: webpack.Compiler[] = []; private clientEntries: { [x: string]: string } = {}; @@ -34,9 +35,18 @@ export default class MarkoWebpackPlugin { [entryName: string]: { [assetType: string]: string[] }; }; } = {}; - - constructor(private options: { runtimeId?: string } = {}) { - this.options = { ...options }; + private options: { + runtimeId?: string; + markoCompileCache: Map; + markoVirtualSources: Map; + }; + + constructor(options: { runtimeId?: string } = {}) { + this.options = { + ...options, + markoCompileCache: new Map(), + markoVirtualSources: new Map() + }; if (this.options.runtimeId) { this.options.runtimeId = normalizeRuntimeId(this.options.runtimeId); @@ -54,7 +64,6 @@ export default class MarkoWebpackPlugin { patchWatchingWebpack4(compiler); compiler.markoAssetsRead = false; compiler.markoPluginOptions = this.options; - compiler.markoCompileCache = this.compileCache; compiler.markoEntriesPending = createDeferredPromise(); compiler.hooks.invalid.tap("MarkoWebpackServer:invalid", () => { @@ -226,7 +235,6 @@ export default class MarkoWebpackPlugin { patchWatchingWebpack4(compiler); compiler.markoEntriesRead = false; compiler.markoPluginOptions = this.options; - compiler.markoCompileCache = this.compileCache; compiler.options.entry = async () => { await this.serverCompiler.markoEntriesPending;