Skip to content

Commit 7c34973

Browse files
authored
fix: entry query should handle non-string entry (#484)
1 parent c11cfbf commit 7c34973

File tree

3 files changed

+143
-8
lines changed

3 files changed

+143
-8
lines changed

packages/core/src/config.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import type {
3939
LibOnlyConfig,
4040
PkgJson,
4141
Redirect,
42+
RsbuildConfigEntry,
43+
RsbuildConfigEntryItem,
4244
RsbuildConfigOutputTarget,
4345
RsbuildConfigWithLibInfo,
4446
RslibConfig,
@@ -788,18 +790,36 @@ const composeSyntaxConfig = (
788790
};
789791
};
790792

791-
const appendEntryQuery = (
792-
entry: NonNullable<RsbuildConfig['source']>['entry'],
793-
): NonNullable<RsbuildConfig['source']>['entry'] => {
794-
const newEntry: Record<string, string> = {};
795-
for (const key in entry) {
796-
newEntry[key] = `${entry[key]}?${RSLIB_ENTRY_QUERY}`;
793+
export const appendEntryQuery = (
794+
entry: RsbuildConfigEntry,
795+
): RsbuildConfigEntry => {
796+
const newEntry: Record<string, RsbuildConfigEntryItem> = {};
797+
798+
for (const [key, value] of Object.entries(entry)) {
799+
let result: RsbuildConfigEntryItem = value;
800+
801+
if (typeof value === 'string') {
802+
result = `${value}?${RSLIB_ENTRY_QUERY}`;
803+
} else if (Array.isArray(value)) {
804+
result = value.map((item) => `${item}?${RSLIB_ENTRY_QUERY}`);
805+
} else {
806+
result = {
807+
...value,
808+
import:
809+
typeof value.import === 'string'
810+
? `${value.import}?${RSLIB_ENTRY_QUERY}`
811+
: value.import.map((item) => `${item}?${RSLIB_ENTRY_QUERY}`),
812+
};
813+
}
814+
815+
newEntry[key] = result;
797816
}
817+
798818
return newEntry;
799819
};
800820

801821
const composeEntryConfig = async (
802-
entries: NonNullable<RsbuildConfig['source']>['entry'],
822+
entries: RsbuildConfigEntry,
803823
bundle: LibConfig['bundle'],
804824
root: string,
805825
cssModulesAuto: CssLoaderOptionsAuto,
@@ -1154,7 +1174,7 @@ async function composeLibRsbuildConfig(config: LibConfig) {
11541174
userExternals: config.output?.externals,
11551175
});
11561176
const { entryConfig, lcp } = await composeEntryConfig(
1157-
config.source?.entry,
1177+
config.source?.entry!,
11581178
config.bundle,
11591179
rootPath,
11601180
cssModulesAuto,

packages/core/src/types/config/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export type RsbuildConfigWithLibInfo = {
2424
config: RsbuildConfig;
2525
};
2626

27+
export type RsbuildConfigEntry = NonNullable<
28+
NonNullable<RsbuildConfig['source']>['entry']
29+
>;
30+
export type RsbuildConfigEntryItem = RsbuildConfigEntry[string];
31+
2732
export type RsbuildConfigOutputTarget = NonNullable<
2833
RsbuildConfig['output']
2934
>['target'];

packages/core/tests/entry.test.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { describe, expect, test } from 'vitest';
2+
import { appendEntryQuery } from '../src/config';
3+
4+
describe('appendEntryQuery', () => {
5+
test('string', () => {
6+
expect(
7+
appendEntryQuery({
8+
index: 'src/index.js',
9+
foo: 'src/foo.js',
10+
}),
11+
).toMatchInlineSnapshot(`
12+
{
13+
"foo": "src/foo.js?__rslib_entry__",
14+
"index": "src/index.js?__rslib_entry__",
15+
}
16+
`);
17+
});
18+
19+
test('string[]', () => {
20+
expect(
21+
appendEntryQuery({
22+
index: ['src/index.ts', 'src/extra.ts'],
23+
foo: ['src/foo.js'],
24+
}),
25+
).toMatchInlineSnapshot(`
26+
{
27+
"foo": [
28+
"src/foo.js?__rslib_entry__",
29+
],
30+
"index": [
31+
"src/index.ts?__rslib_entry__",
32+
"src/extra.ts?__rslib_entry__",
33+
],
34+
}
35+
`);
36+
});
37+
38+
test('Rspack.EntryDescription', () => {
39+
expect(
40+
appendEntryQuery({
41+
index: {
42+
import: ['src/index.ts', 'src/extra.ts'],
43+
layer: 'l1',
44+
},
45+
foo: {
46+
import: ['src/foo.js'],
47+
dependOn: ['src/dep.js'],
48+
},
49+
}),
50+
).toMatchInlineSnapshot(`
51+
{
52+
"foo": {
53+
"dependOn": [
54+
"src/dep.js",
55+
],
56+
"import": [
57+
"src/foo.js?__rslib_entry__",
58+
],
59+
},
60+
"index": {
61+
"import": [
62+
"src/index.ts?__rslib_entry__",
63+
"src/extra.ts?__rslib_entry__",
64+
],
65+
"layer": "l1",
66+
},
67+
}
68+
`);
69+
});
70+
71+
test('combined', () => {
72+
expect(
73+
appendEntryQuery({
74+
index: {
75+
import: ['src/index.ts', 'src/extra.ts'],
76+
layer: 'l1',
77+
},
78+
foo: {
79+
import: ['src/foo.js'],
80+
dependOn: ['src/dep.js'],
81+
},
82+
bar: 'src/bar.ts',
83+
baz: ['src/baz.ts', 'src/bar.ts'],
84+
}),
85+
).toMatchInlineSnapshot(`
86+
{
87+
"bar": "src/bar.ts?__rslib_entry__",
88+
"baz": [
89+
"src/baz.ts?__rslib_entry__",
90+
"src/bar.ts?__rslib_entry__",
91+
],
92+
"foo": {
93+
"dependOn": [
94+
"src/dep.js",
95+
],
96+
"import": [
97+
"src/foo.js?__rslib_entry__",
98+
],
99+
},
100+
"index": {
101+
"import": [
102+
"src/index.ts?__rslib_entry__",
103+
"src/extra.ts?__rslib_entry__",
104+
],
105+
"layer": "l1",
106+
},
107+
}
108+
`);
109+
});
110+
});

0 commit comments

Comments
 (0)