Skip to content

Commit 400dfe2

Browse files
committed
fix: correct detect transformed module external request
1 parent 88b41b1 commit 400dfe2

File tree

6 files changed

+95
-17
lines changed

6 files changed

+95
-17
lines changed

packages/core/src/config.ts

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,39 @@ export async function loadConfig({
141141
return { content: content as RslibConfig, filePath: configFilePath };
142142
}
143143

144+
// Match logic is derived from https://github.com/webpack/webpack/blob/94aba382eccf3de1004d235045d4462918dfdbb7/lib/ExternalModuleFactoryPlugin.js#L89-L158
145+
const handleMatchedExternal = (
146+
value: string | string[] | boolean | Record<string, string | string[]>,
147+
request: string,
148+
): boolean => {
149+
if (typeof value === 'boolean') {
150+
return value;
151+
}
152+
153+
if (typeof value === 'string') {
154+
const [first, second] = value.split(' ');
155+
const hasType = !!second;
156+
const _request = second ? second : first;
157+
158+
// Don't need to warn explicit declared external type.
159+
if (!hasType) {
160+
return request === _request;
161+
}
162+
163+
return false;
164+
}
165+
166+
if (Array.isArray(value)) {
167+
return handleMatchedExternal(value[0] ?? '', request);
168+
}
169+
170+
if (typeof value === 'object') {
171+
return false;
172+
}
173+
174+
return false;
175+
};
176+
144177
const composeExternalsWarnConfig = (
145178
format: Format,
146179
...externalsArray: NonNullable<EnvironmentConfig['output']>['externals'][]
@@ -165,12 +198,15 @@ const composeExternalsWarnConfig = (
165198
request: string,
166199
callback: (matched?: true) => void,
167200
) => {
201+
// string
168202
if (typeof externals === 'string') {
169-
if (externals === request) {
203+
if (handleMatchedExternal(externals, request)) {
170204
callback(true);
171205
return;
172206
}
173-
} else if (Array.isArray(externals)) {
207+
}
208+
// array
209+
if (Array.isArray(externals)) {
174210
let i = 0;
175211
const next = () => {
176212
let asyncFlag: boolean;
@@ -202,16 +238,24 @@ const composeExternalsWarnConfig = (
202238

203239
next();
204240
return;
205-
} else if (externals instanceof RegExp) {
241+
}
242+
// regexp
243+
if (externals instanceof RegExp) {
206244
if (externals.test(request)) {
207245
callback(true);
208246
return;
209247
}
210-
} else if (typeof externals === 'function') {
248+
}
249+
// function
250+
else if (typeof externals === 'function') {
211251
// TODO: Support function
212-
} else if (typeof externals === 'object') {
252+
}
253+
// object
254+
else if (typeof externals === 'object') {
213255
if (Object.prototype.hasOwnProperty.call(externals, request)) {
214-
callback(true);
256+
if (handleMatchedExternal(externals[request]!, request)) {
257+
callback(true);
258+
}
215259
return;
216260
}
217261
}
@@ -1449,8 +1493,8 @@ async function composeLibRsbuildConfig(
14491493
const dtsConfig = await composeDtsConfig(config, dtsExtension);
14501494
const externalsWarnConfig = composeExternalsWarnConfig(
14511495
format!,
1452-
autoExternalConfig?.output?.externals,
14531496
userExternalsConfig?.output?.externals,
1497+
autoExternalConfig?.output?.externals,
14541498
);
14551499
const minifyConfig = composeMinifyConfig(config);
14561500
const bannerFooterConfig = composeBannerFooterConfig(banner, footer);

pnpm-lock.yaml

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

tests/integration/auto-external/index.test.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,21 @@ test('should get warn when use require in ESM', async () => {
8989
const { entries } = await buildAndGetResults({ fixturePath });
9090
const logStrings = logs.map((log) => stripAnsi(log));
9191

92-
expect(entries.esm).toContain(
93-
'import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react"',
94-
);
95-
96-
expect(
97-
logStrings.some((l) =>
98-
l.includes(stripAnsi(composeModuleImportWarn('react'))),
99-
),
100-
).toBe(true);
92+
const moduleExternalized = ['react', 'e2', 'e3', 'e5', 'e6', 'e7'];
93+
94+
for (const item of moduleExternalized) {
95+
expect(entries.esm).toContain(
96+
`import * as __WEBPACK_EXTERNAL_MODULE_${item}__ from "${item}"`,
97+
);
98+
}
99+
100+
for (const item of moduleExternalized) {
101+
expect(
102+
logStrings.some((l) =>
103+
l.includes(stripAnsi(composeModuleImportWarn(item))),
104+
),
105+
).toBe(true);
106+
}
101107

102108
restore();
103109
});

tests/integration/auto-external/module-import-warn/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "module-import-warn",
33
"private": true,
44
"dependencies": {
5+
"lodash": "^4.17.21",
56
"react": "^19.0.0"
67
}
78
}

tests/integration/auto-external/module-import-warn/rslib.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,19 @@ export default defineConfig({
88
index: './src/index.ts',
99
},
1010
},
11+
output: {
12+
externals: [
13+
{
14+
e1: 'commonjs e1',
15+
e2: 'e2',
16+
e3: true,
17+
e4: ['commonjs e4'],
18+
e5: ['e5'],
19+
'lodash/add': false,
20+
e8: ['module e8'],
21+
},
22+
/e6/,
23+
'e7',
24+
],
25+
},
1126
});
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
export const foo = () => {
22
const React = require('react');
3-
return React.version;
3+
const e1 = require('e1');
4+
const e2 = require('e2');
5+
const e3 = require('e3');
6+
const e4 = require('e4');
7+
const e5 = require('e5');
8+
const e6 = require('e6');
9+
const e7 = require('e7');
10+
const e8 = require('e8');
11+
const add = require('lodash/add');
12+
return React.version + e1 + e2 + e3 + e4 + e5 + e6 + e7 + e8 + add;
413
};

0 commit comments

Comments
 (0)