Fix theme name requirement regression with efficient lookup by name (#35007)
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
@@ -29,7 +29,7 @@ export function MastodonThemes(): Plugin {
|
||||
throw new Error('Invalid themes.yml file');
|
||||
}
|
||||
|
||||
for (const themePath of Object.values(themes)) {
|
||||
for (const [themeName, themePath] of Object.entries(themes)) {
|
||||
if (
|
||||
typeof themePath !== 'string' ||
|
||||
themePath.split('.').length !== 2 || // Ensure it has exactly one period
|
||||
@@ -40,7 +40,7 @@ export function MastodonThemes(): Plugin {
|
||||
);
|
||||
continue;
|
||||
}
|
||||
entrypoints[path.basename(themePath)] = path.resolve(
|
||||
entrypoints[`themes/${themeName}`] = path.resolve(
|
||||
userConfig.root,
|
||||
themePath,
|
||||
);
|
||||
|
||||
68
config/vite/plugin-name-lookup.ts
Normal file
68
config/vite/plugin-name-lookup.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { relative, extname } from 'node:path';
|
||||
|
||||
import type { Plugin } from 'vite';
|
||||
|
||||
export function MastodonNameLookup(): Plugin {
|
||||
const nameMap: Record<string, string> = {};
|
||||
|
||||
let root = '';
|
||||
|
||||
return {
|
||||
name: 'mastodon-name-lookup',
|
||||
applyToEnvironment(environment) {
|
||||
return !!environment.config.build.manifest;
|
||||
},
|
||||
configResolved(userConfig) {
|
||||
root = userConfig.root;
|
||||
},
|
||||
generateBundle(options, bundle) {
|
||||
if (!root) {
|
||||
throw new Error(
|
||||
'MastodonNameLookup plugin requires the root to be set in the config.',
|
||||
);
|
||||
}
|
||||
|
||||
// Iterate over all chunks in the bundle and create a lookup map
|
||||
for (const file in bundle) {
|
||||
const chunk = bundle[file];
|
||||
if (
|
||||
chunk?.type !== 'chunk' ||
|
||||
!chunk.isEntry ||
|
||||
!chunk.facadeModuleId
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const relativePath = relative(
|
||||
root,
|
||||
sanitizeFileName(chunk.facadeModuleId),
|
||||
);
|
||||
const ext = extname(relativePath);
|
||||
const name = chunk.name.replace(ext, '');
|
||||
|
||||
if (nameMap[name]) {
|
||||
throw new Error(
|
||||
`Entrypoint ${relativePath} conflicts with ${nameMap[name]}`,
|
||||
);
|
||||
}
|
||||
|
||||
nameMap[name] = relativePath;
|
||||
}
|
||||
|
||||
this.emitFile({
|
||||
type: 'asset',
|
||||
fileName: '.vite/manifest-lookup.json',
|
||||
source: JSON.stringify(nameMap, null, 2),
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Taken from https://github.com/rollup/rollup/blob/4f69d33af3b2ec9320c43c9e6c65ea23a02bdde3/src/utils/sanitizeFileName.ts
|
||||
// https://datatracker.ietf.org/doc/html/rfc2396
|
||||
// eslint-disable-next-line no-control-regex
|
||||
const INVALID_CHAR_REGEX = /[\u0000-\u001F"#$%&*+,:;<=>?[\]^`{|}\u007F]/g;
|
||||
|
||||
function sanitizeFileName(name: string): string {
|
||||
return name.replace(INVALID_CHAR_REGEX, '');
|
||||
}
|
||||
Reference in New Issue
Block a user