refactor: rename internal vite plugin name.

Follow convention.
This commit is contained in:
Alan Agius 2024-07-16 12:21:09 +00:00 committed by Alan Agius
parent b168948687
commit f5c250ab48

View File

@ -17,36 +17,38 @@ const escapeRegexSpecialChars = (inputString: string): string => {
return inputString.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string return inputString.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}; };
export const createRemoveIdPrefixPlugin = (externals: string[]): Plugin => ({ export function createRemoveIdPrefixPlugin(externals: string[]): Plugin {
name: 'vite-plugin-remove-id-prefix', return {
apply: 'serve', name: 'angular-plugin-remove-id-prefix',
configResolved: (resolvedConfig) => { apply: 'serve',
// don't do anything when the list of externals is empty configResolved: (resolvedConfig) => {
if (externals.length === 0) { // don't do anything when the list of externals is empty
return; if (externals.length === 0) {
} return;
}
const escapedExternals = externals.map(escapeRegexSpecialChars); const escapedExternals = externals.map(escapeRegexSpecialChars);
const prefixedExternalRegex = new RegExp( const prefixedExternalRegex = new RegExp(
`${VITE_ID_PREFIX}(${escapedExternals.join('|')})`, `${VITE_ID_PREFIX}(${escapedExternals.join('|')})`,
'g', 'g',
); );
// @ts-expect-error: Property 'push' does not exist on type 'readonly Plugin<any>[]' // @ts-expect-error: Property 'push' does not exist on type 'readonly Plugin<any>[]'
// Reasoning: // Reasoning:
// since the /@id/ prefix is added by Vite's import-analysis plugin, // since the /@id/ prefix is added by Vite's import-analysis plugin,
// we must add our actual plugin dynamically, to ensure that it will run // we must add our actual plugin dynamically, to ensure that it will run
// AFTER the import-analysis. // AFTER the import-analysis.
resolvedConfig.plugins.push({ resolvedConfig.plugins.push({
name: 'vite-plugin-remove-id-prefix-transform', name: 'angular-plugin-remove-id-prefix-transform',
transform: (code: string) => { transform: (code: string) => {
// don't do anything when code does not contain the Vite prefix // don't do anything when code does not contain the Vite prefix
if (!code.includes(VITE_ID_PREFIX)) { if (!code.includes(VITE_ID_PREFIX)) {
return code; return code;
} }
return code.replace(prefixedExternalRegex, (_, externalName) => externalName); return code.replace(prefixedExternalRegex, (_, externalName) => externalName);
}, },
}); });
}, },
}); };
}