Skip to content

Commit 9778f4e

Browse files
committed
chore: Correct webpack issue with pretty-format's new export
1 parent 2cc80c1 commit 9778f4e

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

packages/case-connector/eslint.config.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import lintConfig from '@contract-case/eslint-config-case-maintainer';
22
import { globalIgnores } from 'eslint/config';
33

44
export default [
5-
globalIgnores(['cjs.js'], 'Ignore CJS node entry point'),
5+
globalIgnores(
6+
['cjs.js', 'rename-inner-webpack-vars-loader.cjs'],
7+
'Ignore CJS node entry point and webpack loader',
8+
),
69
...lintConfig,
710
{
811
rules: {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Webpack loader that renames internal webpack runtime variables in
3+
* pre-bundled (webpack-compiled) third-party packages.
4+
*
5+
* pretty-format v30 ships its CJS build as a webpack bundle with its own
6+
* __webpack_modules__, __webpack_require__, etc. When our outer webpack
7+
* processes this file, it replaces bare `require()` calls (e.g.
8+
* require("react-is")) with __webpack_require__(id). At runtime the inner
9+
* __webpack_require__ shadows the outer one, so the outer's module ID is
10+
* looked up in the inner's module map — which fails.
11+
*
12+
* By renaming the inner webpack variables, the shadowing is eliminated and
13+
* the outer webpack can properly resolve and bundle all dependencies.
14+
*/
15+
module.exports = function renameInnerWebpackVars(source) {
16+
return source
17+
.replaceAll('__webpack_modules__', '__nested_webpack_modules__')
18+
.replaceAll('__webpack_module_cache__', '__nested_webpack_module_cache__')
19+
.replaceAll('__webpack_require__', '__nested_webpack_require__')
20+
.replaceAll('__webpack_exports__', '__nested_webpack_exports__')
21+
.replaceAll('__unused_webpack_module', '__unused_nested_webpack_module');
22+
};

packages/case-connector/webpack.config.cjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ module.exports = {
2929
use: ['source-map-loader'],
3030
enforce: 'pre',
3131
},
32+
{
33+
// pretty-format ships a nested webpack bundle as its CJS build.
34+
// Rename its internal webpack variables so they don't shadow ours.
35+
test: /pretty-format[/\\]build[/\\]index\.js$/,
36+
enforce: 'pre',
37+
use: [path.join(__dirname, 'rename-inner-webpack-vars-loader.cjs')],
38+
},
3239
],
3340
},
3441
optimization: {

0 commit comments

Comments
 (0)