Skip to content

Commit 1a12cac

Browse files
committed
fix(esm): add missing CJS interop runtime requirement
1 parent a7c0970 commit 1a12cac

6 files changed

Lines changed: 230 additions & 1 deletion

File tree

crates/rspack_plugin_esm_library/src/plugin.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,19 @@ async fn additional_module_runtime_requirements(
409409
async fn additional_chunk_runtime_requirements(
410410
&self,
411411
_compilation: &Compilation,
412-
_chunk_ukey: &ChunkUkey,
412+
chunk_ukey: &ChunkUkey,
413413
runtime_requirements: &mut RuntimeGlobals,
414414
_runtime_modules: &mut Vec<Box<dyn RuntimeModule>>,
415415
) -> Result<()> {
416+
if let Some(chunk_link) = self.links.borrow().get(chunk_ukey)
417+
&& chunk_link
418+
.required
419+
.values()
420+
.any(|interop| interop.default_access.is_some())
421+
{
422+
runtime_requirements.insert(RuntimeGlobals::COMPAT_GET_DEFAULT_EXPORT);
423+
}
424+
416425
// Add REQUIRE_SCOPE only when runtime_requirements actually contain globals
417426
// that live on the __rspack_require object (same check the runtime plugin
418427
// uses in handle_scope_globals). This avoids pulling in an empty
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
```mjs title=foo.mjs
2+
import { __webpack_require__ } from "./runtime.mjs";
3+
4+
__webpack_require__.add({
5+
"./foo.cjs"
6+
/*!*****************!*\
7+
!*** ./foo.cjs ***!
8+
\*****************/
9+
(module) {
10+
module.exports = 42;
11+
12+
13+
},
14+
});
15+
__webpack_require__("./foo.cjs");
16+
17+
export {};
18+
19+
```
20+
21+
```mjs title=main.mjs
22+
import { __webpack_require__ } from "./runtime.mjs";
23+
import "./foo.mjs";
24+
25+
// ./index.js
26+
27+
28+
it('should re-export a CommonJS default from another entry', async () => {
29+
const { value } = await import(
30+
/* webpackIgnore: true */ './main.mjs'
31+
);
32+
expect(value).toBe(42);
33+
});
34+
35+
const foo = __webpack_require__("./foo.cjs");
36+
var foo_default = /*#__PURE__*/__webpack_require__.n(foo);
37+
var foo_default_0 = foo_default();
38+
39+
export { foo_default_0 as value };
40+
41+
```
42+
43+
```mjs title=runtime.mjs
44+
45+
var __webpack_modules__ = {};
46+
// The module cache
47+
var __webpack_module_cache__ = {};
48+
// The require function
49+
function __webpack_require__(moduleId) {
50+
// Check if module is in cache
51+
var cachedModule = __webpack_module_cache__[moduleId];
52+
if (cachedModule !== undefined) {
53+
return cachedModule.exports;
54+
}
55+
// Create a new module (and put it into the cache)
56+
var module = (__webpack_module_cache__[moduleId] = {
57+
exports: {}
58+
});
59+
// Execute the module function
60+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
61+
62+
// Return the exports of the module
63+
return module.exports;
64+
}
65+
// expose the modules object (__webpack_modules__)
66+
__webpack_require__.m = __webpack_modules__;
67+
68+
// webpack/runtime/compat_get_default_export
69+
(() => {
70+
// getDefaultExport function for compatibility with non-ESM modules
71+
__webpack_require__.n = (module) => {
72+
var getter = module && module.__esModule ?
73+
() => (module['default']) :
74+
() => (module);
75+
__webpack_require__.d(getter, { a: getter });
76+
return getter;
77+
};
78+
79+
})();
80+
// webpack/runtime/define_property_getters
81+
(() => {
82+
__webpack_require__.d = (exports, getters, values) => {
83+
var define = (defs, kind) => {
84+
for(var key in defs) {
85+
if(__webpack_require__.o(defs, key) && !__webpack_require__.o(exports, key)) {
86+
Object.defineProperty(exports, key, { enumerable: true, [kind]: defs[key] });
87+
}
88+
}
89+
};
90+
define(getters, "get");
91+
define(values, "value");
92+
};
93+
})();
94+
// webpack/runtime/esm_register_module
95+
(() => {
96+
__webpack_require__.add = function registerModules(modules) { Object.assign(__webpack_require__.m, modules) }
97+
98+
})();
99+
// webpack/runtime/has_own_property
100+
(() => {
101+
__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
102+
})();
103+
104+
export { __webpack_require__ };
105+
106+
```
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
```mjs title=foo.mjs
2+
import { rspackRequire, moduleFactories, compatGetDefaultExport } from "./runtime.mjs";
3+
moduleFactories.add({
4+
"./foo.cjs"
5+
/*!*****************!*\
6+
!*** ./foo.cjs ***!
7+
\*****************/
8+
(module) {
9+
module.exports = 42;
10+
11+
12+
},
13+
});
14+
rspackRequire("./foo.cjs");
15+
16+
export {};
17+
18+
```
19+
20+
```mjs title=main.mjs
21+
import { rspackRequire, compatGetDefaultExport } from "./runtime.mjs";
22+
import "./foo.mjs";
23+
24+
// ./index.js
25+
26+
27+
it('should re-export a CommonJS default from another entry', async () => {
28+
const { value } = await import(
29+
/* webpackIgnore: true */ './main.mjs'
30+
);
31+
expect(value).toBe(42);
32+
});
33+
34+
const foo = rspackRequire("./foo.cjs");
35+
var foo_default = /*#__PURE__*/compatGetDefaultExport(foo);
36+
var foo_default_0 = foo_default();
37+
38+
export { foo_default_0 as value };
39+
40+
```
41+
42+
```mjs title=runtime.mjs
43+
44+
var modules = {};
45+
// The module cache
46+
var moduleCache = {};
47+
// The require function
48+
function rspackRequire(moduleId) {
49+
// Check if module is in cache
50+
var cachedModule = moduleCache[moduleId];
51+
if (cachedModule !== undefined) {
52+
return cachedModule.exports;
53+
}
54+
// Create a new module (and put it into the cache)
55+
var module = (moduleCache[moduleId] = {
56+
exports: {}
57+
});
58+
// Execute the module function
59+
modules[moduleId](module, module.exports, rspackRequire);
60+
61+
// Return the exports of the module
62+
return module.exports;
63+
}
64+
export { rspackRequire };
65+
// expose the modules object (modules)
66+
var moduleFactories = modules;
67+
export { moduleFactories };
68+
69+
// rspack/runtime/compat_get_default_export
70+
// getDefaultExport function for compatibility with non-ESM modules
71+
var compatGetDefaultExport = (module) => {
72+
var getter = module && module.__esModule ?
73+
() => (module['default']) :
74+
() => (module);
75+
definePropertyGetters(getter, { a: getter });
76+
return getter;
77+
};
78+
;
79+
export { compatGetDefaultExport };
80+
// rspack/runtime/define_property_getters
81+
var definePropertyGetters = (exports, getters, values) => {
82+
var define = (defs, kind) => {
83+
for(var key in defs) {
84+
if(hasOwnProperty(defs, key) && !hasOwnProperty(exports, key)) {
85+
Object.defineProperty(exports, key, { enumerable: true, [kind]: defs[key] });
86+
}
87+
}
88+
};
89+
define(getters, "get");
90+
define(values, "value");
91+
};;
92+
// rspack/runtime/esm_register_module
93+
moduleFactories.add = function registerModules(modules) { Object.assign(moduleFactories, modules) }
94+
;
95+
// rspack/runtime/has_own_property
96+
var hasOwnProperty = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop));
97+
98+
99+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 42;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export { default as value } from './foo.cjs';
2+
3+
it('should re-export a CommonJS default from another entry', async () => {
4+
const { value } = await import(
5+
/* webpackIgnore: true */ './main.mjs'
6+
);
7+
expect(value).toBe(42);
8+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
entry: {
3+
main: './index.js',
4+
foo: './foo.cjs',
5+
},
6+
};

0 commit comments

Comments
 (0)