| 294 | * Plugin applied before user plugins |
| 295 | */ |
| 296 | export function cssPlugin(config: ResolvedConfig): Plugin { |
| 297 | const isBuild = config.command === 'build' |
| 298 | let moduleCache: Map<string, Record<string, string>> |
| 299 | |
| 300 | const idResolver = createBackCompatIdResolver(config, { |
| 301 | preferRelative: true, |
| 302 | tryIndex: false, |
| 303 | extensions: [], |
| 304 | }) |
| 305 | |
| 306 | let preprocessorWorkerController: PreprocessorWorkerController | undefined |
| 307 | |
| 308 | // warm up cache for resolved postcss config |
| 309 | if (config.css.transformer !== 'lightningcss') { |
| 310 | resolvePostcssConfig(config).catch(() => { |
| 311 | /* will be handled later */ |
| 312 | }) |
| 313 | } |
| 314 | |
| 315 | return { |
| 316 | name: 'vite:css', |
| 317 | |
| 318 | buildStart() { |
| 319 | // Ensure a new cache for every build (i.e. rebuilding in watch mode) |
| 320 | moduleCache = new Map<string, Record<string, string>>() |
| 321 | cssModulesCache.set(config, moduleCache) |
| 322 | |
| 323 | removedPureCssFilesCache.set(config, new Map<string, RenderedChunk>()) |
| 324 | |
| 325 | preprocessorWorkerController = createPreprocessorWorkerController( |
| 326 | normalizeMaxWorkers(config.css.preprocessorMaxWorkers), |
| 327 | ) |
| 328 | preprocessorWorkerControllerCache.set( |
| 329 | config, |
| 330 | preprocessorWorkerController, |
| 331 | ) |
| 332 | }, |
| 333 | |
| 334 | async buildEnd() { |
| 335 | await preprocessorWorkerController?.close() |
| 336 | }, |
| 337 | |
| 338 | load: { |
| 339 | filter: { |
| 340 | id: CSS_LANGS_RE, |
| 341 | }, |
| 342 | handler(id) { |
| 343 | if (urlRE.test(id)) { |
| 344 | if (isModuleCSSRequest(id)) { |
| 345 | throw new Error( |
| 346 | `?url is not supported with CSS modules. (tried to import ${JSON.stringify( |
| 347 | id, |
| 348 | )})`, |
| 349 | ) |
| 350 | } |
| 351 | |
| 352 | // *.css?url |
| 353 | // in dev, it's handled by assets plugin. |