From 1fd012a31f8a9856c8951f61ee59e933fca43d15 Mon Sep 17 00:00:00 2001 From: lippzhang <489435760@qq.com> Date: Thu, 6 Jul 2023 16:58:09 +0800 Subject: [PATCH] chore(packages/rspack): add version check for binding --- packages/rspack/src/rspack.ts | 4 ++++ .../rspack/src/util/bindingVersionCheck.ts | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 packages/rspack/src/util/bindingVersionCheck.ts diff --git a/packages/rspack/src/rspack.ts b/packages/rspack/src/rspack.ts index 10582b31fa57..9dfaaf0cbefd 100644 --- a/packages/rspack/src/rspack.ts +++ b/packages/rspack/src/rspack.ts @@ -30,6 +30,7 @@ import { Callback } from "tapable"; import MultiStats from "./multiStats"; import assert from "assert"; import { asArray, isNil } from "./util"; +import { bindingVersionCheck } from "./util/bindingVersionCheck"; import IgnoreWarningsPlugin from "./lib/ignoreWarningsPlugin"; function createMultiCompiler(options: MultiRspackOptions): MultiCompiler { @@ -113,6 +114,9 @@ function rspack( options: MultiRspackOptions | RspackOptions, callback?: Callback | Callback ) { + bindingVersionCheck(err => { + if (err) throw err; + }); asArray(options).every(opts => { validateConfig(opts); }); diff --git a/packages/rspack/src/util/bindingVersionCheck.ts b/packages/rspack/src/util/bindingVersionCheck.ts new file mode 100644 index 000000000000..0afdba8577bd --- /dev/null +++ b/packages/rspack/src/util/bindingVersionCheck.ts @@ -0,0 +1,24 @@ +function getBindingVersion() { + return require("@rspack/binding/package.json").version; +} +function getRspackVersion() { + return require("../../package.json").version; +} + +type Callback = (error: Error | null) => void; + +export const bindingVersionCheck = (callback: Callback) => { + const coreVersion = getRspackVersion(); + const bindingVersion = getBindingVersion(); + if (coreVersion !== bindingVersion) { + return callback( + new Error( + `The rspack core version ${JSON.stringify( + coreVersion + )} is not match to binding version ${JSON.stringify(bindingVersion)}` + ) + ); + } else { + return callback(null); + } +};