* * @param options - options * @returns * * @public
(options: UserDefinedOptions)
| 14 | * @public |
| 15 | */ |
| 16 | function pluginPurgeCSS(options: UserDefinedOptions): Plugin { |
| 17 | const filter = createFilter( |
| 18 | options.include || ["**/*.css"], |
| 19 | options.exclude || "node_modules/**", |
| 20 | ); |
| 21 | |
| 22 | const styles: string[] = []; |
| 23 | let dest = ""; |
| 24 | |
| 25 | return { |
| 26 | name: "purgecss", |
| 27 | transform: async (code, id) => { |
| 28 | if (!filter(id)) return null; |
| 29 | |
| 30 | const v = await new PurgeCSS().purge({ |
| 31 | content: options.content, |
| 32 | css: [ |
| 33 | { |
| 34 | raw: code, |
| 35 | }, |
| 36 | ], |
| 37 | }); |
| 38 | let css = v[0].css; |
| 39 | |
| 40 | styles.push(css); |
| 41 | |
| 42 | css = JSON.stringify(css); |
| 43 | if (options.insert) { |
| 44 | // do thing |
| 45 | } else if (!options.output) { |
| 46 | code = css; |
| 47 | } else { |
| 48 | code = `"";`; |
| 49 | } |
| 50 | |
| 51 | return { |
| 52 | code: `export default ${code}`, |
| 53 | map: { mappings: "" }, |
| 54 | }; |
| 55 | }, |
| 56 | generateBundle() { |
| 57 | if (!options.insert && (!styles.length || options.output === false)) { |
| 58 | return; |
| 59 | } |
| 60 | const css = styles.reduce((acc, value) => { |
| 61 | return acc + value; |
| 62 | }, ""); |
| 63 | if (typeof options.output === "string") { |
| 64 | return fs.writeFileSync(options.output, css); |
| 65 | } |
| 66 | if (typeof options.output === "function") { |
| 67 | return options.output(css, styles); |
| 68 | } |
| 69 | if (!options.insert && dest) { |
| 70 | if (dest.endsWith(".js") || dest.endsWith(".ts")) { |
| 71 | dest = dest.slice(0, -3); |
| 72 | } |
| 73 | dest = `${dest}.css`; |
nothing calls this directly
no test coverage detected
searching dependent graphs…