Skip to content

Commit 9f09ed1

Browse files
LingyuCoderCopilot
andauthored
fix: should export rspack function from compiler.rspack (#12636)
* fix: exports function from compiler.rspack * fix: exports function from compiler.rspack * Update tests/rspack-test/compilerCases/exports.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix: exports function from compiler.rspack --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 3de0ea5 commit 9f09ed1

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

packages/rspack/src/Compiler.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,22 @@ import { applyRspackOptionsDefaults, getPnpDefault } from './config/defaults';
3434
import type { PlatformTargetProperties } from './config/target';
3535
import ExecuteModulePlugin from './ExecuteModulePlugin';
3636
import ConcurrentCompilationError from './error/ConcurrentCompilationError';
37+
import * as rspackExports from './exports';
3738
import {
3839
ThreadsafeInputNodeFS,
3940
ThreadsafeIntermediateNodeFS,
4041
ThreadsafeOutputNodeFS,
4142
} from './FileSystem';
4243
import type { FileSystemInfoEntry } from './FileSystemInfo';
43-
import { rspack } from './index';
44+
import type { rspack } from './index';
4445
import Cache from './lib/Cache';
4546
import CacheFacade from './lib/CacheFacade';
4647
import { Logger } from './logging/Logger';
4748
import { NormalModuleFactory } from './NormalModuleFactory';
4849
import { ResolverFactory } from './ResolverFactory';
4950
import { RuleSetCompiler } from './RuleSetCompiler';
5051
import { createCompilerRuntimeGlobals } from './RuntimeGlobals';
52+
import { rspack as rspackFn } from './rspack';
5153
import { Stats } from './Stats';
5254
import {
5355
createCompilationHooksRegisters,
@@ -250,14 +252,16 @@ class Compiler {
250252
};
251253

252254
const compilerRuntimeGlobals = createCompilerRuntimeGlobals(options);
253-
this.webpack = {
254-
...rspack,
255-
RuntimeGlobals: compilerRuntimeGlobals,
256-
} as typeof rspack;
257-
this.rspack = {
258-
...rspack,
255+
const compilerFn = function (...params: Parameters<typeof rspackFn>) {
256+
return rspackFn(...params);
257+
};
258+
const compilerRspack = Object.assign(compilerFn, rspackExports, {
259259
RuntimeGlobals: compilerRuntimeGlobals,
260-
} as typeof rspack;
260+
}) as unknown as typeof rspack;
261+
compilerRspack.rspack = compilerRspack;
262+
compilerRspack.webpack = compilerRspack;
263+
this.webpack = compilerRspack;
264+
this.rspack = compilerRspack;
261265
this.root = this;
262266
this.outputPath = '';
263267

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const { RuntimeGlobals } = require("@rspack/core");
2+
3+
class MyPlugin {
4+
apply(compiler) {
5+
expect(typeof compiler.rspack).toBe("function");
6+
expect(compiler.rspack.sources).toBeTruthy();
7+
expect(compiler.rspack.Compilation).toBeTruthy();
8+
expect(compiler.rspack.RuntimeGlobals).toBeTruthy();
9+
expect(compiler.rspack.RuntimeGlobals).not.toBe(RuntimeGlobals);
10+
}
11+
}
12+
13+
/** @type {import('@rspack/test-tools').TCompilerCaseConfig} */
14+
module.exports = {
15+
description: "should export rspack function from compiler.rspack with unique RuntimeGlobals",
16+
options(context) {
17+
return {
18+
context: context.getSource(),
19+
entry: "./d",
20+
plugins: [new MyPlugin()]
21+
};
22+
},
23+
async build(_, compiler) {
24+
await new Promise(resolve => {
25+
compiler.run(() => {
26+
resolve();
27+
});
28+
});
29+
},
30+
};

0 commit comments

Comments
 (0)