fix: Only allow a single entrypoint with a given name

This commit is contained in:
Aaron Klinker
2023-06-24 17:57:31 -05:00
parent 3ca16eeb23
commit 8eb4e86b91
2 changed files with 20 additions and 16 deletions
+6 -15
View File
@@ -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',
);
});
});
+14 -1
View File
@@ -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<string, Entrypoint | undefined> = {};
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;