-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: error on duplicated entries (#619)
- Loading branch information
Showing
10 changed files
with
145 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { | ||
getPathWithoutExtension, | ||
baseNameWithoutExtension, | ||
validateEntryFiles, | ||
} from './file-path' | ||
|
||
describe('getFirstBaseName', () => { | ||
it('should return first part base name without extension of file path', () => { | ||
expect(getPathWithoutExtension('index.js')).toBe('index') | ||
expect(getPathWithoutExtension('index.d.ts')).toBe('index') | ||
expect(getPathWithoutExtension('index')).toBe('index') | ||
// give few segments nested file path | ||
expect(getPathWithoutExtension('./foo/nested/index.js')).toBe( | ||
'./foo/nested', | ||
) | ||
expect(getPathWithoutExtension('./foo/nested/index.d.ts')).toBe( | ||
'./foo/nested', | ||
) | ||
expect(getPathWithoutExtension('./foo.jsx')).toBe('./foo') | ||
}) | ||
}) | ||
|
||
describe('baseNameWithoutExtension', () => { | ||
it('should return full base name without last extension of file path', () => { | ||
// give few segments nested file path | ||
expect(baseNameWithoutExtension('dist/foo/nested/index.js')).toBe('index') | ||
expect( | ||
baseNameWithoutExtension('dist/foo/nested/index.development.ts'), | ||
).toBe('index.development') | ||
expect( | ||
baseNameWithoutExtension('dist/foo/nested/index.react-server.js'), | ||
).toBe('index.react-server') | ||
}) | ||
}) | ||
|
||
describe('validateEntryFiles', () => { | ||
it('should throw error if there are multiple files with the same base name', () => { | ||
expect(() => | ||
validateEntryFiles(['index.js', 'index/index.ts']), | ||
).toThrowError('Conflicted entry files found for entries: .') | ||
}) | ||
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import path from 'path' | ||
|
||
// Example: ./src/util/foo.ts -> ./src/util/foo | ||
// Example: ./src/util/foo/index.ts -> ./src/util/foo | ||
// Example: ./src/util/foo.d.ts -> ./src/util/foo | ||
export const getPathWithoutExtension = (filePath: string): string => { | ||
const pathWithoutExtension = filePath | ||
// Remove the file extension first | ||
.replace(/(\.\w+)+$/, '') | ||
// Remove '/index' if it exists at the end of the path | ||
.replace(/\/index$/, '') | ||
|
||
return pathWithoutExtension | ||
} | ||
|
||
// Example: ./src/util/foo.development.ts -> foo.development | ||
// Example: ./src/util/foo.react-server.ts -> foo.react-server | ||
export const baseNameWithoutExtension = (filePath: string): string => { | ||
return path.basename(filePath, path.extname(filePath)) | ||
} | ||
|
||
export function validateEntryFiles(entryFiles: string[]) { | ||
const fileBasePaths = new Set<string>() | ||
const duplicatePaths = new Set<string>() | ||
|
||
for (const filePath of entryFiles) { | ||
// Check if there are multiple files with the same base name | ||
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 (fileBasePaths.has(fileBasePath)) { | ||
duplicatePaths.add( | ||
// Add a dot if the base name is empty, 'foo' -> './foo', '' -> '.' | ||
'./' + filePath.replace(/\\/g, '/'), | ||
) | ||
} | ||
fileBasePaths.add(fileBasePath) | ||
} | ||
|
||
if (duplicatePaths.size > 0) { | ||
throw new Error( | ||
`Conflicted entry files found for entries: ${[...duplicatePaths].join( | ||
', ', | ||
)}`, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
test/integration/conflicted-entry/conflicted-entry.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { createIntegrationTest } from '../utils' | ||
|
||
describe('integration - conflicted-entry', () => { | ||
it('should error on conflicted entries', async () => { | ||
await createIntegrationTest( | ||
{ | ||
directory: __dirname, | ||
}, | ||
async ({ code, stderr }) => { | ||
expect(code).toBe(1) | ||
expect(stderr).toContain( | ||
'Conflicted entry files found for entries: ./foo', | ||
) | ||
}, | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "conflicted-entry", | ||
"exports": { | ||
"./foo": "./dist/foo.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class Foo {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class Foo {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters