gatsby-plugin-purgecss
Version:
Gatsby plugin for purgecss. Removes unused css/sass/less/stylus from files and modules. Supports Tailwindcss, Bootstrap, Bulma etc.
98 lines (72 loc) • 3.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = loader;
var _purgecss = _interopRequireDefault(require("purgecss"));
var _loaderUtils = require("loader-utils");
var _shared = require("./shared");
var _utils = require("./utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/**
* Purgecss Loader
* @this {LoaderContext & {rootContext:string}}
* @param {string} source
*/
function loader(source) {
const options =
/** @type {OptionObject} */
(0, _loaderUtils.getOptions)(this);
if (options.rejected) {
_shared.stats.addSize(source);
}
if (Array.isArray(options.ignore) && options.ignore.length > 0) {
const normalizedPath = (0, _utils.normalizePath)(this.resourcePath, this.rootContext);
if (options.ignore.some(file => normalizedPath.includes(file))) {
console.log('\ngatsby-plugin-purgecss: Ignored ', this.resourcePath);
_shared.stats.addRemovedSize(source);
return source;
}
}
if (Array.isArray(options.purgeOnly) && options.purgeOnly.length > 0) {
const normalizedPath = (0, _utils.normalizePath)(this.resourcePath, this.rootContext);
if (options.purgeOnly.some(file => normalizedPath.includes(file))) {
console.log('\ngatsby-plugin-purgecss: Only processing ', this.resourcePath);
} else {
_shared.stats.addRemovedSize(source);
return source;
}
}
let css;
try {
// @ts-ignore
css = new _purgecss.default(_objectSpread({
css: [{
raw: source
}]
}, options)).purge();
} catch (error) {
console.log('\ngatsby-plugin-purgecss: Could not parse file, skipping. Your build will not break.\n', this.resourcePath);
if (options.debug) {
_shared.Debug.writeAppendError(error);
} else {
console.log('Use debug option to investigate further.');
}
return source;
}
if (options.rejected) {
const rejected = css[0].rejected;
_shared.stats.add(rejected.length);
_shared.stats.addRemovedSize(css[0].css);
if (options.printRejected && Array.isArray(rejected)) {
const filtered = rejected.map(val => {
return val.replace('\n', '');
});
console.log(_utils.color.FgGreen, '\nFrom: ', this.resourcePath);
console.log(_utils.color.Reset, 'Removed Selectors: ', options.printAll ? JSON.stringify(filtered) : filtered);
}
}
return css[0].css;
}