| 101 | export const htmlProxyResult: Map<string, string> = new Map() |
| 102 | |
| 103 | export function htmlInlineProxyPlugin(config: ResolvedConfig): Plugin { |
| 104 | // Should do this when `constructor` rather than when `buildStart`, |
| 105 | // `buildStart` will be triggered multiple times then the cached result will be emptied. |
| 106 | // https://github.com/vitejs/vite/issues/6372 |
| 107 | htmlProxyMap.set(config, new Map()) |
| 108 | return { |
| 109 | name: 'vite:html-inline-proxy', |
| 110 | |
| 111 | resolveId: { |
| 112 | filter: { id: isHtmlProxyRE }, |
| 113 | handler(id) { |
| 114 | return id |
| 115 | }, |
| 116 | }, |
| 117 | |
| 118 | load: { |
| 119 | filter: { id: isHtmlProxyRE }, |
| 120 | handler(id) { |
| 121 | const proxyMatch = htmlProxyRE.exec(id) |
| 122 | if (proxyMatch) { |
| 123 | const index = Number(proxyMatch[1]) |
| 124 | const file = cleanUrl(id) |
| 125 | const url = file.replace(normalizePath(config.root), '') |
| 126 | const result = htmlProxyMap.get(config)!.get(url)?.[index] |
| 127 | if (result) { |
| 128 | // set moduleSideEffects to keep the module even if `treeshake.moduleSideEffects=false` is set |
| 129 | return { ...result, moduleSideEffects: true } |
| 130 | } else { |
| 131 | throw new Error(`No matching HTML proxy module found from ${id}`) |
| 132 | } |
| 133 | } |
| 134 | }, |
| 135 | }, |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | export function addToHTMLProxyCache( |
| 140 | config: ResolvedConfig, |