Skip to content
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

fix: validate entry correctness in bundle mode #659

Merged
merged 1 commit into from
Jan 7, 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
8 changes: 7 additions & 1 deletion packages/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ export function runCli(): void {
await cliBuild();
} catch (err) {
logger.error('Failed to build.');
logger.error(err);
if (err instanceof AggregateError) {
for (const error of err.errors) {
logger.error(error);
}
} else {
logger.error(err);
}
process.exit(1);
}
});
Expand Down
28 changes: 23 additions & 5 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,22 +899,40 @@ const composeEntryConfig = async (
}

if (bundle !== false) {
let isFileEntry = false;
const entryErrorReasons: string[] = [];
traverseEntryQuery(entries, (entry) => {
const entryAbsPath = path.isAbsolute(entry)
? entry
: path.resolve(root, entry);
const isDirLike = path.extname(entryAbsPath) === '';
const dirError = `Glob pattern ${color.cyan(`"${entry}"`)} is not supported when "bundle" is "true", considering "bundle" to "false" to use bundleless mode, or specify a file entry to bundle. See ${color.green('https://lib.rsbuild.dev/guide/basic/output-structure')} for more details.`;

if (fs.existsSync(entryAbsPath)) {
const stats = fs.statSync(entryAbsPath);
isFileEntry = stats.isFile();
if (!stats.isFile()) {
// Existed dir.
entryErrorReasons.push(dirError);
} else {
// Existed file.
}
} else {
if (isDirLike) {
// Non-existed dir.
entryErrorReasons.push(dirError);
} else {
// Non-existed file.
entryErrorReasons.push(
`Can't resolve the entry ${color.cyan(`"${entry}"`)} at the location ${color.cyan(`${entryAbsPath}`)}. Please ensure that the file exists.`,
);
}
}

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.`,
if (entryErrorReasons.length) {
throw new AggregateError(
entryErrorReasons.map((reason) => new Error(reason)),
);
}

Expand Down
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

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

11 changes: 0 additions & 11 deletions tests/integration/entry/glob-bundle/rslib.config.ts

This file was deleted.

28 changes: 23 additions & 5 deletions tests/integration/entry/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,34 @@ test('glob entry bundleless', async () => {
`);
});

test('glob entry bundle', async () => {
const fixturePath = join(__dirname, 'glob-bundle');
test('validate entry and throw errors', async () => {
const fixturePath = join(__dirname, 'validate');
let errMsg = '';
try {
await buildAndGetResults({ fixturePath });
await buildAndGetResults({
fixturePath,
configPath: 'bundleWithGlob.config.ts',
});
} catch (e) {
errMsg = (e as Error).message;
errMsg = (e as AggregateError).errors.join('\n\n');
}

expect(stripAnsi(errMsg)).toMatchInlineSnapshot(`
"Error: Glob pattern "./src" is not supported when "bundle" is "true", considering "bundle" to "false" to use bundleless mode, or specify a file entry to bundle. See https://lib.rsbuild.dev/guide/basic/output-structure for more details.

Error: Glob pattern "!./src/ignored" is not supported when "bundle" is "true", considering "bundle" to "false" to use bundleless mode, or specify a file entry to bundle. See https://lib.rsbuild.dev/guide/basic/output-structure for more details."
`);

try {
await buildAndGetResults({
fixturePath,
configPath: 'nonExistingFile.config.ts',
});
} catch (e) {
errMsg = (e as AggregateError).errors.join('\n\n');
}

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."`,
`"Error: Can't resolve the entry "./src/main.ts" at the location <ROOT>/tests/integration/entry/validate/src/main.ts. Please ensure that the file exists."`,
);
});
14 changes: 14 additions & 0 deletions tests/integration/entry/validate/bundleWithGlob.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineConfig } from '@rslib/core';
import { generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
source: {
entry: {
index: ['./src', '!./src/ignored'],
},
},
}),
],
});
14 changes: 14 additions & 0 deletions tests/integration/entry/validate/nonExistingFile.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineConfig } from '@rslib/core';
import { generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
source: {
entry: {
index: ['./src/main.ts'],
},
},
}),
],
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "entry-glob-bundle-test",
"name": "entry-validate-test",
"version": "1.0.0",
"private": true,
"type": "module"
Expand Down
Loading