(config: ResolvedConfig)
| 150 | * Also supports loading plain strings with import text from './foo.txt?raw' |
| 151 | */ |
| 152 | export function assetPlugin(config: ResolvedConfig): Plugin { |
| 153 | registerCustomMime() |
| 154 | |
| 155 | return { |
| 156 | name: 'vite:asset', |
| 157 | |
| 158 | perEnvironmentStartEndDuringDev: true, |
| 159 | |
| 160 | buildStart() { |
| 161 | assetCache.set(this.environment, new Map()) |
| 162 | cssEntriesMap.set(this.environment, new Map()) |
| 163 | }, |
| 164 | |
| 165 | resolveId: { |
| 166 | filter: { |
| 167 | id: [ |
| 168 | urlRE, |
| 169 | DEFAULT_ASSETS_RE, |
| 170 | ...makeIdFiltersToMatchWithQuery(config.rawAssetsInclude).map((v) => |
| 171 | typeof v === 'string' ? picomatch.makeRe(v, { dot: true }) : v, |
| 172 | ), |
| 173 | ], |
| 174 | }, |
| 175 | handler(id) { |
| 176 | if (!config.assetsInclude(cleanUrl(id)) && !urlRE.test(id)) { |
| 177 | return |
| 178 | } |
| 179 | // imports to absolute urls pointing to files in /public |
| 180 | // will fail to resolve in the main resolver. handle them here. |
| 181 | const publicFile = checkPublicFile(id, config) |
| 182 | if (publicFile) { |
| 183 | return id |
| 184 | } |
| 185 | }, |
| 186 | }, |
| 187 | |
| 188 | load: { |
| 189 | filter: { |
| 190 | id: { |
| 191 | include: [ |
| 192 | rawRE, |
| 193 | urlRE, |
| 194 | DEFAULT_ASSETS_RE, |
| 195 | ...makeIdFiltersToMatchWithQuery(config.rawAssetsInclude), |
| 196 | ], |
| 197 | // Rollup convention, this id should be handled by the |
| 198 | // plugin that marked it with \0 |
| 199 | exclude: /^\0/, |
| 200 | }, |
| 201 | }, |
| 202 | async handler(id) { |
| 203 | // raw requests, read from disk |
| 204 | if (rawRE.test(id)) { |
| 205 | const file = checkPublicFile(id, config) || cleanUrl(id) |
| 206 | this.addWatchFile(file) |
| 207 | // raw query, read file and return as string |
| 208 | return { |
| 209 | code: `export default ${JSON.stringify( |
no test coverage detected