Skip to content

Commit

Permalink
fix: avoid storing shared data on compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed May 16, 2024
1 parent 9070fa9 commit c1f04ea
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 20 additions & 18 deletions src/loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown, unknown>;
markoVirtualSources?: Map<string, { code: string | Buffer; map?: unknown }>;
}
}

type LoaderOptions = loaderUtils.OptionObject &
Config & {
target?: webpack.loader.LoaderContext["target"];
Expand Down Expand Up @@ -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<string, any>;
markoVirtualSources: Map<string, { code: string; map?: any }>;
} = (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);

Expand All @@ -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);
}

Expand All @@ -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: {
Expand All @@ -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"),
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 16 additions & 8 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ declare module "webpack" {
interface Compiler {
watchMode?: boolean;
watching?: webpack.Watching;
markoCompileCache?: Map<unknown, unknown>;
markoPluginOptions?: MarkoWebpackPlugin["options"];
markoPluginOptions?: MarkoWebpackPlugin["options"] & {
markoCompileCache?: Map<unknown, unknown>;
markoVirtualSources?: Map<string, { code: string | Buffer; map?: any }>;
};
markoAssetsPending?: ResolvablePromise<void>;
markoAssetsRead?: boolean;
markoEntriesPending?: ResolvablePromise<void>;
Expand All @@ -25,7 +27,6 @@ declare module "webpack" {
}

export default class MarkoWebpackPlugin {
private compileCache = new Map<unknown, unknown>();
private serverCompiler: webpack.Compiler;
private browserCompilers: webpack.Compiler[] = [];
private clientEntries: { [x: string]: string } = {};
Expand All @@ -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<unknown, unknown>;
markoVirtualSources: Map<string, { code: string | Buffer; map?: any }>;
};

constructor(options: { runtimeId?: string } = {}) {
this.options = {
...options,
markoCompileCache: new Map(),
markoVirtualSources: new Map()
};

if (this.options.runtimeId) {
this.options.runtimeId = normalizeRuntimeId(this.options.runtimeId);
Expand All @@ -54,7 +64,6 @@ export default class MarkoWebpackPlugin {
patchWatchingWebpack4(compiler);
compiler.markoAssetsRead = false;
compiler.markoPluginOptions = this.options;
compiler.markoCompileCache = this.compileCache;
compiler.markoEntriesPending = createDeferredPromise<void>();

compiler.hooks.invalid.tap("MarkoWebpackServer:invalid", () => {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c1f04ea

Please sign in to comment.