Skip to content

Commit 6db19fe

Browse files
authored
fix: should not panic if ref-cjs is removed (#12147)
1 parent f70f870 commit 6db19fe

5 files changed

Lines changed: 101 additions & 1 deletion

File tree

crates/rspack_plugin_esm_library/src/link.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,14 @@ impl EsmLibraryPlugin {
16421642
continue;
16431643
}
16441644

1645+
if compilation
1646+
.chunk_graph
1647+
.get_module_chunks(*module_id)
1648+
.is_empty()
1649+
{
1650+
continue;
1651+
}
1652+
16451653
let Some(module) = module_graph
16461654
.module_by_identifier(module_id)
16471655
.expect("should have module")
@@ -1878,6 +1886,7 @@ impl EsmLibraryPlugin {
18781886

18791887
let outgoing_module_info = &concate_modules_map[conn.module_identifier()];
18801888
let ref_module = *conn.module_identifier();
1889+
18811890
//ensure chunk
18821891
chunk_imports.entry(ref_module).or_default();
18831892

@@ -2089,9 +2098,19 @@ impl EsmLibraryPlugin {
20892098
.module_by_identifier(m)
20902099
.expect("should have module");
20912100
for dep_id in module.get_dependencies() {
2092-
let Some(ref_module) = module_graph.module_identifier_by_dependency_id(dep_id) else {
2101+
let Some(conn) = module_graph.connection_by_dependency_id(dep_id) else {
20932102
continue;
20942103
};
2104+
2105+
if !conn.is_target_active(
2106+
&module_graph,
2107+
None,
2108+
&compilation.module_graph_cache_artifact,
2109+
) {
2110+
continue;
2111+
}
2112+
2113+
let ref_module = conn.module_identifier();
20952114
chunk_imports.entry(*ref_module).or_default();
20962115
}
20972116
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
```mjs title=main.mjs
2+
import { __webpack_require__ } from "./runtime.mjs";
3+
__webpack_require__.add({
4+
"./index.js":
5+
/*!******************!*\
6+
!*** ./index.js ***!
7+
\******************/
8+
(function (module, __unused_webpack___webpack_exports__, __webpack_require__) {
9+
/* module decorator */ module = __webpack_require__.hmd(module);
10+
11+
12+
// access module so entry is not scope hoisted
13+
console.log.bind(module)
14+
15+
16+
}),
17+
});
18+
const index = __webpack_require__("./index.js");
19+
20+
21+
```
22+
23+
```mjs title=runtime.mjs
24+
25+
var __webpack_modules__ = {};
26+
// The module cache
27+
var __webpack_module_cache__ = {};
28+
// The require function
29+
function __webpack_require__(moduleId) {
30+
// Check if module is in cache
31+
var cachedModule = __webpack_module_cache__[moduleId];
32+
if (cachedModule !== undefined) {
33+
return cachedModule.exports;
34+
}
35+
// Create a new module (and put it into the cache)
36+
var module = (__webpack_module_cache__[moduleId] = {
37+
id: moduleId,
38+
loaded: false,
39+
exports: {}
40+
});
41+
// Execute the module function
42+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
43+
44+
// Flag the module as loaded
45+
module.loaded = true;
46+
// Return the exports of the module
47+
return module.exports;
48+
}
49+
// expose the modules object (__webpack_modules__)
50+
__webpack_require__.m = __webpack_modules__;
51+
52+
// esm library register module runtime
53+
(() => {
54+
__webpack_require__.add = function registerModules(modules) { Object.assign(__webpack_require__.m, modules) }
55+
56+
})();
57+
// webpack/runtime/esm_module_decorator
58+
(() => {
59+
__webpack_require__.hmd = (module) => {
60+
module = Object.create(module);
61+
if (!module.children) module.children = [];
62+
Object.defineProperty(module, 'exports', {
63+
enumerable: true,
64+
set: () => {
65+
throw new Error('ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: ' + module.id);
66+
}
67+
});
68+
return module;
69+
};
70+
})();
71+
72+
export { __webpack_require__ };
73+
74+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import 'side-effect-free'
2+
3+
// access module so entry is not scope hoisted
4+
console.log.bind(module)

tests/rspack-test/esmOutputCases/basic/tree-shaking/node_modules/side-effect-free/index.js

Whitespace-only changes.

tests/rspack-test/esmOutputCases/basic/tree-shaking/node_modules/side-effect-free/package.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)