diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 0e40eb6ff..010d7bdd8 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -39,6 +39,8 @@ import type { LibOnlyConfig, PkgJson, Redirect, + RsbuildConfigEntry, + RsbuildConfigEntryItem, RsbuildConfigOutputTarget, RsbuildConfigWithLibInfo, RslibConfig, @@ -788,18 +790,36 @@ const composeSyntaxConfig = ( }; }; -const appendEntryQuery = ( - entry: NonNullable['entry'], -): NonNullable['entry'] => { - const newEntry: Record = {}; - for (const key in entry) { - newEntry[key] = `${entry[key]}?${RSLIB_ENTRY_QUERY}`; +export const appendEntryQuery = ( + entry: RsbuildConfigEntry, +): RsbuildConfigEntry => { + const newEntry: Record = {}; + + for (const [key, value] of Object.entries(entry)) { + let result: RsbuildConfigEntryItem = value; + + if (typeof value === 'string') { + result = `${value}?${RSLIB_ENTRY_QUERY}`; + } else if (Array.isArray(value)) { + result = value.map((item) => `${item}?${RSLIB_ENTRY_QUERY}`); + } else { + result = { + ...value, + import: + typeof value.import === 'string' + ? `${value.import}?${RSLIB_ENTRY_QUERY}` + : value.import.map((item) => `${item}?${RSLIB_ENTRY_QUERY}`), + }; + } + + newEntry[key] = result; } + return newEntry; }; const composeEntryConfig = async ( - entries: NonNullable['entry'], + entries: RsbuildConfigEntry, bundle: LibConfig['bundle'], root: string, cssModulesAuto: CssLoaderOptionsAuto, @@ -1154,7 +1174,7 @@ async function composeLibRsbuildConfig(config: LibConfig) { userExternals: config.output?.externals, }); const { entryConfig, lcp } = await composeEntryConfig( - config.source?.entry, + config.source?.entry!, config.bundle, rootPath, cssModulesAuto, diff --git a/packages/core/src/types/config/index.ts b/packages/core/src/types/config/index.ts index e9eb95408..72d2f461d 100644 --- a/packages/core/src/types/config/index.ts +++ b/packages/core/src/types/config/index.ts @@ -24,6 +24,11 @@ export type RsbuildConfigWithLibInfo = { config: RsbuildConfig; }; +export type RsbuildConfigEntry = NonNullable< + NonNullable['entry'] +>; +export type RsbuildConfigEntryItem = RsbuildConfigEntry[string]; + export type RsbuildConfigOutputTarget = NonNullable< RsbuildConfig['output'] >['target']; diff --git a/packages/core/tests/entry.test.ts b/packages/core/tests/entry.test.ts new file mode 100644 index 000000000..c8914c485 --- /dev/null +++ b/packages/core/tests/entry.test.ts @@ -0,0 +1,110 @@ +import { describe, expect, test } from 'vitest'; +import { appendEntryQuery } from '../src/config'; + +describe('appendEntryQuery', () => { + test('string', () => { + expect( + appendEntryQuery({ + index: 'src/index.js', + foo: 'src/foo.js', + }), + ).toMatchInlineSnapshot(` + { + "foo": "src/foo.js?__rslib_entry__", + "index": "src/index.js?__rslib_entry__", + } + `); + }); + + test('string[]', () => { + expect( + appendEntryQuery({ + index: ['src/index.ts', 'src/extra.ts'], + foo: ['src/foo.js'], + }), + ).toMatchInlineSnapshot(` + { + "foo": [ + "src/foo.js?__rslib_entry__", + ], + "index": [ + "src/index.ts?__rslib_entry__", + "src/extra.ts?__rslib_entry__", + ], + } + `); + }); + + test('Rspack.EntryDescription', () => { + expect( + appendEntryQuery({ + index: { + import: ['src/index.ts', 'src/extra.ts'], + layer: 'l1', + }, + foo: { + import: ['src/foo.js'], + dependOn: ['src/dep.js'], + }, + }), + ).toMatchInlineSnapshot(` + { + "foo": { + "dependOn": [ + "src/dep.js", + ], + "import": [ + "src/foo.js?__rslib_entry__", + ], + }, + "index": { + "import": [ + "src/index.ts?__rslib_entry__", + "src/extra.ts?__rslib_entry__", + ], + "layer": "l1", + }, + } + `); + }); + + test('combined', () => { + expect( + appendEntryQuery({ + index: { + import: ['src/index.ts', 'src/extra.ts'], + layer: 'l1', + }, + foo: { + import: ['src/foo.js'], + dependOn: ['src/dep.js'], + }, + bar: 'src/bar.ts', + baz: ['src/baz.ts', 'src/bar.ts'], + }), + ).toMatchInlineSnapshot(` + { + "bar": "src/bar.ts?__rslib_entry__", + "baz": [ + "src/baz.ts?__rslib_entry__", + "src/bar.ts?__rslib_entry__", + ], + "foo": { + "dependOn": [ + "src/dep.js", + ], + "import": [ + "src/foo.js?__rslib_entry__", + ], + }, + "index": { + "import": [ + "src/index.ts?__rslib_entry__", + "src/extra.ts?__rslib_entry__", + ], + "layer": "l1", + }, + } + `); + }); +});