kist
Version:
Package Pipeline Processor
99 lines (98 loc) • 4.89 kB
JavaScript
;
// ============================================================================
// Import
// ============================================================================
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JavaScriptMinifyAction = void 0;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const terser_1 = require("terser");
const Action_1 = require("../../core/pipeline/Action");
const terser_config_js_1 = __importDefault(require("./terser.config.js"));
// ============================================================================
// Classes
// ============================================================================
/**
* JavaScriptMinifyAction handles the minification of JavaScript files.
* Uses Terser to reduce file size and optimize performance.
*/
class JavaScriptMinifyAction extends Action_1.Action {
/**
* Executes the JavaScript minification action.
*
* @param options - The options containing input and output file paths.
* @returns A Promise that resolves when the minification process completes.
* @throws {Error} If input file is missing, minification fails, or output cannot be written.
*/
execute(options) {
return __awaiter(this, void 0, void 0, function* () {
const { inputPath, outputPath, customConfig = {} } = options;
if (!inputPath || !outputPath) {
throw new Error("Invalid options: 'inputPath' and 'outputPath' are required.");
}
this.logInfo(`Minifying JavaScript file: ${inputPath} → ${outputPath}`);
try {
yield this.minifyFile(inputPath, outputPath, customConfig);
this.logInfo(`JavaScript minification completed: ${outputPath}`);
}
catch (error) {
this.logError("JavaScript minification failed.", error);
throw error;
}
});
}
/**
* Minifies a JavaScript file using Terser.
*
* @param inputPath - Path to the input JavaScript file.
* @param outputPath - Path where the minified file will be saved.
* @param customConfig - Custom Terser configuration.
* @returns A Promise that resolves when the minification is complete.
*/
minifyFile(inputPath, outputPath, customConfig) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
try {
const resolvedInputPath = path_1.default.resolve(inputPath);
const resolvedOutputPath = path_1.default.resolve(outputPath);
// Read JavaScript file
const inputCode = yield fs_1.promises.readFile(resolvedInputPath, "utf8");
// Merge Terser configuration with explicit type casting
const terserOptions = Object.assign(Object.assign(Object.assign({}, terser_config_js_1.default), customConfig), { ecma: terser_config_js_1.default.ecma, nameCache: (_a = terser_config_js_1.default.nameCache) !== null && _a !== void 0 ? _a : undefined });
// Minify using Terser
const result = yield (0, terser_1.minify)(inputCode, terserOptions);
if (!result.code) {
throw new Error("Minification resulted in empty output.");
}
// Write minified file
yield fs_1.promises.writeFile(resolvedOutputPath, result.code, "utf8");
this.logDebug(`Minified JavaScript file saved to ${resolvedOutputPath}`);
}
catch (error) {
this.logError(`Error minifying JavaScript file: ${inputPath}`, error);
throw error;
}
});
}
/**
* Provides a description of the action.
*
* @returns A string description of the action.
*/
describe() {
return "Minifies JavaScript files using Terser to reduce size and optimize performance.";
}
}
exports.JavaScriptMinifyAction = JavaScriptMinifyAction;