From cb84473965cdb2a4a3d3904a66593be787f3ba30 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Tue, 7 Jan 2025 14:18:19 +0800 Subject: [PATCH 1/4] fix: user externals should have higher priority --- packages/core/src/config.ts | 6 +++--- pnpm-lock.yaml | 10 ++++++++++ tests/integration/externals/index.test.ts | 17 +++++++++++++++++ .../externals/user-externals/package.json | 12 ++++++++++++ .../externals/user-externals/rslib.config.ts | 9 +++++++++ .../externals/user-externals/src/index.js | 5 +++++ 6 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 tests/integration/externals/user-externals/package.json create mode 100644 tests/integration/externals/user-externals/rslib.config.ts create mode 100644 tests/integration/externals/user-externals/src/index.js diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 56a27edc1..2c74f6fb8 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -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 later to override the externals config of the ahead ones. + // 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, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 47d46ff6b..973e9cbfb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -694,6 +694,16 @@ importers: tests/integration/externals/node: {} + tests/integration/externals/user-externals: + dependencies: + lodash: + specifier: ^4.17.21 + version: 4.17.21 + devDependencies: + '@types/lodash': + specifier: ^4.17.14 + version: 4.17.14 + tests/integration/format: {} tests/integration/minify/config/disabled: {} diff --git a/tests/integration/externals/index.test.ts b/tests/integration/externals/index.test.ts index ca93be81c..9f1101ecd 100644 --- a/tests/integration/externals/index.test.ts +++ b/tests/integration/externals/index.test.ts @@ -74,3 +74,20 @@ 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 + const fixturePath = join(__dirname, 'user-externals'); + const { entries } = await buildAndGetResults({ fixturePath }); + expect(entries.esm).toMatchInlineSnapshot( + ` + "import * as __WEBPACK_EXTERNAL_MODULE_memfs__ from "memfs"; + import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash"; + import * as __WEBPACK_EXTERNAL_MODULE_lodash_es_zip_b8981481__ from "lodash-es/zip"; + console.log(__WEBPACK_EXTERNAL_MODULE_memfs__["default"], __WEBPACK_EXTERNAL_MODULE_lodash__["default"].add, __WEBPACK_EXTERNAL_MODULE_lodash_es_zip_b8981481__["default"]); + " + `, + ); +}); diff --git a/tests/integration/externals/user-externals/package.json b/tests/integration/externals/user-externals/package.json new file mode 100644 index 000000000..39258c091 --- /dev/null +++ b/tests/integration/externals/user-externals/package.json @@ -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" + } +} diff --git a/tests/integration/externals/user-externals/rslib.config.ts b/tests/integration/externals/user-externals/rslib.config.ts new file mode 100644 index 000000000..7a19f2736 --- /dev/null +++ b/tests/integration/externals/user-externals/rslib.config.ts @@ -0,0 +1,9 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [generateBundleEsmConfig()], + output: { + externals: { 'lodash/zip': 'lodash-es/zip', 'node:fs': 'memfs' }, + }, +}); diff --git a/tests/integration/externals/user-externals/src/index.js b/tests/integration/externals/user-externals/src/index.js new file mode 100644 index 000000000..719108b4b --- /dev/null +++ b/tests/integration/externals/user-externals/src/index.js @@ -0,0 +1,5 @@ +import fs from 'node:fs'; +import lodash from 'lodash'; +import zip from 'lodash/zip'; + +console.log(fs, lodash.add, zip); From 2705cb477fdf1117da57688ef10ca01c0de9b698 Mon Sep 17 00:00:00 2001 From: Wei Date: Tue, 7 Jan 2025 14:57:26 +0800 Subject: [PATCH 2/4] Update packages/core/src/config.ts --- packages/core/src/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 2c74f6fb8..ad8c74d7c 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -1391,7 +1391,7 @@ 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. `userExternalsConfig` should present later to override the externals config of the ahead ones. + // 2. `userExternalsConfig` should present earlier to override the externals config of the ahead ones. // 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. From e010d26142955c0f33a42621009e57e83a8d2cb1 Mon Sep 17 00:00:00 2001 From: Wei Date: Tue, 7 Jan 2025 14:58:31 +0800 Subject: [PATCH 3/4] Update packages/core/src/config.ts --- packages/core/src/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index ad8c74d7c..155018e9f 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -1391,7 +1391,7 @@ 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. `userExternalsConfig` should present earlier to override the externals config of the ahead ones. + // 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. From 9faab6d68c87c751147a1379ea66f8b76de955fe Mon Sep 17 00:00:00 2001 From: fi3ework Date: Tue, 7 Jan 2025 15:23:36 +0800 Subject: [PATCH 4/4] up --- tests/integration/externals/index.test.ts | 30 ++++++++++++++----- .../externals/user-externals/rslib.config.ts | 27 ++++++++++++++--- .../externals/user-externals/src/foo.js | 1 + .../externals/user-externals/src/index.js | 3 +- 4 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 tests/integration/externals/user-externals/src/foo.js diff --git a/tests/integration/externals/index.test.ts b/tests/integration/externals/index.test.ts index 9f1101ecd..4a11497b9 100644 --- a/tests/integration/externals/index.test.ts +++ b/tests/integration/externals/index.test.ts @@ -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'; @@ -77,17 +77,31 @@ test('require ESM from CJS', async () => { test('user externals', async () => { // Ensure the priority of user externals higher than others. - // - memfs: userExternalsConfig > targetExternalsConfig - // - lodash-es/zip: userExternalsConfig > autoExternalConfig + // - "memfs": userExternalsConfig > targetExternalsConfig + // - "lodash-es/zip": userExternalsConfig > autoExternalConfig + // - "./foo2": userExternalsConfig > bundlelessExternalConfig + const fixturePath = join(__dirname, 'user-externals'); - const { entries } = await buildAndGetResults({ fixturePath }); - expect(entries.esm).toMatchInlineSnapshot( + const { entries, contents } = await buildAndGetResults({ fixturePath }); + expect(entries.esm0).toMatchInlineSnapshot( ` - "import * as __WEBPACK_EXTERNAL_MODULE_memfs__ from "memfs"; + "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_es_zip_b8981481__ from "lodash-es/zip"; - console.log(__WEBPACK_EXTERNAL_MODULE_memfs__["default"], __WEBPACK_EXTERNAL_MODULE_lodash__["default"].add, __WEBPACK_EXTERNAL_MODULE_lodash_es_zip_b8981481__["default"]); + 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); + " + `); }); diff --git a/tests/integration/externals/user-externals/rslib.config.ts b/tests/integration/externals/user-externals/rslib.config.ts index 7a19f2736..d4241ebf1 100644 --- a/tests/integration/externals/user-externals/rslib.config.ts +++ b/tests/integration/externals/user-externals/rslib.config.ts @@ -1,9 +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: { 'lodash/zip': 'lodash-es/zip', 'node:fs': 'memfs' }, - }, + lib: [ + generateBundleEsmConfig({ + output: { + externals: baseExternals, + distPath: { + root: 'dist/bundle', + }, + }, + }), + generateBundleEsmConfig({ + bundle: false, + output: { + externals: { ...baseExternals, './foo': './foo2' }, + distPath: { + root: 'dist/bundle-false', + }, + }, + }), + ], }); diff --git a/tests/integration/externals/user-externals/src/foo.js b/tests/integration/externals/user-externals/src/foo.js new file mode 100644 index 000000000..3329a7d97 --- /dev/null +++ b/tests/integration/externals/user-externals/src/foo.js @@ -0,0 +1 @@ +export const foo = 'foo'; diff --git a/tests/integration/externals/user-externals/src/index.js b/tests/integration/externals/user-externals/src/index.js index 719108b4b..295dc9d1e 100644 --- a/tests/integration/externals/user-externals/src/index.js +++ b/tests/integration/externals/user-externals/src/index.js @@ -1,5 +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); +console.log(fs, lodash.add, zip, foo);