Skip to content

Commit

Permalink
fix path
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Dec 29, 2024
1 parent 9472efe commit 2f6c456
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
13 changes: 11 additions & 2 deletions src/util/file-path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,21 @@ describe('baseNameWithoutExtension', () => {
describe('validateEntryFiles', () => {
it('should throw error if there are multiple files with the same base name', () => {
expect(() =>
validateEntryFiles(['index.js', 'index.ts', 'index.mjs']),
validateEntryFiles(['index.js', 'index/index.ts']),
).toThrowError('Conflicted entry files found for entries: .')
})
it('should not throw error if there are no multiple files with the same base name', () => {
it.only('should throw error if the normalized base names are same', () => {
expect(() => validateEntryFiles(['foo/index.jsx', 'foo.ts'])).toThrowError(
'Conflicted entry files found for entries: ./foo',
)
})
it('should not throw error if there are no multiple files with the same base name', () => {
expect(() =>
validateEntryFiles([
'index.development.js',
'index.ts',
'index.react-server.mjs',
]),
).not.toThrow()
})
})
33 changes: 18 additions & 15 deletions src/util/file-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,34 @@ export const baseNameWithoutExtension = (filePath: string): string => {
}

export function validateEntryFiles(entryFiles: string[]) {
const baseNames = new Set<string>()
const duplicateBaseNames = new Set<string>()
const fileBasePaths = new Set<string>()
const duplicatePaths = new Set<string>()

for (const file of entryFiles) {
for (const filePath of entryFiles) {
// Check if there are multiple files with the same base name
// e.g. index.js, index.ts, index.mjs
// e.g. <name>.ext and <name>./index.ext
let baseName = getPathWithoutExtension(file)
const indexSuffix = '/index'
if (baseName.endsWith(indexSuffix)) {
baseName = baseName.slice(0, -indexSuffix.length)
const filePathWithoutExt = filePath
.slice(0, -path.extname(filePath).length)
.replace(/\\/g, '/')
const segments = filePathWithoutExt.split('/')
const lastSegment = segments.pop() || ''

if (lastSegment !== 'index' && lastSegment !== '') {
segments.push(lastSegment)
}
const fileBasePath = segments.join('/')

if (baseNames.has(baseName)) {
duplicateBaseNames.add(
if (fileBasePaths.has(fileBasePath)) {
duplicatePaths.add(
// Add a dot if the base name is empty, 'foo' -> './foo', '' -> '.'
'.' + (baseName !== '' ? '/' : '') + baseName,
'./' + filePath.replace(/\\/g, '/'),
)
}
baseNames.add(baseName)
fileBasePaths.add(fileBasePath)
}

if (duplicateBaseNames.size > 0) {
if (duplicatePaths.size > 0) {
throw new Error(
`Conflicted entry files found for entries: ${[...duplicateBaseNames].join(
`Conflicted entry files found for entries: ${[...duplicatePaths].join(
', ',
)}`,
)
Expand Down

0 comments on commit 2f6c456

Please sign in to comment.