UNPKG

gatsby-plugin-purgecss

Version:

Gatsby plugin for purgecss. Removes unused css/sass/less/stylus from files and modules. Supports Tailwindcss, Bootstrap, Bulma etc.

72 lines (71 loc) 2.96 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const purgecss_1 = __importDefault(require("purgecss")); const shared_1 = require("./shared"); const debug_1 = require("./debug"); const utils_1 = require("./utils"); // eslint-disable-next-line sonarjs/cognitive-complexity const purgeCSSLoader = function loader(source) { if (Buffer.isBuffer(source)) { console.warn('gatsby-plugin-purgecss: Cannot process raw buffers'); return; } const callback = this.async(); const options = this.getOptions(); if (options.printSummary) { shared_1.stats.addSize(source); } if (Array.isArray(options.ignore) && options.ignore.length > 0) { const normalizedPath = (0, utils_1.normalizePath)(this.resourcePath, this.rootContext); if (options.ignore.some((file) => normalizedPath.includes(file))) { console.log('gatsby-plugin-purgecss: Ignored', this.resourcePath); shared_1.stats.addRemovedSize(source); callback(undefined, source); return; } } if (Array.isArray(options.purgeOnly) && options.purgeOnly.length > 0) { const normalizedPath = (0, utils_1.normalizePath)(this.resourcePath, this.rootContext); if (options.purgeOnly.some((file) => normalizedPath.includes(file))) { console.log('gatsby-plugin-purgecss: Only processing', this.resourcePath); } else { shared_1.stats.addRemovedSize(source); callback(undefined, source); return; } } void new purgecss_1.default() .purge({ ...options.purgeCSSOptions, css: [{ raw: source }], }) .then((result) => { if (options.printSummary) { shared_1.stats.addRemovedSize(result[0].css); } const rejected = result[0].rejected; if (options.printRejected && Array.isArray(rejected)) { const filtered = rejected.map((value) => { return value.replace('\n', ''); }); console.log(utils_1.color.FgGreen, '\nFrom:', this.resourcePath); console.log(utils_1.color.Reset, 'Removed Selectors:', options.printAll ? JSON.stringify(filtered) : filtered); } callback(undefined, result[0].css); }) .catch((error) => { console.log('\ngatsby-plugin-purgecss: Could not parse file, skipping. Your build will not break.\n', this.resourcePath, error); if (options.debug) { (0, debug_1.writeAppendError)(error); } else { console.log('Use debug option to investigate further.'); } callback(error, source); }); }; exports.default = purgeCSSLoader;