Skip to content
Merged
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
2 changes: 2 additions & 0 deletions crates/node_binding/napi-binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,8 @@ export interface JsRuntimeModule {
moduleIdentifier: string
constructorName: string
name: string
stage: number
isolate: boolean
}

export interface JsRuntimeModuleArg {
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_binding_api/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,8 @@ pub struct JsRuntimeModule {
pub module_identifier: String,
pub constructor_name: String,
pub name: String,
pub stage: u32,
pub isolate: bool,
}

#[napi(object, object_from_js = false)]
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_binding_api/src/plugins/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,8 @@ impl CompilationRuntimeModule for CompilationRuntimeModuleTap {
.as_str()
.cow_replace(compilation.runtime_template.runtime_module_prefix(), "")
.into_owned(),
stage: module.stage().into(),
isolate: module.should_isolate(),
},
chunk: ChunkWrapper::new(*chunk_ukey, compilation),
};
Expand Down
11 changes: 11 additions & 0 deletions crates/rspack_core/src/runtime_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ impl From<u32> for RuntimeModuleStage {
}
}

impl From<RuntimeModuleStage> for u32 {
fn from(value: RuntimeModuleStage) -> Self {
match value {
RuntimeModuleStage::Normal => 0,
RuntimeModuleStage::Basic => 5,
RuntimeModuleStage::Attach => 10,
RuntimeModuleStage::Trigger => 20,
}
}
}

