-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Expand file tree
/
Copy pathid-prefix-plugin.ts
More file actions
71 lines (61 loc) · 2.54 KB
/
Copy pathid-prefix-plugin.ts
File metadata and controls
71 lines (61 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import type { Plugin } from 'vite' with {
'resolution-mode': 'import',
};
// NOTE: the implementation for this Vite plugin is roughly based on:
// https://github.com/MilanKovacic/vite-plugin-externalize-dependencies
const VITE_ID_PREFIX = '@id/';
const escapeRegexSpecialChars = (inputString: string): string => {
return inputString.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
};
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 transformFn = createTransformer(resolvedConfig.base, externals);
// @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: transformFn,
});
},
};
}
/**
* Creates a transform function that removes the Vite ID prefix from externals.
* @param base The base path of the application.
* @param externals The external package names.
* @returns A function that transforms code by removing the Vite ID prefix.
*/
export function createTransformer(base: string, externals: string[]): (code: string) => string {
// The path suffix is bounded so that a match can never extend past the end of an
// import specifier string literal. With a greedy `.+`, minified (single-line) code
// would let the first match consume the remainder of the line, leaving all later
// `/@id/` occurrences on that line unstripped.
const escapedExternals = externals.map((e) => escapeRegexSpecialChars(e) + '(?:/[^\'"`\\s]+)?');
const prefixedExternalRegex = new RegExp(
`${base}${VITE_ID_PREFIX}(${escapedExternals.join('|')})`,
'g',
);
return (code: string) => {
return code.includes(VITE_ID_PREFIX)
? code.replace(prefixedExternalRegex, (_, externalName) => externalName)
: // don't do anything when code does not contain the Vite prefix
code;
};
}