Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import type {
LibOnlyConfig,
PkgJson,
Redirect,
RsbuildConfigEntry,
RsbuildConfigEntryItem,
RsbuildConfigOutputTarget,
RsbuildConfigWithLibInfo,
RslibConfig,
Expand Down Expand Up @@ -788,18 +790,36 @@ const composeSyntaxConfig = (
};
};

const appendEntryQuery = (
entry: NonNullable<RsbuildConfig['source']>['entry'],
): NonNullable<RsbuildConfig['source']>['entry'] => {
const newEntry: Record<string, string> = {};
for (const key in entry) {
newEntry[key] = `${entry[key]}?${RSLIB_ENTRY_QUERY}`;
export const appendEntryQuery = (
entry: RsbuildConfigEntry,
): RsbuildConfigEntry => {
const newEntry: Record<string, RsbuildConfigEntryItem> = {};

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<RsbuildConfig['source']>['entry'],
entries: RsbuildConfigEntry,
bundle: LibConfig['bundle'],
root: string,
cssModulesAuto: CssLoaderOptionsAuto,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export type RsbuildConfigWithLibInfo = {
config: RsbuildConfig;
};

export type RsbuildConfigEntry = NonNullable<
NonNullable<RsbuildConfig['source']>['entry']
>;
export type RsbuildConfigEntryItem = RsbuildConfigEntry[string];

export type RsbuildConfigOutputTarget = NonNullable<
RsbuildConfig['output']
>['target'];
Expand Down
110 changes: 110 additions & 0 deletions packages/core/tests/entry.test.ts
Original file line number Diff line number Diff line change
@@ -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",
},
}
`);
});
});
Loading