( pureCssChunkNames: string[], outputFormat: InternalModuleFormat, )
| 1249 | * @param outputFormat The module output format to decide whether to replace `import` or `require` |
| 1250 | */ |
| 1251 | export function getEmptyChunkReplacer( |
| 1252 | pureCssChunkNames: string[], |
| 1253 | outputFormat: InternalModuleFormat, |
| 1254 | ): (code: string) => string { |
| 1255 | const emptyChunkFiles = pureCssChunkNames |
| 1256 | .map((file) => escapeRegex(path.basename(file))) |
| 1257 | .join('|') |
| 1258 | |
| 1259 | // for cjs, require calls might be chained by minifier using the comma operator. |
| 1260 | // in this case we have to keep one comma if a next require is chained |
| 1261 | // or add a semicolon to terminate the chain. |
| 1262 | const emptyChunkRE = new RegExp( |
| 1263 | outputFormat === 'es' |
| 1264 | ? `\\bimport\\s*["'][^"']*(?:${emptyChunkFiles})["'];` |
| 1265 | : `(\\b|,\\s*)require\\(\\s*["'\`][^"'\`]*(?:${emptyChunkFiles})["'\`]\\)(;|,)`, |
| 1266 | 'g', |
| 1267 | ) |
| 1268 | |
| 1269 | return (code: string) => |
| 1270 | code.replace( |
| 1271 | emptyChunkRE, |
| 1272 | // remove css import while preserving source map location |
| 1273 | (m, p1, p2) => { |
| 1274 | if (outputFormat === 'es') { |
| 1275 | return `/* empty css ${''.padEnd(m.length - 15)}*/` |
| 1276 | } |
| 1277 | if (p2 === ';') { |
| 1278 | // if it ends with `;`, move it before and remove the leading `,` |
| 1279 | return `${p2}/* empty css ${''.padEnd(m.length - 16)}*/` |
| 1280 | } |
| 1281 | // if it ends with `,`, remove it but keep the leading `,` if exists |
| 1282 | return `${p1}/* empty css ${''.padEnd(m.length - 15 - p1.length)}*/` |
| 1283 | }, |
| 1284 | ) |
| 1285 | } |
| 1286 | |
| 1287 | const fileURLWithWindowsDriveRE = /^file:\/\/\/[a-zA-Z]:\// |
| 1288 |
no test coverage detected