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: startsWith cannot determine subdirectories correctly #4916

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions .changeset/shaggy-bears-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/app-tools': patch
---

fix: startsWith cannot determine subdirectories correctly
fix: startsWith 函数不能正确地判断子目录
14 changes: 12 additions & 2 deletions packages/solutions/app-tools/src/analyze/getBundleEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ const ensureExtensions = (file: string) => {
*/
const isDirectory = (file: string) => !path.extname(file);

const isSubDirOrEqual = (parent: string, child: string): boolean => {
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved
if (parent === child) {
return true;
}
const relative = path.relative(parent, child);
const isSubdir =
relative && !relative.startsWith('..') && !path.isAbsolute(relative);
return Boolean(isSubdir);
};

const ifAlreadyExists = (
entrypoints: Entrypoint[],
checked: Entrypoint,
Expand All @@ -38,8 +48,8 @@ const ifAlreadyExists = (
}
// filesystem routes entrypoint conflict with normal entrypoint.
if (
entrypoint.entry.startsWith(checked.entry) ||
checked.entry.startsWith(entrypoint.entry)
isSubDirOrEqual(entrypoint.entry, checked.entry) ||
isSubDirOrEqual(checked.entry, entrypoint.entry)
) {
throw new Error(
`Entry configuration conflicts\n Your configuration: ${checked.entry}.\n Default entrypoint: ${entrypoint.entry}\n Please reset source.entries or set source.disableDefaultEntries to disable the default entry rules.`,
Expand Down