ngx-unused-css
Version:
Detect unused CSS in angular components
83 lines • 3.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Main = void 0;
var tslib_1 = require("tslib");
var chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
var path_1 = (0, tslib_1.__importDefault)(require("path"));
var table_1 = require("table");
var constants_1 = require("./constants");
var getUnusedClasses_1 = (0, tslib_1.__importDefault)(require("./main/getUnusedClasses"));
var Main = /** @class */ (function () {
function Main(config) {
var _this = this;
this.config = config;
if (!config.styleExt) {
config.styleExt = constants_1.DEFAULT_STYLE_EXTENSION;
}
// Execute run immediately if running via CLI
if (this.config.cli) {
this.run()
.then(function (r) {
var res = r.css;
if (r.globalCss.length > 0) {
res.push([r.globalCss, '***** GLOBAL UNUSED CSS *****']);
}
if (res.length > 0) {
_this.log(res);
}
else {
console.log('No duplicate classes were found!');
}
})
.catch(function (err) {
throw new Error(err);
});
}
}
Main.prototype.run = function () {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
var unusedClasses, css, globalCss, _a, e_1;
return (0, tslib_1.__generator)(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 4, , 5]);
unusedClasses = new getUnusedClasses_1.default(this.config);
return [4 /*yield*/, unusedClasses.getUnusedClasses(this.config.path)];
case 1:
css = _b.sent();
_a = this.config.globalStyles;
if (!_a) return [3 /*break*/, 3];
return [4 /*yield*/, unusedClasses.getGlobalUnusedClasses(this.config.globalStyles)];
case 2:
_a = (_b.sent());
_b.label = 3;
case 3:
globalCss = (_a) ||
[];
return [2 /*return*/, { css: css, globalCss: globalCss }];
case 4:
e_1 = _b.sent();
console.log('e', e_1, path_1.default.resolve(''));
throw new Error(e_1);
case 5: return [2 /*return*/];
}
});
});
};
Main.prototype.log = function (classes) {
var result = chalk_1.default.red.bold('Unused CSS classes were found for the following files:\n\n');
classes.forEach(function (e) {
var htmlPath = e[1];
var cssPath = e[1].replace('.html', '.scss');
result += chalk_1.default.red(htmlPath) + '\n';
result += chalk_1.default.red.bold(cssPath) + '\n';
var cssClasses = e[0].join('\n');
result += (0, table_1.table)([[chalk_1.default.green(cssClasses)]]);
});
console.log(result);
process.exit(1);
};
return Main;
}());
exports.Main = Main;
//# sourceMappingURL=main.js.map