Skip to content

Commit

Permalink
feat: add message comment for block promise (#6403)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Apr 29, 2024
1 parent 2b1012d commit d13137c
Show file tree
Hide file tree
Showing 35 changed files with 118 additions and 83 deletions.
1 change: 1 addition & 0 deletions crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ impl ContextModule {
Some(block_id),
&mut code_gen_result.runtime_requirements,
compilation,
"lazy-once context",
);
let map = self.get_user_request_map(dependencies, compilation);
let fake_map = self.get_fake_map(dependencies, compilation);
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/dependency/dependency_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl DependencyType {
DependencyType::EsmImportSpecifier => Cow::Borrowed("esm import specifier"),
DependencyType::EsmExportExpression => Cow::Borrowed("esm export expression"),
DependencyType::EsmExportHeader => Cow::Borrowed("esm export header"),
DependencyType::DynamicImport => Cow::Borrowed("dynamic import"),
DependencyType::DynamicImport => Cow::Borrowed("import()"),
DependencyType::CjsRequire => Cow::Borrowed("cjs require"),
DependencyType::CjsFullRequire => Cow::Borrowed("cjs full require"),
DependencyType::CjsExports => Cow::Borrowed("cjs exports"),
Expand Down
54 changes: 44 additions & 10 deletions crates/rspack_core/src/dependency/runtime_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ pub fn module_namespace_promise(
dep_id: &DependencyId,
block: Option<&AsyncDependenciesBlockIdentifier>,
request: &str,
_message: &str,
message: &str,
weak: bool,
) -> String {
let TemplateContext {
Expand All @@ -396,7 +396,7 @@ pub fn module_namespace_promise(
return missing_module_promise(request);
};

let promise = block_promise(block, runtime_requirements, compilation);
let promise = block_promise(block, runtime_requirements, compilation, message);
let exports_type = get_exports_type(
&compilation.get_module_graph(),
dep_id,
Expand Down Expand Up @@ -499,20 +499,54 @@ pub fn block_promise(
block: Option<&AsyncDependenciesBlockIdentifier>,
runtime_requirements: &mut RuntimeGlobals,
compilation: &Compilation,
message: &str,
) -> String {
let Some(block) = block else {
// ImportEagerDependency
return "Promise.resolve()".to_string();
let comment = comment(
&compilation.options,
CommentOptions {
request: None,
chunk_name: None,
message: Some(message),
},
);
return format!("Promise.resolve({comment})");
};
let chunk_group = compilation
.chunk_graph
.get_block_chunk_group(block, &compilation.chunk_group_by_ukey);
let Some(chunk_group) = chunk_group else {
return "Promise.resolve()".to_string();
let comment = comment(
&compilation.options,
CommentOptions {
request: None,
chunk_name: None,
message: Some(message),
},
);
return format!("Promise.resolve({comment})");
};
if chunk_group.chunks.is_empty() {
return "Promise.resolve()".to_string();
let comment = comment(
&compilation.options,
CommentOptions {
request: None,
chunk_name: None,
message: Some(message),
},
);
return format!("Promise.resolve({comment})");
}
let mg = compilation.get_module_graph();
let block = mg.block_by_id_expect(block);
let comment = comment(
&compilation.options,
CommentOptions {
request: None,
chunk_name: block.get_group_options().and_then(|o| o.name()),
message: Some(message),
},
);
let chunks = chunk_group
.chunks
.iter()
Expand All @@ -523,11 +557,11 @@ pub fn block_promise(
let chunk_id = serde_json::to_string(chunks[0].id.as_ref().expect("should have chunk.id"))
.expect("should able to json stringify");
runtime_requirements.insert(RuntimeGlobals::ENSURE_CHUNK);
format!("{}({chunk_id})", RuntimeGlobals::ENSURE_CHUNK)
format!("{}({comment}{chunk_id})", RuntimeGlobals::ENSURE_CHUNK)
} else if !chunks.is_empty() {
runtime_requirements.insert(RuntimeGlobals::ENSURE_CHUNK);
format!(
"Promise.all([{}])",
"Promise.all({comment}[{}])",
chunks
.iter()
.map(|c| format!(
Expand All @@ -540,7 +574,7 @@ pub fn block_promise(
.join(", ")
)
} else {
"Promise.resolve()".to_string()
format!("Promise.resolve({comment})")
}
}

Expand Down Expand Up @@ -635,7 +669,7 @@ pub fn async_module_factory(
.block_by_id(block_id)
.expect("should have block");
let dep = block.get_dependencies()[0];
let ensure_chunk = block_promise(Some(block_id), runtime_requirements, compilation);
let ensure_chunk = block_promise(Some(block_id), runtime_requirements, compilation, "");
let factory = returning_function(
&module_raw(compilation, runtime_requirements, &dep, request, false),
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ impl ExposeModuleMap {
.join(", "),
)
} else {
let block_promise = block_promise(Some(block_id), runtime_requirements, compilation);
let block_promise = block_promise(Some(block_id), runtime_requirements, compilation, "");
let module_raw = returning_function(
&returning_function(
&modules_iter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ Rspack x.x.x compiled with 1 error in X s (6a173f34164e1af948e4)"
exports[`StatsTestCases should print correct stats for filename 1`] = `
"PublicPath: auto
asset 521.xxxx.js 335 bytes [emitted]
asset 909.xxxx.js 8.72 KiB [emitted] (name: main)
Entrypoint main 8.72 KiB = 909.xxxx.js
asset 909.xxxx.js 8.73 KiB [emitted] (name: main)
Entrypoint main 8.73 KiB = 909.xxxx.js
runtime modules 11 modules
./index.js
./dynamic.js
Rspack x.x.x compiled successfully in X s (a605a17d8616567b9f8b)"
Rspack x.x.x compiled successfully in X s (185b80456c181897fcd5)"
`;
exports[`StatsTestCases should print correct stats for hot+production 1`] = `
Expand Down Expand Up @@ -125,19 +125,19 @@ Rspack compiled with 2 errors"
exports[`StatsTestCases should print correct stats for limit-chunk-count-plugin 1`] = `
"1 chunks:
PublicPath: auto
asset bundle1.js 3.45 KiB [emitted] (name: main)
Entrypoint main 3.45 KiB = bundle1.js
asset bundle1.js 3.5 KiB [emitted] (name: main)
Entrypoint main 3.5 KiB = bundle1.js
chunk bundle1.js (main) <{909}> >{909}< [entry]
./b.js
./c.js
./d.js
./e.js
./index.js
1 chunks (Rspack x.x.x) compiled successfully in X s (0c10f85791d2b49cd72e)
1 chunks (Rspack x.x.x) compiled successfully in X s (0d76b7dadd0c657ad341)
2 chunks:
PublicPath: auto
asset 76.bundle2.js 465 bytes [emitted] (name: c)
asset 76.bundle2.js 495 bytes [emitted] (name: c)
asset bundle2.js 10.1 KiB [emitted] (name: main)
Entrypoint main 10.1 KiB = bundle2.js
chunk 76.bundle2.js (c) <{76}> <{909}> >{76}<
Expand All @@ -147,12 +147,12 @@ exports[`StatsTestCases should print correct stats for limit-chunk-count-plugin
chunk bundle2.js (main) >{76}< [entry]
./b.js
./index.js
2 chunks (Rspack x.x.x) compiled successfully in X s (9fdb65a92ff09f5c7e0a)
2 chunks (Rspack x.x.x) compiled successfully in X s (a80080d4a166d3a145b2)
3 chunks:
PublicPath: auto
asset 345.bundle3.js 182 bytes [emitted]
asset 76.bundle3.js 359 bytes [emitted] (name: c)
asset 76.bundle3.js 389 bytes [emitted] (name: c)
asset bundle3.js 10.1 KiB [emitted] (name: main)
Entrypoint main 10.1 KiB = bundle3.js
chunk 345.bundle3.js <{76}>
Expand All @@ -163,13 +163,13 @@ exports[`StatsTestCases should print correct stats for limit-chunk-count-plugin
chunk bundle3.js (main) >{76}< [entry]
./b.js
./index.js
3 chunks (Rspack x.x.x) compiled successfully in X s (2528b9b2df61ea01f70c)
3 chunks (Rspack x.x.x) compiled successfully in X s (0e854af4043d4cced12b)
4 chunks:
PublicPath: auto
asset 697.bundle4.js 128 bytes [emitted]
asset 753.bundle4.js 128 bytes [emitted]
asset 76.bundle4.js 359 bytes [emitted] (name: c)
asset 76.bundle4.js 389 bytes [emitted] (name: c)
asset bundle4.js 10.1 KiB [emitted] (name: main)
Entrypoint main 10.1 KiB = bundle4.js
chunk 697.bundle4.js <{76}>
Expand All @@ -181,7 +181,7 @@ exports[`StatsTestCases should print correct stats for limit-chunk-count-plugin
chunk bundle4.js (main) >{76}< [entry]
./b.js
./index.js
4 chunks (Rspack x.x.x) compiled successfully in X s (2551584c072908e69932)"
4 chunks (Rspack x.x.x) compiled successfully in X s (4525b2f4f1626b91a6f4)"
`;
exports[`StatsTestCases should print correct stats for logging-loader 1`] = `
Expand All @@ -206,7 +206,7 @@ Rspack compiled with 1 error"
`;
exports[`StatsTestCases should print correct stats for named-chunk-group 1`] = `
"Entrypoint main 8.7 KiB = main.js
"Entrypoint main 8.72 KiB = main.js
Chunk Group cimanyd 335 bytes = cimanyd.js"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ console.log("123");
```js title=main.js
(self['webpackChunkwebpack'] = self['webpackChunkwebpack'] || []).push([["main"], {
"./index.js": (function (__unused_webpack_module, __unused_webpack_exports, __webpack_require__) {
__webpack_require__.e("normal").then(__webpack_require__.t.bind(__webpack_require__, "./normal.js", 23));
__webpack_require__.e("./sub/fold").then(__webpack_require__.t.bind(__webpack_require__, "./sub_fold.js", 23));
__webpack_require__.e("./sub/single").then(__webpack_require__.t.bind(__webpack_require__, "./single_quote.js", 23));
__webpack_require__.e("./sub/other").then(__webpack_require__.t.bind(__webpack_require__, "./other.js", 23));
__webpack_require__.e("./user/[id]").then(__webpack_require__.t.bind(__webpack_require__, "./user/1.js", 23));
__webpack_require__.e("user/[id]/page").then(__webpack_require__.t.bind(__webpack_require__, "./user/page/2.js", 23));
__webpack_require__.e("user/(id)/page").then(__webpack_require__.t.bind(__webpack_require__, "./user/page/3.js", 23));
__webpack_require__.e("bug_only_single_quote_js").then(__webpack_require__.t.bind(__webpack_require__, "./bug_only_single_quote.js", 23));
__webpack_require__.e(/* import() | normal */ "normal").then(__webpack_require__.t.bind(__webpack_require__, "./normal.js", 23));
__webpack_require__.e(/* import() | ./sub/fold */ "./sub/fold").then(__webpack_require__.t.bind(__webpack_require__, "./sub_fold.js", 23));
__webpack_require__.e(/* import() | ./sub/single */ "./sub/single").then(__webpack_require__.t.bind(__webpack_require__, "./single_quote.js", 23));
__webpack_require__.e(/* import() | ./sub/other */ "./sub/other").then(__webpack_require__.t.bind(__webpack_require__, "./other.js", 23));
__webpack_require__.e(/* import() | ./user/[id] */ "./user/[id]").then(__webpack_require__.t.bind(__webpack_require__, "./user/1.js", 23));
__webpack_require__.e(/* import() | user/[id]/page */ "user/[id]/page").then(__webpack_require__.t.bind(__webpack_require__, "./user/page/2.js", 23));
__webpack_require__.e(/* import() | user/(id)/page */ "user/(id)/page").then(__webpack_require__.t.bind(__webpack_require__, "./user/page/3.js", 23));
__webpack_require__.e(/* import() */ "bug_only_single_quote_js").then(__webpack_require__.t.bind(__webpack_require__, "./bug_only_single_quote.js", 23));
}),

},function(__webpack_require__) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(self['webpackChunkwebpack'] = self['webpackChunkwebpack'] || []).push([["main"], {
"./index.js": (function (__unused_webpack_module, __unused_webpack_exports, __webpack_require__) {
(async function() {
return __webpack_require__.e("wasm_wasm").then(__webpack_require__.bind(__webpack_require__, "./module.js")).then(function(mod) {
return __webpack_require__.e(/* import() */ "wasm_wasm").then(__webpack_require__.bind(__webpack_require__, "./module.js")).then(function(mod) {
if (mod.result !== 42) throw new Error("panic");
});
})();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ console.log("b");
(self['webpackChunkwebpack'] = self['webpackChunkwebpack'] || []).push([["main"], {
"./index.js": (function (__unused_webpack_module, __unused_webpack_exports, __webpack_require__) {
console.log("hello, world");
__webpack_require__.e("a_js").then(__webpack_require__.t.bind(__webpack_require__, "./a.js", 23));
__webpack_require__.e("b_js").then(__webpack_require__.t.bind(__webpack_require__, "./b.js", 23));
__webpack_require__.e(/* import() */ "a_js").then(__webpack_require__.t.bind(__webpack_require__, "./a.js", 23));
__webpack_require__.e(/* import() */ "b_js").then(__webpack_require__.t.bind(__webpack_require__, "./b.js", 23));
}),

},function(__webpack_require__) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ module.exports = webpackAsyncContext;
}),
"./index.js": (function (__unused_webpack_module, __unused_webpack_exports, __webpack_require__) {
const request = "a";
__webpack_require__.e("child_a_js").then(__webpack_require__.bind(__webpack_require__, "./child/a.js")).then(({ a })=>console.log("Literal", a));
__webpack_require__.e("child_b_js").then(__webpack_require__.bind(__webpack_require__, "./child/b.js")).then(({ b })=>console.log("Template Literal", b));
__webpack_require__.e(/* import() */ "child_a_js").then(__webpack_require__.bind(__webpack_require__, "./child/a.js")).then(({ a })=>console.log("Literal", a));
__webpack_require__.e(/* import() */ "child_b_js").then(__webpack_require__.bind(__webpack_require__, "./child/b.js")).then(({ b })=>console.log("Template Literal", b));
__webpack_require__("./child lazy recursive ^\\.\\/.*\\.js$")(`./${request}.js`).then(({ a })=>console.log("context_module_tpl", a));
__webpack_require__("./child lazy recursive ^\\.\\/.*\\.js$")("./" + request + ".js").then(({ a })=>console.log("context_module_bin", a));
__webpack_require__("./child lazy recursive ^\\.\\/.*\\.js$")("./".concat(request, ".js")).then(({ a })=>console.log("context_module_concat", a));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = "foo";
/* harmony import */var _foo_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./foo.js");
/* harmony import */var _foo_js__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_foo_js__WEBPACK_IMPORTED_MODULE_0__);

__webpack_require__.e("parent-1_js").then(__webpack_require__.t.bind(__webpack_require__, "./parent-1.js", 23));
__webpack_require__.e("parent-2_js").then(__webpack_require__.t.bind(__webpack_require__, "./parent-2.js", 23));
__webpack_require__.e(/* import() */ "parent-1_js").then(__webpack_require__.t.bind(__webpack_require__, "./parent-1.js", 23));
__webpack_require__.e(/* import() */ "parent-2_js").then(__webpack_require__.t.bind(__webpack_require__, "./parent-2.js", 23));
}),

},function(__webpack_require__) {
Expand All @@ -23,7 +23,7 @@ var __webpack_exports__ = (__webpack_exec__("./index.js"));
```js title=parent-1_js.js
(self['webpackChunkwebpack'] = self['webpackChunkwebpack'] || []).push([["parent-1_js"], {
"./parent-1.js": (function (__unused_webpack_module, __unused_webpack_exports, __webpack_require__) {
__webpack_require__.e("shared_js").then(__webpack_require__.bind(__webpack_require__, "./shared.js"));
__webpack_require__.e(/* import() */ "shared_js").then(__webpack_require__.bind(__webpack_require__, "./shared.js"));
}),

}]);
Expand All @@ -32,7 +32,7 @@ __webpack_require__.e("shared_js").then(__webpack_require__.bind(__webpack_requi
```js title=parent-2_js.js
(self['webpackChunkwebpack'] = self['webpackChunkwebpack'] || []).push([["parent-2_js"], {
"./parent-2.js": (function (__unused_webpack_module, __unused_webpack_exports, __webpack_require__) {
__webpack_require__.e("shared_js").then(__webpack_require__.bind(__webpack_require__, "./shared.js"));
__webpack_require__.e(/* import() */ "shared_js").then(__webpack_require__.bind(__webpack_require__, "./shared.js"));
}),

}]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ __webpack_require__.r(__webpack_exports__);
/* harmony import */var _shared__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./shared.js");
/* harmony import */var _shared__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_shared__WEBPACK_IMPORTED_MODULE_0__);

__webpack_require__.e("dynamic-2_js-_15a90").then(__webpack_require__.bind(__webpack_require__, "./dynamic-2.js"));
__webpack_require__.e(/* import() */ "dynamic-2_js-_15a90").then(__webpack_require__.bind(__webpack_require__, "./dynamic-2.js"));
console.log("dynamic-1");
}),

Expand All @@ -21,7 +21,7 @@ __webpack_require__.r(__webpack_exports__);
/* harmony import */var _shared__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./shared.js");
/* harmony import */var _shared__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_shared__WEBPACK_IMPORTED_MODULE_0__);

__webpack_require__.e("dynamic-2_js-_15a90").then(__webpack_require__.bind(__webpack_require__, "./dynamic-2.js"));
__webpack_require__.e(/* import() */ "dynamic-2_js-_15a90").then(__webpack_require__.bind(__webpack_require__, "./dynamic-2.js"));
console.log("dynamic-1");
}),
"./shared.js": (function () {
Expand All @@ -39,7 +39,7 @@ __webpack_require__.r(__webpack_exports__);
/* harmony import */var _shared__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./shared.js");
/* harmony import */var _shared__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_shared__WEBPACK_IMPORTED_MODULE_0__);

__webpack_require__.e("dynamic-1_js-_1d740").then(__webpack_require__.bind(__webpack_require__, "./dynamic-1.js"));
__webpack_require__.e(/* import() */ "dynamic-1_js-_1d740").then(__webpack_require__.bind(__webpack_require__, "./dynamic-1.js"));
console.log("dynamic-2");
}),

Expand All @@ -54,7 +54,7 @@ __webpack_require__.r(__webpack_exports__);
/* harmony import */var _shared__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./shared.js");
/* harmony import */var _shared__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_shared__WEBPACK_IMPORTED_MODULE_0__);

__webpack_require__.e("dynamic-1_js-_1d740").then(__webpack_require__.bind(__webpack_require__, "./dynamic-1.js"));
__webpack_require__.e(/* import() */ "dynamic-1_js-_1d740").then(__webpack_require__.bind(__webpack_require__, "./dynamic-1.js"));
console.log("dynamic-2");
}),
"./shared.js": (function () {
Expand All @@ -67,8 +67,8 @@ console.log("shared");
```js title=main.js
(self['webpackChunkwebpack'] = self['webpackChunkwebpack'] || []).push([["main"], {
"./index.js": (function (__unused_webpack_module, __unused_webpack_exports, __webpack_require__) {
__webpack_require__.e("dynamic-1_js-_1d741").then(__webpack_require__.bind(__webpack_require__, "./dynamic-1.js"));
__webpack_require__.e("dynamic-2_js-_15a91").then(__webpack_require__.bind(__webpack_require__, "./dynamic-2.js"));
__webpack_require__.e(/* import() */ "dynamic-1_js-_1d741").then(__webpack_require__.bind(__webpack_require__, "./dynamic-1.js"));
__webpack_require__.e(/* import() */ "dynamic-2_js-_15a91").then(__webpack_require__.bind(__webpack_require__, "./dynamic-2.js"));
console.log("index");
}),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
__webpack_require__.r(__webpack_exports__);
/* harmony import */var _shared__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./shared.js");
/* harmony import */var _shared__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_shared__WEBPACK_IMPORTED_MODULE_0__);
Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "./index.js"));
Promise.resolve(/* import() */ ).then(__webpack_require__.bind(__webpack_require__, "./index.js"));

console.log("index1");
}),
Expand All @@ -27,7 +27,7 @@ var __webpack_exports__ = (__webpack_exec__("./index.js"));
"use strict";
/* harmony import */var _shared__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./shared.js");
/* harmony import */var _shared__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_shared__WEBPACK_IMPORTED_MODULE_0__);
__webpack_require__.e("index_js").then(__webpack_require__.bind(__webpack_require__, "./index.js"));
__webpack_require__.e(/* import() */ "index_js").then(__webpack_require__.bind(__webpack_require__, "./index.js"));

console.log("index2");
}),
Expand All @@ -50,7 +50,7 @@ var __webpack_exports__ = (__webpack_exec__("./index2.js"));
__webpack_require__.r(__webpack_exports__);
/* harmony import */var _shared__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./shared.js");
/* harmony import */var _shared__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_shared__WEBPACK_IMPORTED_MODULE_0__);
Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "./index.js"));
Promise.resolve(/* import() */ ).then(__webpack_require__.bind(__webpack_require__, "./index.js"));

console.log("index1");
}),
Expand Down
Loading

2 comments on commit d13137c

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

task failure

Please sign in to comment.