vite-plugin-purgecss-updated-v5
Version:
Updated vite plugin for removing unused CSS from generated bundles with PurgeCSS for Vite 5 peerDependencies.
81 lines (79 loc) • 2.54 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
cssRegExp: () => cssRegExp,
default: () => index_default,
sourceString: () => sourceString
});
module.exports = __toCommonJS(index_exports);
var import_path = require("path");
var import_purgecss = require("purgecss");
var cssRegExp = /^.*(\.module)?\.css(\?.*)?$/i;
function purgeCssPlugin(opts) {
return {
name: "vite-plugin-purgecss",
enforce: "post",
async generateBundle(_, bundle) {
let content = [];
let css = [];
if (opts != null) {
const { content: optsContent, css: optsCss, ...rest } = opts;
content = optsContent ?? content;
css = optsCss ?? css;
opts = rest;
}
const outputs = {};
for (const id in bundle) {
const file = bundle[id];
const isChunk = file.type === "chunk";
if (isChunk || !cssRegExp.test(file.fileName)) {
content.push({
extension: (0, import_path.extname)(file.fileName).slice(1),
raw: isChunk ? file.code : sourceString(file.source)
});
continue;
}
css.push({ name: id, raw: sourceString(file.source) });
outputs[id] = file;
}
const results = await new import_purgecss.PurgeCSS().purge({
...opts,
content,
css
});
for (const result of results) {
outputs[result.file].source = result.css;
}
}
};
}
function sourceString(source) {
if (typeof source === "string") {
return source;
}
return new TextDecoder().decode(source);
}
var index_default = purgeCssPlugin;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
cssRegExp,
sourceString
});