From 8eb4e86b9176d2ab1997ae27f1aff220920e0862 Mon Sep 17 00:00:00 2001 From: Aaron Klinker Date: Sat, 24 Jun 2023 17:57:31 -0500 Subject: [PATCH] fix: Only allow a single entrypoint with a given name --- src/utils/__tests__/findEntrypoints.test.ts | 21 ++++++--------------- src/utils/findEntrypoints.ts | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/utils/__tests__/findEntrypoints.test.ts b/src/utils/__tests__/findEntrypoints.test.ts index 718678a6..4935f0cc 100644 --- a/src/utils/__tests__/findEntrypoints.test.ts +++ b/src/utils/__tests__/findEntrypoints.test.ts @@ -421,20 +421,11 @@ describe('findEntrypoints', () => { expect(entrypoints[0]).toEqual(expected); }); - it.todo( - 'should ignore CSS and JS files inside a HTML page directory', - () => {}, - ); + it('should not allow multiple entrypoints with the same name', async () => { + globMock.mockResolvedValueOnce(['popup.html', 'popup/index.html']); - it.todo( - 'should warn when there is an unexpected file in the entrypoints directory', - () => {}, - ); - - it.todo( - 'should ignore entrypoints starting with a . (preventing warnings like .DS_Store files)', - () => {}, - ); - - it.todo('should not allow multiple entrypoints with the same name', () => {}); + await expect(() => findEntrypoints(config)).rejects.toThrowError( + 'Multiple entrypoints with the name "popup" detected, but only one is allowed: src/entrypoints/popup.html, src/entrypoints/popup/index.html', + ); + }); }); diff --git a/src/utils/findEntrypoints.ts b/src/utils/findEntrypoints.ts index c30227c1..a73c095d 100644 --- a/src/utils/findEntrypoints.ts +++ b/src/utils/findEntrypoints.ts @@ -1,4 +1,4 @@ -import { resolve } from 'path'; +import { relative, resolve } from 'path'; import { BackgroundEntrypoint, BackgroundScriptDefintition, @@ -30,6 +30,7 @@ export async function findEntrypoints( relativePaths.sort(); const pathGlobs = Object.keys(PATH_GLOB_TO_TYPE_MAP); + const existingNames: Record = {}; const entrypoints: Entrypoint[] = []; await Promise.all( @@ -79,7 +80,19 @@ export async function findEntrypoints( }; } + const withSameName = existingNames[entrypoint.name]; + if (withSameName) { + throw Error( + `Multiple entrypoints with the name "${ + entrypoint.name + }" detected, but only one is allowed: ${[ + relative(config.root, withSameName.inputPath), + relative(config.root, entrypoint.inputPath), + ].join(', ')}`, + ); + } entrypoints.push(entrypoint); + existingNames[entrypoint.name] = entrypoint; }), ); return entrypoints;