Skip to content

Commit 797a677

Browse files
committed
fix: user externals should have higher priority
1 parent 2046e00 commit 797a677

File tree

6 files changed

+56
-3
lines changed

6 files changed

+56
-3
lines changed

packages/core/src/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,14 +1391,14 @@ async function composeLibRsbuildConfig(
13911391
// #region Externals configs
13921392
// The order of the externals config should come in the following order:
13931393
// 1. `externalsWarnConfig` should come before other externals config to touch the externalized modules first.
1394-
// 2. The externals config in `bundlelessExternalConfig` should present after other externals config as
1394+
// 2. `userExternalsConfig` should present later to override the externals config of the ahead ones.
1395+
// 3. The externals config in `bundlelessExternalConfig` should present after other externals config as
13951396
// it relies on other externals config to bail out the externalized modules first then resolve
13961397
// the correct path for relative imports.
1397-
// 3. `userExternalsConfig` should present later to override the externals config of the ahead ones.
13981398
externalsWarnConfig,
1399+
userExternalsConfig,
13991400
autoExternalConfig,
14001401
targetExternalsConfig,
1401-
userExternalsConfig,
14021402
bundlelessExternalConfig,
14031403
// #endregion
14041404
entryConfig,

pnpm-lock.yaml

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

tests/integration/externals/index.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,20 @@ test('require ESM from CJS', async () => {
7474
const bazValue = await baz();
7575
expect(bazValue).toBe('baz');
7676
});
77+
78+
test('user externals', async () => {
79+
// Ensure the priority of user externals higher than others.
80+
// - memfs: userExternalsConfig > targetExternalsConfig
81+
// - lodash-es/zip: userExternalsConfig > autoExternalConfig
82+
const fixturePath = join(__dirname, 'user-externals');
83+
const { entries } = await buildAndGetResults({ fixturePath });
84+
expect(entries.esm).toMatchInlineSnapshot(
85+
`
86+
"import * as __WEBPACK_EXTERNAL_MODULE_memfs__ from "memfs";
87+
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
88+
import * as __WEBPACK_EXTERNAL_MODULE_lodash_es_zip_b8981481__ from "lodash-es/zip";
89+
console.log(__WEBPACK_EXTERNAL_MODULE_memfs__["default"], __WEBPACK_EXTERNAL_MODULE_lodash__["default"].add, __WEBPACK_EXTERNAL_MODULE_lodash_es_zip_b8981481__["default"]);
90+
"
91+
`,
92+
);
93+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "externals-user-externals-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"dependencies": {
7+
"lodash": "^4.17.21"
8+
},
9+
"devDependencies": {
10+
"@types/lodash": "^4.17.14"
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [generateBundleEsmConfig()],
6+
output: {
7+
externals: { 'lodash/zip': 'lodash-es/zip', 'node:fs': 'memfs' },
8+
},
9+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import fs from 'node:fs';
2+
import lodash from 'lodash';
3+
import zip from 'lodash/zip';
4+
5+
console.log(fs, lodash.add, zip);

0 commit comments

Comments
 (0)