* * @param options - options * @returns * * @public
(options: UserDefinedOptions)
| 25 | * @public |
| 26 | */ |
| 27 | function gulpPurgeCSS(options: UserDefinedOptions): internal.Transform { |
| 28 | return through.obj(async function (file: VinylFile, _encoding, callback) { |
| 29 | // empty |
| 30 | if (file.isNull()) return callback(null, file); |
| 31 | // buffer |
| 32 | if (file.isBuffer()) { |
| 33 | try { |
| 34 | const optionsGulp = { |
| 35 | ...options, |
| 36 | content: getFiles(options.content, options.skippedContentGlobs), |
| 37 | css: [ |
| 38 | { |
| 39 | raw: file.contents.toString(), |
| 40 | }, |
| 41 | ], |
| 42 | stdin: true, |
| 43 | sourceMap: !!file.sourceMap, |
| 44 | }; |
| 45 | const purgedCSSResults = await new PurgeCSS().purge(optionsGulp); |
| 46 | const purge = purgedCSSResults[0]; |
| 47 | const result = |
| 48 | optionsGulp.rejected && purge.rejected |
| 49 | ? purge.rejected.join(" {}\n") + " {}" |
| 50 | : purge.css; |
| 51 | file.contents = Buffer.from(result, "utf-8"); |
| 52 | |
| 53 | // apply source map to the chain |
| 54 | if (file.sourceMap) { |
| 55 | applySourceMap(file, purge.sourceMap); |
| 56 | } |
| 57 | callback(null, file); |
| 58 | } catch (e: unknown) { |
| 59 | if (e instanceof Error) { |
| 60 | this.emit("error", new PluginError(PLUGIN_NAME, e.message)); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | // stream |
| 65 | if (file.isStream()) { |
| 66 | let css = ""; |
| 67 | file.contents |
| 68 | .on("data", (buffer: string | Buffer) => { |
| 69 | css += buffer.toString(); |
| 70 | }) |
| 71 | .on("end", async () => { |
| 72 | try { |
| 73 | const optionsGulp = { |
| 74 | ...options, |
| 75 | css: [ |
| 76 | { |
| 77 | raw: css, |
| 78 | }, |
| 79 | ], |
| 80 | sourceMap: !!file.sourceMap, |
| 81 | }; |
| 82 | |
| 83 | const purgedCSSResults = await new PurgeCSS().purge(optionsGulp); |
| 84 | const purge = purgedCSSResults[0]; |