Skip to content

Commit

Permalink
fix: user externals should have higher priority (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework authored Jan 7, 2025
1 parent 9dc4a48 commit 5867fd1
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1391,14 +1391,14 @@ async function composeLibRsbuildConfig(
// #region Externals configs
// The order of the externals config should come in the following order:
// 1. `externalsWarnConfig` should come before other externals config to touch the externalized modules first.
// 2. The externals config in `bundlelessExternalConfig` should present after other externals config as
// 2. `userExternalsConfig` should present at first to takes effect earlier than others.
// 3. The externals config in `bundlelessExternalConfig` should present after other externals config as
// it relies on other externals config to bail out the externalized modules first then resolve
// the correct path for relative imports.
// 3. `userExternalsConfig` should present later to override the externals config of the ahead ones.
externalsWarnConfig,
userExternalsConfig,
autoExternalConfig,
targetExternalsConfig,
userExternalsConfig,
bundlelessExternalConfig,
// #endregion
entryConfig,
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 32 additions & 1 deletion tests/integration/externals/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'node:path';
import stripAnsi from 'strip-ansi';
import { buildAndGetResults, proxyConsole } from 'test-helper';
import { buildAndGetResults, proxyConsole, queryContent } from 'test-helper';
import { expect, test } from 'vitest';
import { composeModuleImportWarn } from '../../../packages/core/src/config';

Expand Down Expand Up @@ -74,3 +74,34 @@ test('require ESM from CJS', async () => {
const bazValue = await baz();
expect(bazValue).toBe('baz');
});

test('user externals', async () => {
// Ensure the priority of user externals higher than others.
// - "memfs": userExternalsConfig > targetExternalsConfig
// - "lodash-es/zip": userExternalsConfig > autoExternalConfig
// - "./foo2": userExternalsConfig > bundlelessExternalConfig

const fixturePath = join(__dirname, 'user-externals');
const { entries, contents } = await buildAndGetResults({ fixturePath });
expect(entries.esm0).toMatchInlineSnapshot(
`
"import * as __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__ from "node:fs";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_lodash_zip_41bf8b9e__ from "lodash/zip";
const foo = 'foo';
console.log(__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"], __WEBPACK_EXTERNAL_MODULE_lodash__["default"].add, __WEBPACK_EXTERNAL_MODULE_lodash_zip_41bf8b9e__["default"], foo);
"
`,
);

expect(
queryContent(contents.esm1!, 'index.js', { basename: true }).content,
).toMatchInlineSnapshot(`
"import * as __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__ from "node:fs";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_lodash_zip_41bf8b9e__ from "lodash/zip";
import * as __WEBPACK_EXTERNAL_MODULE__foo2_1d132755__ from "./foo2";
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);
"
`);
});
12 changes: 12 additions & 0 deletions tests/integration/externals/user-externals/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "externals-user-externals-test",
"version": "1.0.0",
"private": true,
"type": "module",
"dependencies": {
"lodash": "^4.17.21"
},
"devDependencies": {
"@types/lodash": "^4.17.14"
}
}
28 changes: 28 additions & 0 deletions tests/integration/externals/user-externals/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { defineConfig } from '@rslib/core';
import { generateBundleEsmConfig } from 'test-helper';

const baseExternals = {
externals: { 'lodash/zip': 'lodash-es/zip', 'node:fs': 'memfs' },
};

export default defineConfig({
lib: [
generateBundleEsmConfig({
output: {
externals: baseExternals,
distPath: {
root: 'dist/bundle',
},
},
}),
generateBundleEsmConfig({
bundle: false,
output: {
externals: { ...baseExternals, './foo': './foo2' },
distPath: {
root: 'dist/bundle-false',
},
},
}),
],
});
1 change: 1 addition & 0 deletions tests/integration/externals/user-externals/src/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo';
6 changes: 6 additions & 0 deletions tests/integration/externals/user-externals/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import fs from 'node:fs';
import lodash from 'lodash';
import zip from 'lodash/zip';
import { foo } from './foo';

console.log(fs, lodash.add, zip, foo);

0 comments on commit 5867fd1

Please sign in to comment.