automin
Version:
"Create a minified mirror version of your js, css, html, json files"
90 lines (89 loc) • 3.65 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const env_res_1 = __importDefault(require("env-res"));
const get_files_1 = __importDefault(require("./components/get-files"));
const outputs_1 = require("./helpers/outputs");
const MINIFIERS = __importStar(require("./components/minifier"));
const path_1 = require("path");
const minify_1 = __importDefault(require("./components/minify"));
const just_copy_1 = __importDefault(require("./components/just-copy"));
const cache_1 = require("./components/cache");
const FORCE_EXCLUDE = [
".git",
"node_modules"
];
// Start
const availableTypes = Object.keys(MINIFIERS).map((x) => "." + x);
exports.default = () => {
// For Name cache featured by uglify-js
(0, cache_1.load_cache)("uglify-js-name-cache");
console.log("Scanning directories...");
let scanned_count = 0;
const to_copy = [];
// input and output directories can be change depending on validators
const files = (0, get_files_1.default)(env_res_1.default.get('input'), {
filter: (fpath, fname) => {
(0, outputs_1.print)(fpath.replace(env_res_1.default.get('input'), ""), {
overwrite: true
});
scanned_count++;
return true;
},
filter_folders: (fpath, filename) => {
filename = filename.toLowerCase();
return !FORCE_EXCLUDE.some((value) => value.toLowerCase() == filename);
},
filter_files: (fpath, fname) => {
fname = fname.toLowerCase();
let ext = (0, path_1.extname)(fname);
// Skip all files with .min. before extension
if (fname.endsWith(".min" + ext)) {
to_copy.push(fpath);
return false;
}
if (availableTypes.indexOf(ext) == -1) {
to_copy.push(fpath);
return false;
}
return true;
}
});
(0, outputs_1.print)(`${scanned_count} files has been scanned`, {
overwrite: true
});
console.log(`${files.length} files is about to minify`);
// Copy files that does not have available minifier
(0, just_copy_1.default)(to_copy);
// Try to minify files that has support
(0, minify_1.default)(files);
// Save nameCache as json file after processing
// Can be corrupted if the process has been
// terminated while working...
(0, cache_1.save_cache)("uglify-js-name-cache");
};