Skip to content

Commit 1fff015

Browse files
authored
perf: define Rspack version to reduce require calls (#10121)
* perf: define Rspack version to reduce require calls * fix: release canary * fix: canary build * fix: improve config merging
1 parent 95dab1f commit 1fff015

File tree

9 files changed

+30
-36
lines changed

9 files changed

+30
-36
lines changed

.github/workflows/release-canary.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ jobs:
9090
with:
9191
path: artifacts
9292

93-
- name: Build node packages
94-
run: pnpm run build:js
95-
9693
- name: Move artifacts
9794
run: node scripts/build-npm.cjs
9895

@@ -105,6 +102,7 @@ jobs:
105102
- name: Release
106103
run: |
107104
./x version snapshot
105+
pnpm run build:js:canary
108106
./x publish snapshot --tag canary
109107
env:
110108
NPM_TOKEN: ${{ secrets.RSPACK_CANARY_RELEASE_TOKEN }}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"clean": "pnpm --filter @rspack/cli run clean",
1212
"check-dependency-version": "pnpx check-dependency-version-consistency@5 . --ignore-dep chalk --ignore-package webpack-test --ignore-package webpack-examples",
1313
"build:js": "pnpm --filter \"@rspack/core\" build && pnpm --parallel --filter \"@rspack/*\" --filter \"create-rspack\" --filter \"!@rspack/core\" build",
14+
"build:js:canary": "pnpm --filter \"@rspack-canary/core\" build && pnpm --parallel --filter \"@rspack-canary/*\" --filter \"create-rspack-canary\" --filter \"!@rspack-canary/core\" build",
1415
"build:cli:dev": "npm run build:binding:dev && npm run build:js",
1516
"build:cli:dev:wasm": "pnpm --filter @rspack/binding build:dev:wasm && npm run build:js",
1617
"build:cli:release": "npm run build:binding:release && npm run build:js",

packages/rspack/rslib.config.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { type LibConfig, defineConfig } from "@rslib/core";
1+
import { type LibConfig, defineConfig, rsbuild } from "@rslib/core";
22
import prebundleConfig from "./prebundle.config.mjs";
33

4+
const merge = rsbuild.mergeRsbuildConfig;
5+
46
const externalAlias = ({ request }: { request?: string }, callback) => {
57
const { dependencies } = prebundleConfig;
68

@@ -25,6 +27,12 @@ const externalAlias = ({ request }: { request?: string }, callback) => {
2527
const commonLibConfig: LibConfig = {
2628
format: "cjs",
2729
syntax: ["node 16"],
30+
source: {
31+
define: {
32+
WEBPACK_VERSION: JSON.stringify(require("./package.json").webpackVersion),
33+
RSPACK_VERSION: JSON.stringify(require("./package.json").version)
34+
}
35+
},
2836
output: {
2937
externals: [externalAlias],
3038
minify: {
@@ -57,8 +65,7 @@ const commonLibConfig: LibConfig = {
5765

5866
export default defineConfig({
5967
lib: [
60-
{
61-
...commonLibConfig,
68+
merge(commonLibConfig, {
6269
dts: {
6370
build: true
6471
},
@@ -69,33 +76,29 @@ export default defineConfig({
6976
tsconfigPath: "./tsconfig.build.json"
7077
},
7178
output: {
72-
...commonLibConfig.output,
7379
externals: [externalAlias, "./moduleFederationDefaultRuntime.js"]
7480
},
7581
footer: {
7682
// make default export in cjs work
7783
js: "module.exports = __webpack_exports__.default;"
7884
}
79-
},
80-
{
81-
...commonLibConfig,
85+
}),
86+
merge(commonLibConfig, {
8287
source: {
8388
entry: {
8489
cssExtractLoader: "./src/builtin-plugin/css-extract/loader.ts"
8590
}
8691
}
87-
},
88-
{
89-
...commonLibConfig,
92+
}),
93+
merge(commonLibConfig, {
9094
syntax: "es2015",
9195
source: {
9296
entry: {
9397
cssExtractHmr: "./src/runtime/cssExtractHmr.ts"
9498
}
9599
}
96-
},
97-
{
98-
...commonLibConfig,
100+
}),
101+
merge(commonLibConfig, {
99102
source: {
100103
entry: {
101104
worker: "./src/loader-runner/worker.ts"
@@ -105,6 +108,6 @@ export default defineConfig({
105108
// make default export in cjs work
106109
js: "module.exports = __webpack_exports__.default;"
107110
}
108-
}
111+
})
109112
]
110113
});

packages/rspack/src/MultiStats.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ export default class MultiStats {
8888
return obj;
8989
});
9090
if (childOptions.version) {
91-
obj.rspackVersion = require("../package.json").version;
92-
obj.version = require("../package.json").webpackVersion;
91+
obj.rspackVersion = RSPACK_VERSION;
92+
obj.version = WEBPACK_VERSION;
9393
}
9494
if (childOptions.hash) {
9595
obj.hash = obj.children.map(j => j.hash).join("");

packages/rspack/src/config/defaults.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,7 @@ const applybundlerInfoDefaults = (
260260
if (typeof rspackFuture === "object") {
261261
D(rspackFuture, "bundlerInfo", {});
262262
if (typeof rspackFuture.bundlerInfo === "object") {
263-
D(
264-
rspackFuture.bundlerInfo,
265-
"version",
266-
require("../../package.json").version
267-
);
263+
D(rspackFuture.bundlerInfo, "version", RSPACK_VERSION);
268264
D(rspackFuture.bundlerInfo, "bundler", "rspack");
269265
// don't inject for library mode
270266
D(rspackFuture.bundlerInfo, "force", !library);

packages/rspack/src/env.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Env variables defined by `rslib.config.ts`
2+
declare const WEBPACK_VERSION: string;
3+
declare const RSPACK_VERSION: string;

packages/rspack/src/exports.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
import {
2-
version as _version,
3-
webpackVersion as _webpackVersion
4-
// @ts-ignore 'package.json' is not under 'rootDir'
5-
} from "../package.json";
6-
71
// this is a hack to be compatible with plugin which detect webpack's version
8-
const rspackVersion = _version as string;
9-
const version = _webpackVersion as string;
2+
const rspackVersion = RSPACK_VERSION as string;
3+
const version = WEBPACK_VERSION as string;
104

115
export { rspackVersion, version };
126

packages/rspack/src/stats/DefaultStatsFactoryPlugin.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -833,9 +833,8 @@ const SIMPLE_EXTRACTORS: SimpleExtractors = {
833833
object.hash = statsCompilation.hash;
834834
},
835835
version: object => {
836-
const { version, webpackVersion } = require("../../package.json");
837-
object.version = webpackVersion;
838-
object.rspackVersion = version;
836+
object.version = RSPACK_VERSION;
837+
object.rspackVersion = WEBPACK_VERSION;
839838
},
840839
env: (object, _compilation, _context, { _env }) => {
841840
object.env = _env;

packages/rspack/src/util/bindingVersionCheck.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function isMusl() {
5454
}
5555

5656
const BINDING_VERSION = require("@rspack/binding/package.json").version;
57-
const CORE_VERSION = require("../../package.json").version;
57+
const CORE_VERSION = RSPACK_VERSION;
5858

5959
const getAddonPlatformArchAbi = () => {
6060
const { platform, arch } = process;

0 commit comments

Comments
 (0)