Skip to content

Commit 52e1e4a

Browse files
authored
fix: make Compilation check logs clearer (#9853)
1 parent 6b63330 commit 52e1e4a

File tree

6 files changed

+23
-30
lines changed

6 files changed

+23
-30
lines changed

packages/rspack/src/Compilation.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ export type NormalizedStatsOptions = KnownNormalizedStatsOptions &
184184
Omit<StatsOptions, keyof KnownNormalizedStatsOptions> &
185185
Record<string, any>;
186186

187+
export const checkCompilation = (compilation: Compilation) => {
188+
if (!(compilation instanceof Compilation)) {
189+
throw new TypeError(
190+
`The 'compilation' argument must be an instance of Compilation. This usually occurs when multiple versions of "@rspack/core" are used, or when the code in "@rspack/core" is executed multiple times.`
191+
);
192+
}
193+
};
194+
187195
export class Compilation {
188196
#inner: JsCompilation;
189197
#shutdown: boolean;

packages/rspack/src/NormalModule.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import util from "node:util";
22
import * as binding from "@rspack/binding";
33
import * as liteTapable from "@rspack/lite-tapable";
44
import type { Source } from "webpack-sources";
5-
import { Compilation } from "./Compilation";
5+
import { type Compilation, checkCompilation } from "./Compilation";
66
import type { Module } from "./Module";
77
import type { LoaderContext } from "./config";
88
import { JsSource } from "./util/source";
@@ -100,11 +100,8 @@ Object.defineProperty(binding.NormalModule, "getCompilationHooks", {
100100
enumerable: true,
101101
configurable: true,
102102
value(compilation: Compilation): NormalModuleCompilationHooks {
103-
if (!(compilation instanceof Compilation)) {
104-
throw new TypeError(
105-
"The 'compilation' argument must be an instance of Compilation"
106-
);
107-
}
103+
checkCompilation(compilation);
104+
108105
let hooks = compilationHooksMap.get(compilation);
109106
if (hooks === undefined) {
110107
hooks = {

packages/rspack/src/builtin-plugin/JavascriptModulesPlugin.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { type BuiltinPlugin, BuiltinPluginName } from "@rspack/binding";
22

33
import * as liteTapable from "@rspack/lite-tapable";
44
import type { Chunk } from "../Chunk";
5-
import { Compilation } from "../Compilation";
5+
import { type Compilation, checkCompilation } from "../Compilation";
66
import type Hash from "../util/hash";
77
import { RspackBuiltinPlugin, createBuiltinPlugin } from "./base";
88

@@ -22,11 +22,8 @@ export class JavascriptModulesPlugin extends RspackBuiltinPlugin {
2222
}
2323

2424
static getCompilationHooks(compilation: Compilation) {
25-
if (!(compilation instanceof Compilation)) {
26-
throw new TypeError(
27-
"The 'compilation' argument must be an instance of Compilation"
28-
);
29-
}
25+
checkCompilation(compilation);
26+
3027
let hooks = compilationHooksMap.get(compilation);
3128
if (hooks === undefined) {
3229
hooks = {

packages/rspack/src/builtin-plugin/RsdoctorPlugin.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
} from "@rspack/binding";
2727
import * as liteTapable from "@rspack/lite-tapable";
2828
import { z } from "zod";
29-
import { Compilation } from "../Compilation";
29+
import { type Compilation, checkCompilation } from "../Compilation";
3030
import type { Compiler } from "../Compiler";
3131
import type { CreatePartialRegisters } from "../taps/types";
3232
import { validate } from "../util/validate";
@@ -122,11 +122,8 @@ const RsdoctorPlugin = RsdoctorPluginImpl as typeof RsdoctorPluginImpl & {
122122
RsdoctorPlugin.getHooks = RsdoctorPlugin.getCompilationHooks = (
123123
compilation: Compilation
124124
) => {
125-
if (!(compilation instanceof Compilation)) {
126-
throw new TypeError(
127-
"The 'compilation' argument must be an instance of Compilation"
128-
);
129-
}
125+
checkCompilation(compilation);
126+
130127
let hooks = compilationHooksMap.get(compilation);
131128
if (hooks === undefined) {
132129
hooks = {

packages/rspack/src/builtin-plugin/RuntimePlugin.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as binding from "@rspack/binding";
22
import * as liteTapable from "@rspack/lite-tapable";
33

44
import { Chunk } from "../Chunk";
5-
import { Compilation } from "../Compilation";
5+
import { type Compilation, checkCompilation } from "../Compilation";
66
import type { CreatePartialRegisters } from "../taps/types";
77
import { create } from "./base";
88

@@ -32,11 +32,8 @@ const compilationHooksMap: WeakMap<Compilation, RuntimePluginHooks> =
3232
RuntimePlugin.getHooks = RuntimePlugin.getCompilationHooks = (
3333
compilation: Compilation
3434
) => {
35-
if (!(compilation instanceof Compilation)) {
36-
throw new TypeError(
37-
"The 'compilation' argument must be an instance of Compilation"
38-
);
39-
}
35+
checkCompilation(compilation);
36+
4037
let hooks = compilationHooksMap.get(compilation);
4138
if (hooks === undefined) {
4239
hooks = {

packages/rspack/src/builtin-plugin/html-plugin/hooks.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
JsBeforeEmitData
88
} from "@rspack/binding";
99
import * as liteTapable from "@rspack/lite-tapable";
10-
import { Compilation } from "../../Compilation";
10+
import { type Compilation, checkCompilation } from "../../Compilation";
1111
import type { HtmlRspackPluginOptions } from "./options";
1212

1313
const compilationHooksMap: WeakMap<Compilation, HtmlRspackPluginHooks> =
@@ -39,11 +39,8 @@ export type HtmlRspackPluginHooks = {
3939
};
4040

4141
export const getPluginHooks = (compilation: Compilation) => {
42-
if (!(compilation instanceof Compilation)) {
43-
throw new TypeError(
44-
"The 'compilation' argument must be an instance of Compilation"
45-
);
46-
}
42+
checkCompilation(compilation);
43+
4744
let hooks = compilationHooksMap.get(compilation);
4845
if (hooks === undefined) {
4946
hooks = {

0 commit comments

Comments
 (0)