Skip to content

Commit

Permalink
Update StyleProcessor.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvianen committed Sep 7, 2024
1 parent 9d8f0ac commit 10eacb2
Showing 1 changed file with 27 additions and 86 deletions.
113 changes: 27 additions & 86 deletions src/ts/sass/StyleProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ class StyleProcessor {
*/
async processPostCSS(css: string, styleOption: "expanded" | "compressed") {
const config = styleOption === "expanded" ? postcssConfigExpanded : postcssConfigCompressed;
const result = await postcss(config.plugins).process(css, { from: undefined, map: { inline: false } });
const result = await postcss(config.plugins).process(
css,
{ from: undefined, map: { inline: false } }
);
return result.css;
}

/**
* Ensures that the given directory exists. Creates it if it does not exist.
* Ensures that the given directory exists. Creates it if it does not
* exist.
* @param dirPath The path of the directory to check and create.
*/
private async ensureDirectoryExists(dirPath: string): Promise<void> {
Expand All @@ -48,10 +52,12 @@ class StyleProcessor {
if (error instanceof Error) {
const nodeError = error as NodeJS.ErrnoException;
if (nodeError.code !== "EEXIST") {
throw nodeError; // Rethrow if it"s not a "directory exists" error
// Rethrow if it"s not a "directory exists" error
throw nodeError;
}
} else {
throw error; // Rethrow if it"s not an Error instance
// Rethrow if it"s not an Error instance
throw error;
}
}
}
Expand All @@ -60,19 +66,30 @@ class StyleProcessor {
* Compiles SCSS to CSS and processes it using PostCSS.
* @param inputFile Path to the input SCSS file.
* @param outputFile Path to the output CSS file.
* @param styleOption Style option for the output, either "expanded" or "compressed".
* @param styleOption Style option for the output, either "expanded" or
* "compressed".
*/
async processStyles(inputFile: string, outputFile: string, styleOption: "expanded" | "compressed") {
async processStyles(
inputFile: string,
outputFile: string,
styleOption: "expanded" | "compressed"
) {
try {
// Ensure the output directory exists
const outputDir = path.dirname(outputFile);
await this.ensureDirectoryExists(outputDir);

// Compile SCSS to CSS
const result = await sass.compileAsync(inputFile, { style: styleOption });
const result = await sass.compileAsync(
inputFile,
{ style: styleOption }
);

// Process the compiled CSS with PostCSS
const processedCss = await this.processPostCSS(result.css, styleOption);
const processedCss = await this.processPostCSS(
result.css,
styleOption
);

// Write the processed CSS to a file
await fs.writeFile(outputFile, processedCss, "utf-8");
Expand All @@ -82,89 +99,13 @@ class StyleProcessor {

} catch (err) {
console.error(`Error processing styles from ${inputFile}:`, err);
throw err; // Re-throw the error for further handling if necessary
// Re-throw the error for further handling if necessary
throw err;
}
}
}


// /**
// * Class responsible for processing styles, including compiling SCSS and
// * applying PostCSS transformations.
// */
// class StyleProcessor {

// /**
// * 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.
// */
// async processPostCSS(
// css: string,
// styleOption: "expanded" | "compressed"
// ) {
// const config = styleOption === "expanded" ? postcssConfigExpanded : postcssConfigCompressed;
// return postcss(config.plugins).process(css, { from: undefined, map: { inline: false } });
// }

// /**
// * Ensures that the given directory exists. Creates it if it does not exist.
// * @param dirPath - The path of the directory to check and create.
// */
// private async ensureDirectoryExists(dirPath: string): Promise<void> {
// try {
// await fsPromises.mkdir(dirPath, { recursive: true });
// } catch (error) {
// if (error.code !== "EEXIST") {
// throw error;
// }
// }
// }

// /**
// * Compiles SCSS to CSS and processes it using PostCSS.
// * @param inputFile Path to the input SCSS file.
// * @param outputFile Path to the output CSS file.
// * @param styleOption Style option for the output.
// */
// async processStyles(
// inputFile: string,
// outputFile: fs.PathOrFileDescriptor,
// styleOption: "expanded" | "compressed"
// ) {
// try {

// // Ensure the output directory exists
// const outputDir = path.dirname(outputFile as string);
// await this.ensureDirectoryExists(outputDir);

// // Compile SCSS to CSS
// const result = await sass.compileAsync(
// inputFile, { style: styleOption }
// );

// // Process the compiled CSS with PostCSS and Autoprefixer
// const processed = await this.processPostCSS(
// result.css,
// styleOption
// );

// // Write the processed CSS to a file
// fs.writeFileSync(outputFile, processed.css);

// // Write the source map file
// if (processed.map) {
// fs.writeFileSync(`${outputFile}.map`, processed.map.toString());
// }
// } catch (err) {
// // Handle errors in the compilation or processing
// console.error(`Error processing styles from ${inputFile}:`, err);
// }
// }
// }


// ============================================================================
// Export
// ============================================================================
Expand Down

0 comments on commit 10eacb2

Please sign in to comment.