Skip to content

Commit 9860d26

Browse files
committed
fix: detect possible glob pattern in bundle more
1 parent a823737 commit 9860d26

File tree

7 files changed

+62
-5
lines changed

7 files changed

+62
-5
lines changed

packages/core/src/config.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path, { dirname, extname, isAbsolute, join } from 'node:path';
33
import {
44
type EnvironmentConfig,
55
type RsbuildConfig,
6+
type RsbuildEntry,
67
type RsbuildPlugin,
78
type RsbuildPlugins,
89
type Rspack,
@@ -844,25 +845,26 @@ const composeSyntaxConfig = (
844845
};
845846
};
846847

847-
export const appendEntryQuery = (
848+
const traverseEntryQuery = (
848849
entry: RsbuildConfigEntry,
850+
callback: (entry: string) => string,
849851
): RsbuildConfigEntry => {
850852
const newEntry: Record<string, RsbuildConfigEntryItem> = {};
851853

852854
for (const [key, value] of Object.entries(entry)) {
853855
let result: RsbuildConfigEntryItem = value;
854856

855857
if (typeof value === 'string') {
856-
result = `${value}?${RSLIB_ENTRY_QUERY}`;
858+
result = callback(value);
857859
} else if (Array.isArray(value)) {
858-
result = value.map((item) => `${item}?${RSLIB_ENTRY_QUERY}`);
860+
result = value.map(callback);
859861
} else {
860862
result = {
861863
...value,
862864
import:
863865
typeof value.import === 'string'
864-
? `${value.import}?${RSLIB_ENTRY_QUERY}`
865-
: value.import.map((item) => `${item}?${RSLIB_ENTRY_QUERY}`),
866+
? callback(value.import)
867+
: value.import.map(callback),
866868
};
867869
}
868870

@@ -872,6 +874,9 @@ export const appendEntryQuery = (
872874
return newEntry;
873875
};
874876

877+
export const appendEntryQuery = (entries: RsbuildConfigEntry): RsbuildEntry =>
878+
traverseEntryQuery(entries, (item) => `${item}?${RSLIB_ENTRY_QUERY}`);
879+
875880
const composeEntryConfig = async (
876881
entries: RsbuildConfigEntry,
877882
bundle: LibConfig['bundle'],
@@ -883,6 +888,22 @@ const composeEntryConfig = async (
883888
}
884889

885890
if (bundle !== false) {
891+
let isFileEntry = false;
892+
traverseEntryQuery(entries, (entry) => {
893+
if (fs.existsSync(entry)) {
894+
const stats = fs.statSync(entry);
895+
isFileEntry = stats.isFile();
896+
}
897+
898+
return entry;
899+
});
900+
901+
if (!isFileEntry) {
902+
throw new Error(
903+
`Glob pattern is not supported when "bundle" is "true", considering ${color.green('set "bundle" to "false"')} to use bundleless mode. See ${color.green('https://lib.rsbuild.dev/guide/basic/output-structure')} for more details.`,
904+
);
905+
}
906+
886907
return {
887908
entryConfig: {
888909
source: {

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "entry-glob-bundle-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [generateBundleEsmConfig({})],
6+
source: {
7+
entry: {
8+
index: ['./src', '!./src/ignored'],
9+
},
10+
},
11+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const bar = 'bar';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'foo';

tests/integration/entry/index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { join } from 'node:path';
2+
import stripAnsi from 'strip-ansi';
23
import { buildAndGetResults, queryContent } from 'test-helper';
34
import { expect, test } from 'vitest';
45

@@ -98,3 +99,17 @@ test('glob entry bundleless', async () => {
9899
}
99100
`);
100101
});
102+
103+
test('glob entry bundle', async () => {
104+
const fixturePath = join(__dirname, 'glob-bundle');
105+
let errMsg = '';
106+
try {
107+
await buildAndGetResults({ fixturePath });
108+
} catch (e) {
109+
errMsg = (e as Error).message;
110+
}
111+
112+
expect(stripAnsi(errMsg)).toMatchInlineSnapshot(
113+
`"Glob pattern is not supported when "bundle" is "true", considering set "bundle" to "false" to use bundleless mode. See https://lib.rsbuild.dev/guide/basic/output-structure for more details."`,
114+
);
115+
});

0 commit comments

Comments
 (0)