Skip to content

fix: detect possible glob pattern in bundle more #634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2025
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
34 changes: 29 additions & 5 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path, { dirname, extname, isAbsolute, join } from 'node:path';
import {
type EnvironmentConfig,
type RsbuildConfig,
type RsbuildEntry,
type RsbuildPlugin,
type RsbuildPlugins,
type Rspack,
Expand Down Expand Up @@ -844,25 +845,26 @@ const composeSyntaxConfig = (
};
};

export const appendEntryQuery = (
const traverseEntryQuery = (
entry: RsbuildConfigEntry,
callback: (entry: string) => string,
): 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}`;
result = callback(value);
} else if (Array.isArray(value)) {
result = value.map((item) => `${item}?${RSLIB_ENTRY_QUERY}`);
result = value.map(callback);
} else {
result = {
...value,
import:
typeof value.import === 'string'
? `${value.import}?${RSLIB_ENTRY_QUERY}`
: value.import.map((item) => `${item}?${RSLIB_ENTRY_QUERY}`),
? callback(value.import)
: value.import.map(callback),
};
}

Expand All @@ -872,6 +874,9 @@ export const appendEntryQuery = (
return newEntry;
};

export const appendEntryQuery = (entries: RsbuildConfigEntry): RsbuildEntry =>
traverseEntryQuery(entries, (item) => `${item}?${RSLIB_ENTRY_QUERY}`);

const composeEntryConfig = async (
entries: RsbuildConfigEntry,
bundle: LibConfig['bundle'],
Expand All @@ -883,6 +888,25 @@ const composeEntryConfig = async (
}

if (bundle !== false) {
let isFileEntry = false;
traverseEntryQuery(entries, (entry) => {
const entryAbsPath = path.isAbsolute(entry)
? entry
: path.resolve(root, entry);
if (fs.existsSync(entryAbsPath)) {
const stats = fs.statSync(entryAbsPath);
isFileEntry = stats.isFile();
}

return entry;
});

if (!isFileEntry) {
throw new Error(
`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.`,
);
}

return {
entryConfig: {
source: {
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/integration/entry/glob-bundle/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "entry-glob-bundle-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
11 changes: 11 additions & 0 deletions tests/integration/entry/glob-bundle/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from '@rslib/core';
import { generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [generateBundleEsmConfig({})],
source: {
entry: {
index: ['./src', '!./src/ignored'],
},
},
});
1 change: 1 addition & 0 deletions tests/integration/entry/glob-bundle/src/bar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const bar = 'bar';
1 change: 1 addition & 0 deletions tests/integration/entry/glob-bundle/src/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo';
15 changes: 15 additions & 0 deletions tests/integration/entry/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { join } from 'node:path';
import stripAnsi from 'strip-ansi';
import { buildAndGetResults, queryContent } from 'test-helper';
import { expect, test } from 'vitest';

Expand Down Expand Up @@ -98,3 +99,17 @@ test('glob entry bundleless', async () => {
}
`);
});

test('glob entry bundle', async () => {
const fixturePath = join(__dirname, 'glob-bundle');
let errMsg = '';
try {
await buildAndGetResults({ fixturePath });
} catch (e) {
errMsg = (e as Error).message;
}

expect(stripAnsi(errMsg)).toMatchInlineSnapshot(
`"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."`,
);
});
Loading