Skip to content

Commit 5867fd1

Browse files
authored
fix: user externals should have higher priority (#653)
1 parent 9dc4a48 commit 5867fd1

File tree

7 files changed

+92
-4
lines changed

7 files changed

+92
-4
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 at first to takes effect earlier than others.
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: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { join } from 'node:path';
22
import stripAnsi from 'strip-ansi';
3-
import { buildAndGetResults, proxyConsole } from 'test-helper';
3+
import { buildAndGetResults, proxyConsole, queryContent } from 'test-helper';
44
import { expect, test } from 'vitest';
55
import { composeModuleImportWarn } from '../../../packages/core/src/config';
66

@@ -74,3 +74,34 @@ 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+
// - "./foo2": userExternalsConfig > bundlelessExternalConfig
83+
84+
const fixturePath = join(__dirname, 'user-externals');
85+
const { entries, contents } = await buildAndGetResults({ fixturePath });
86+
expect(entries.esm0).toMatchInlineSnapshot(
87+
`
88+
"import * as __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__ from "node:fs";
89+
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
90+
import * as __WEBPACK_EXTERNAL_MODULE_lodash_zip_41bf8b9e__ from "lodash/zip";
91+
const foo = 'foo';
92+
console.log(__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"], __WEBPACK_EXTERNAL_MODULE_lodash__["default"].add, __WEBPACK_EXTERNAL_MODULE_lodash_zip_41bf8b9e__["default"], foo);
93+
"
94+
`,
95+
);
96+
97+
expect(
98+
queryContent(contents.esm1!, 'index.js', { basename: true }).content,
99+
).toMatchInlineSnapshot(`
100+
"import * as __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__ from "node:fs";
101+
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
102+
import * as __WEBPACK_EXTERNAL_MODULE_lodash_zip_41bf8b9e__ from "lodash/zip";
103+
import * as __WEBPACK_EXTERNAL_MODULE__foo2_1d132755__ from "./foo2";
104+
console.log(__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"], __WEBPACK_EXTERNAL_MODULE_lodash__["default"].add, __WEBPACK_EXTERNAL_MODULE_lodash_zip_41bf8b9e__["default"], __WEBPACK_EXTERNAL_MODULE__foo2_1d132755__.foo);
105+
"
106+
`);
107+
});
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleEsmConfig } from 'test-helper';
3+
4+
const baseExternals = {
5+
externals: { 'lodash/zip': 'lodash-es/zip', 'node:fs': 'memfs' },
6+
};
7+
8+
export default defineConfig({
9+
lib: [
10+
generateBundleEsmConfig({
11+
output: {
12+
externals: baseExternals,
13+
distPath: {
14+
root: 'dist/bundle',
15+
},
16+
},
17+
}),
18+
generateBundleEsmConfig({
19+
bundle: false,
20+
output: {
21+
externals: { ...baseExternals, './foo': './foo2' },
22+
distPath: {
23+
root: 'dist/bundle-false',
24+
},
25+
},
26+
}),
27+
],
28+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'foo';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import fs from 'node:fs';
2+
import lodash from 'lodash';
3+
import zip from 'lodash/zip';
4+
import { foo } from './foo';
5+
6+
console.log(fs, lodash.add, zip, foo);

0 commit comments

Comments
 (0)