UNPKG

kist

Version:

Package Pipeline Processor

165 lines (164 loc) 7.59 kB
"use strict"; // ============================================================================ // Import // ============================================================================ 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); 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.StyleProcessingAction = void 0; const fs_1 = require("fs"); const path_1 = __importDefault(require("path")); const postcss_1 = __importDefault(require("postcss")); const sass = __importStar(require("sass")); const Action_js_1 = require("../../core/pipeline/Action.js"); // Assuming the PostCSS configurations are available at the given paths const postcss_config_compressed_js_1 = __importDefault(require("./postcss.config.compressed.js")); const postcss_config_expanded_js_1 = __importDefault(require("./postcss.config.expanded.js")); // ============================================================================ // Classes // ============================================================================ /** * StyleProcessingAction is a step action responsible for processing styles, * including compiling SCSS and applying PostCSS transformations. It supports * expanded and compressed output styles based on the provided configuration. */ class StyleProcessingAction extends Action_js_1.Action { // Parameters // ======================================================================== // Constructor // ======================================================================== // Methods // ======================================================================== /** * Executes the style processing action. * @param options - The options specific to style processing, including * input/output file paths and style format. * @returns A Promise that resolves when the styles are processed * successfully, or rejects with an error if the action fails. */ execute(options) { return __awaiter(this, void 0, void 0, function* () { const inputFile = options.inputFile; const outputFile = options.outputFile; const styleOption = options.styleOption; if (!inputFile || !outputFile || !styleOption) { throw new Error("Missing required options: inputFile, outputFile, or styleOption."); } this.logInfo(`Processing styles from ${inputFile} to ${outputFile} with ${styleOption} style.`); try { // Ensure the output directory exists const outputDir = path_1.default.dirname(outputFile); yield this.ensureDirectoryExists(outputDir); // Compile SCSS to CSS const result = yield sass.compileAsync(inputFile, { style: styleOption, importers: [new sass.NodePackageImporter()], }); // Process the compiled CSS with PostCSS const processedCss = yield this.processPostCSS(result.css, styleOption); // Write the processed CSS to a file yield fs_1.promises.writeFile(outputFile, processedCss, "utf-8"); this.logInfo(`Styles processed successfully from ${inputFile} to ${outputFile}.`); } catch (error) { this.logError(`Error processing styles from ${inputFile}: ${error}`); throw error; } }); } /** * Processes the given CSS with PostCSS based on the provided style option. * @param css - The CSS string to process. * @param styleOption - The style option, either "expanded" or "compressed". * @returns Processed CSS string. */ processPostCSS(css, styleOption) { return __awaiter(this, void 0, void 0, function* () { const config = styleOption === "expanded" ? postcss_config_expanded_js_1.default : postcss_config_compressed_js_1.default; const result = yield (0, postcss_1.default)(config.plugins).process(css, { from: undefined, map: { inline: false }, }); return result.css; }); } /** * Ensures that the given directory exists, creating it if it does not. * @param dirPath - The path of the directory to check and create. */ ensureDirectoryExists(dirPath) { return __awaiter(this, void 0, void 0, function* () { try { yield fs_1.promises.mkdir(dirPath, { recursive: true }); } catch (error) { if (error instanceof Error) { const nodeError = error; if (nodeError.code !== "EEXIST") { throw nodeError; } } else { throw error; } } }); } /** * Provides a description of the action. * @returns A string description of the action. */ describe() { return "Processes SCSS files into CSS, applying PostCSS transformations for expanded or compressed outputs."; } } exports.StyleProcessingAction = StyleProcessingAction; // ============================================================================ // Export // ============================================================================ // export default StyleProcessingAction;