pub trait RuntimeModuleExt {
fn boxed(self) -> Box<dyn RuntimeModule>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ export class HotUpdatePlugin {
compilation.hooks.runtimeModule.tap(
PLUGIN_NAME,
(module: any, _set: any) => {
if (module.constructorName === 'DefinePropertyGettersRuntimeModule') {
if (
module.constructor.name === 'DefinePropertyGettersRuntimeModule'
) {
module.source.source = Buffer.from(
`
${RuntimeGlobals.definePropertyGetters} = function (exports, definition) {
Expand Down
3 changes: 1 addition & 2 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import { JsRsdoctorChunkGraph } from '@rspack/binding';
import { JsRsdoctorModuleGraph } from '@rspack/binding';
import { JsRsdoctorModuleIdsPatch } from '@rspack/binding';
import { JsRsdoctorModuleSourcesPatch } from '@rspack/binding';
import type { JsRuntimeModule } from '@rspack/binding';
import type { JsStats } from '@rspack/binding';
import type { JsStatsCompilation } from '@rspack/binding';
import type { JsStatsError } from '@rspack/binding';
Expand Down Expand Up @@ -1026,7 +1025,7 @@ export class Compilation {
Set<string>
]>;
runtimeRequirementInTree: liteTapable.HookMap<liteTapable.SyncBailHook<[Chunk, Set<string>], void>>;
runtimeModule: liteTapable.SyncHook<[JsRuntimeModule, Chunk]>;
runtimeModule: liteTapable.SyncHook<[RuntimeModule, Chunk]>;
seal: liteTapable.SyncHook<[]>;
afterSeal: liteTapable.AsyncSeriesHook<[], void>;
needAdditionalPass: liteTapable.SyncBailHook<[], boolean>;
Expand Down
3 changes: 1 addition & 2 deletions packages/rspack/src/Compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import type {
ExternalObject,
JsCompilation,
JsPathData,
JsRuntimeModule,
JsSource,
} from '@rspack/binding';
import binding from '@rspack/binding';
Expand Down Expand Up @@ -252,7 +251,7 @@ export class Compilation {
runtimeRequirementInTree: liteTapable.HookMap<
liteTapable.SyncBailHook<[Chunk, Set<string>], void>
>;
runtimeModule: liteTapable.SyncHook<[JsRuntimeModule, Chunk]>;
runtimeModule: liteTapable.SyncHook<[RuntimeModule, Chunk]>;
seal: liteTapable.SyncHook<[]>;
afterSeal: liteTapable.AsyncSeriesHook<[], void>;
needAdditionalPass: liteTapable.SyncBailHook<[], boolean>;
Expand Down
55 changes: 54 additions & 1 deletion packages/rspack/src/RuntimeModule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { JsAddingRuntimeModule } from '@rspack/binding';
import type {
JsAddingRuntimeModule,
JsRuntimeModule,
JsSource,
} from '@rspack/binding';
import type { Chunk } from './Chunk';
import type { ChunkGraph } from './ChunkGraph';
import type { Compilation } from './Compilation';
Expand Down Expand Up @@ -71,3 +75,52 @@ export class RuntimeModule {
);
}
}

export function createRenderedRuntimeModule(
module: JsRuntimeModule,
): RuntimeModule {
const RuntimeModuleClass = {
[module.constructorName]: class extends RuntimeModule {
private _source: JsSource | undefined;
constructor() {
super(module.name, module.stage);
this._source = module.source;
}

/**
* @deprecated use `module.constructor.name` instead
*/
get constructorName() {
return module.constructorName;
}

/**
* @deprecated use `module.identifier()` instead
*/
get moduleIdentifier() {
return module.moduleIdentifier;
}

get source(): JsSource | undefined {
return this._source;
}

identifier(): string {
return module.moduleIdentifier;
}

readableIdentifier(): string {
return module.moduleIdentifier;
}

shouldIsolate(): boolean {
return module.isolate;
}

generate(): string {
return this._source?.source.toString('utf-8') || '';
}
},
}[module.constructorName];
return new RuntimeModuleClass();
Comment thread
LingyuCoder marked this conversation as resolved.
}
6 changes: 5 additions & 1 deletion packages/rspack/src/taps/compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
__to_binding_runtime_globals,
isReservedRuntimeGlobal,
} from '../RuntimeGlobals';
import { createRenderedRuntimeModule } from '../RuntimeModule';
import { createHash } from '../util/createHash';
import type { CreatePartialRegisters } from './types';

Expand Down Expand Up @@ -133,8 +134,11 @@ export const createCompilationHooksRegisters: CreatePartialRegisters<

function (queried) {
return function ({ module, chunk }: binding.JsRuntimeModuleArg) {
const runtimeModule = createRenderedRuntimeModule(module);
const compilation = getCompiler().__internal__get_compilation()!;
runtimeModule.attach(compilation, chunk, compilation.chunkGraph);
const originSource = module.source?.source;
queried.call(module, chunk);
queried.call(runtimeModule, chunk);
const newSource = module.source?.source;
if (newSource && newSource !== originSource) {
return module;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ class Plugin {
apply(compiler) {
compiler.hooks.compilation.tap("TestFakePlugin", compilation => {
compilation.hooks.runtimeModule.tap("TestFakePlugin", (module, chunk) => {
if (module.constructorName === "CssLoadingRuntimeModule") {
if (module.constructor.name === "CssLoadingRuntimeModule") {
expect(module.constructorName).toBe("CssLoadingRuntimeModule");
expect(module.moduleIdentifier).toBe("webpack/runtime/css loading");
expect(module.identifier()).toBe("webpack/runtime/css loading");
expect(module.readableIdentifier()).toBe("webpack/runtime/css loading");
const originSource = module.source.source.toString("utf-8");
module.source.source = Buffer.from(
`${originSource}\n__webpack_require__.f.miniCss.test = true;\n`,
Expand Down
9 changes: 5 additions & 4 deletions website/docs/en/types/runtime-module.mdx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<>

```ts
type RuntimeModule = {
class RuntimeModule {
source?: {
isRaw: boolean;
isBuffer: boolean;
source: Buffer;
map?: Buffer;
};
moduleIdentifier: string;
constructorName: string;
stage: number;
name: string;
};
identifier(): string;
readableIdentifier(): string;
}
```

Comment thread
LingyuCoder marked this conversation as resolved.
</>
9 changes: 5 additions & 4 deletions website/docs/zh/types/runtime-module.mdx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<>

```ts
type RuntimeModule = {
class RuntimeModule {
source?: {
isRaw: boolean;
isBuffer: boolean;
source: Buffer;
map?: Buffer;
};
moduleIdentifier: string;
constructorName: string;
stage: number;
name: string;
};
identifier(): string;
readableIdentifier(): string;
}
Comment thread
LingyuCoder marked this conversation as resolved.
```

</>
Loading