Skip to content

Commit 2486b2a

Browse files
authored
fix: returning exports from runtime for libraries (#2415)
1 parent 98165eb commit 2486b2a

File tree

7 files changed

+55
-3
lines changed

7 files changed

+55
-3
lines changed

.changeset/brown-countries-attack.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rspack/core": patch
3+
---
4+
5+
fix: returning exports from runtime for libraries

crates/rspack_core/src/runtime_globals.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,8 @@ pub const GET_FULL_HASH: &str = "__webpack_require__.h";
147147
* the global object
148148
*/
149149
pub const GLOBAL: &str = "__webpack_require__.g";
150+
151+
/**
152+
* runtime need to return the exports of the last entry module
153+
*/
154+
pub const RETURN_EXPORTS_FROM_RUNTIME: &str = "return-exports-from-runtime";

crates/rspack_plugin_javascript/src/plugin.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ impl JsPlugin {
160160
pub async fn render_main(&self, args: &rspack_core::RenderManifestArgs<'_>) -> Result<BoxSource> {
161161
let compilation = args.compilation;
162162
let chunk = args.chunk();
163+
let runtime_requirements = compilation
164+
.chunk_graph
165+
.get_tree_runtime_requirements(&args.chunk_ukey);
163166
let mut sources = ConcatSource::default();
164167
sources.add(RawSource::from("var __webpack_modules__ = "));
165168
sources.add(render_chunk_modules(compilation, &args.chunk_ukey)?);
@@ -169,6 +172,9 @@ impl JsPlugin {
169172
if chunk.has_entry_module(&compilation.chunk_graph) {
170173
// TODO: how do we handle multiple entry modules?
171174
sources.add(generate_chunk_entry_code(compilation, &args.chunk_ukey));
175+
if runtime_requirements.contains(runtime_globals::RETURN_EXPORTS_FROM_RUNTIME) {
176+
sources.add(RawSource::from("return __webpack_exports__;\n"));
177+
}
172178
if let Some(source) =
173179
compilation
174180
.plugin_driver

crates/rspack_plugin_library/src/amd_library_plugin.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rspack_core::{
22
rspack_sources::{ConcatSource, RawSource, SourceExt},
3-
ExternalModule, Filename, LibraryName, LibraryOptions, Plugin, PluginContext,
3+
runtime_globals, AdditionalChunkRuntimeRequirementsArgs, ExternalModule, Filename, LibraryName,
4+
LibraryOptions, Plugin, PluginAdditionalChunkRuntimeRequirementsOutput, PluginContext,
45
PluginRenderHookOutput, RenderArgs, SourceType,
56
};
67
use rspack_error::Result;
@@ -39,6 +40,17 @@ impl Plugin for AmdLibraryPlugin {
3940
"AmdLibraryPlugin"
4041
}
4142

43+
fn additional_chunk_runtime_requirements(
44+
&self,
45+
_ctx: PluginContext,
46+
args: &mut AdditionalChunkRuntimeRequirementsArgs,
47+
) -> PluginAdditionalChunkRuntimeRequirementsOutput {
48+
args
49+
.runtime_requirements
50+
.insert(runtime_globals::RETURN_EXPORTS_FROM_RUNTIME);
51+
Ok(())
52+
}
53+
4254
fn render(&self, _ctx: PluginContext, args: &RenderArgs) -> PluginRenderHookOutput {
4355
let compilation = &args.compilation;
4456
let chunk = args.chunk();

crates/rspack_plugin_library/src/umd_library_plugin.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rspack_core::{
22
rspack_sources::{ConcatSource, RawSource, SourceExt},
3-
Chunk, ExternalModule, Filename, LibraryAuxiliaryComment, Plugin, PluginContext,
3+
runtime_globals, AdditionalChunkRuntimeRequirementsArgs, Chunk, ExternalModule, Filename,
4+
LibraryAuxiliaryComment, Plugin, PluginAdditionalChunkRuntimeRequirementsOutput, PluginContext,
45
PluginRenderHookOutput, RenderArgs, SourceType,
56
};
67

@@ -24,6 +25,17 @@ impl Plugin for UmdLibraryPlugin {
2425
"UmdLibraryPlugin"
2526
}
2627

28+
fn additional_chunk_runtime_requirements(
29+
&self,
30+
_ctx: PluginContext,
31+
args: &mut AdditionalChunkRuntimeRequirementsArgs,
32+
) -> PluginAdditionalChunkRuntimeRequirementsOutput {
33+
args
34+
.runtime_requirements
35+
.insert(runtime_globals::RETURN_EXPORTS_FROM_RUNTIME);
36+
Ok(())
37+
}
38+
2739
fn render(&self, _ctx: PluginContext, args: &RenderArgs) -> PluginRenderHookOutput {
2840
let compilation = &args.compilation;
2941
let chunk = args.chunk();

crates/rspack_plugin_runtime/src/array_push_callback_chunk_format.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ impl Plugin for ArrayPushCallbackChunkFormatPlugin {
103103
}
104104
if has_entry {
105105
source.add(generate_chunk_entry_code(args.compilation, args.chunk_ukey));
106+
let runtime_requirements = args
107+
.compilation
108+
.chunk_graph
109+
.get_tree_runtime_requirements(args.chunk_ukey);
110+
if runtime_requirements.contains(runtime_globals::RETURN_EXPORTS_FROM_RUNTIME) {
111+
source.add(RawSource::from("return __webpack_exports__;\n"));
112+
}
106113
if let Some(s) =
107114
args
108115
.compilation
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
it("should run", function () {});
1+
it("should run", function () {
2+
var fs = require("fs");
3+
var source = fs.readFileSync(__filename, "utf-8");
4+
5+
expect(source.includes("return __webpack_exports__")).toBe(true);
6+
});

0 commit comments

Comments
 (0)