Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion packages/docusaurus/src/webpack/plugins/ChunkAssetPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,27 @@ ${DocusaurusGetChunkAssetFn} = function(chunkId) { chunkId = ${JSON.stringify(
*/
export default class ChunkAssetPlugin {
apply(compiler: Compiler): void {
const runtimeModules = new Set<ChunkAssetRuntimeModule>();

compiler.hooks.thisCompilation.tap(PluginName, (compilation) => {
runtimeModules.clear();
compilation.hooks.additionalTreeRuntimeRequirements.tap(
PluginName,
(chunk) => {
compilation.addRuntimeModule(chunk, new ChunkAssetRuntimeModule());
const runtimeModule = new ChunkAssetRuntimeModule();
runtimeModules.add(runtimeModule);
compilation.addRuntimeModule(chunk, runtimeModule);
},
);
});

compiler.hooks.shutdown.tap(PluginName, () => {
// Rspack keeps runtime module generator callbacks alive after shutdown.
runtimeModules.forEach((runtimeModule) =>
runtimeModule.clearCompilationReferences(),
);
runtimeModules.clear();
});
}
}

Expand All @@ -78,4 +91,10 @@ class ChunkAssetRuntimeModule extends webpack.RuntimeModule {
override generate() {
return generateGetChunkAssetRuntimeCode(this.chunk!);
}

clearCompilationReferences() {
this.compilation = undefined;
this.chunk = undefined;
this.chunkGraph = undefined;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {describe, expect, it, vi} from 'vitest';
import {fromPartial} from '@total-typescript/shoehorn';
import ChunkAssetPlugin from '../ChunkAssetPlugin';
import type webpack from 'webpack';

function createHook<Args extends unknown[]>() {
const handlers: ((...args: Args) => void)[] = [];
return {
call: (...args: Args) => handlers.forEach((handler) => handler(...args)),
hook: {
tap: (_name: string, handler: (...args: Args) => void) => {
handlers.push(handler);
},
},
};
}

describe('ChunkAssetPlugin', () => {
it('detaches runtime modules when the compiler shuts down', () => {
const thisCompilation = createHook<[webpack.Compilation]>();
const shutdown = createHook<[]>();
const additionalTreeRuntimeRequirements = createHook<[webpack.Chunk]>();
const chunk = fromPartial<webpack.Chunk>({});
const chunkGraph = fromPartial<webpack.ChunkGraph>({});
const addRuntimeModule = vi.fn();
const compilation = fromPartial<webpack.Compilation>({
addRuntimeModule,
hooks: {
additionalTreeRuntimeRequirements:
additionalTreeRuntimeRequirements.hook,
},
});
addRuntimeModule.mockImplementation(
(runtimeChunk: webpack.Chunk, runtimeModule: webpack.RuntimeModule) => {
runtimeModule.attach(compilation, runtimeChunk, chunkGraph);
},
);
const compiler = fromPartial<webpack.Compiler>({
hooks: {
shutdown: shutdown.hook,
thisCompilation: thisCompilation.hook,
},
});

new ChunkAssetPlugin().apply(compiler);
thisCompilation.call(compilation);
additionalTreeRuntimeRequirements.call(chunk);

const runtimeModule = addRuntimeModule.mock
.calls[0]![1] as webpack.RuntimeModule;
expect(runtimeModule.compilation).toBe(compilation);
expect(runtimeModule.chunk).toBe(chunk);
expect(runtimeModule.chunkGraph).toBe(chunkGraph);

shutdown.call();

expect(runtimeModule.compilation).toBeUndefined();
expect(runtimeModule.chunk).toBeUndefined();
expect(runtimeModule.chunkGraph).toBeUndefined();
});
});