Skip to content

Commit

Permalink
refactor: rename internal vite plugin name.
Browse files Browse the repository at this point in the history
Follow convention.
  • Loading branch information
alan-agius4 committed Jul 16, 2024
1 parent b168948 commit f5c250a
Showing 1 changed file with 35 additions and 33 deletions.
68 changes: 35 additions & 33 deletions packages/angular/build/src/tools/vite/id-prefix-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,38 @@ const escapeRegexSpecialChars = (inputString: string): string => {
return inputString.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
};

export const createRemoveIdPrefixPlugin = (externals: string[]): Plugin => ({
name: 'vite-plugin-remove-id-prefix',
apply: 'serve',
configResolved: (resolvedConfig) => {
// don't do anything when the list of externals is empty
if (externals.length === 0) {
return;
}

const escapedExternals = externals.map(escapeRegexSpecialChars);
const prefixedExternalRegex = new RegExp(
`${VITE_ID_PREFIX}(${escapedExternals.join('|')})`,
'g',
);

// @ts-expect-error: Property 'push' does not exist on type 'readonly Plugin<any>[]'
// Reasoning:
// 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
// AFTER the import-analysis.
resolvedConfig.plugins.push({
name: 'vite-plugin-remove-id-prefix-transform',
transform: (code: string) => {
// don't do anything when code does not contain the Vite prefix
if (!code.includes(VITE_ID_PREFIX)) {
return code;
}

return code.replace(prefixedExternalRegex, (_, externalName) => externalName);
},
});
},
});
export function createRemoveIdPrefixPlugin(externals: string[]): Plugin {
return {
name: 'angular-plugin-remove-id-prefix',
apply: 'serve',
configResolved: (resolvedConfig) => {
// don't do anything when the list of externals is empty
if (externals.length === 0) {
return;
}

const escapedExternals = externals.map(escapeRegexSpecialChars);
const prefixedExternalRegex = new RegExp(
`${VITE_ID_PREFIX}(${escapedExternals.join('|')})`,
'g',
);

// @ts-expect-error: Property 'push' does not exist on type 'readonly Plugin<any>[]'
// Reasoning:
// 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
// AFTER the import-analysis.
resolvedConfig.plugins.push({
name: 'angular-plugin-remove-id-prefix-transform',
transform: (code: string) => {
// don't do anything when code does not contain the Vite prefix
if (!code.includes(VITE_ID_PREFIX)) {
return code;
}

return code.replace(prefixedExternalRegex, (_, externalName) => externalName);
},
});
},
};
}

0 comments on commit f5c250a

Please sign in to comment.