Skip to content

Commit

Permalink
fix: startsWith cannot determine subdirectories correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
yimingjfe committed Nov 6, 2023
1 parent 8128486 commit 95a4494
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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 => {
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

0 comments on commit 95a4494

Please sign in to comment.