From 9c6af5f0cc197a37ed2fde2ed499fc50089b4e07 Mon Sep 17 00:00:00 2001 From: Olezhka-web Date: Wed, 27 Sep 2023 11:58:27 +0300 Subject: [PATCH] fix(eleventy-plugin-styles): fixed error handling during style compilation --- .../eleventy-plugin-styles/src/compile.ts | 56 ++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/packages/eleventy-plugin-styles/src/compile.ts b/packages/eleventy-plugin-styles/src/compile.ts index 1704e0f..79451c6 100644 --- a/packages/eleventy-plugin-styles/src/compile.ts +++ b/packages/eleventy-plugin-styles/src/compile.ts @@ -2,13 +2,9 @@ import { readFile } from 'fs/promises'; import { resolve as resolvePath, normalize, sep } from 'path'; import { render } from 'less'; +import { pipe } from '@fluss/core'; import { compile, Options } from 'sass'; -import { pipe, tryExecute } from '@fluss/core'; -import { - oops, - resolve, - trimLeadingPathDelimiter, -} from '@eleventy-packages/common'; +import { oops, trimLeadingPathDelimiter } from '@eleventy-packages/common'; import { PluginState } from './types'; @@ -32,21 +28,27 @@ type Compiler = ( * It adjusts all Sass files into one CSS file. */ const compileSass: Compiler = (options: Options<'sync'>) => async (file) => { - const { css, loadedUrls } = compile(file, { - // Allow import styles from installed packages. - loadPaths: [resolvePath('node_modules')], - ...options, - }); + try { + const { css, loadedUrls } = compile(file, { + // Allow import styles from installed packages. + loadPaths: [resolvePath('node_modules')], + ...options, + }); - return { - css, - urls: loadedUrls.map((url) => - // decodeURI -> non english letters in path - trimLeadingPathDelimiter( - normalize(decodeURI(url.pathname)).replace(process.cwd() + sep, ''), + return { + css, + urls: loadedUrls.map((url) => + // decodeURI -> non english letters in path + trimLeadingPathDelimiter( + normalize(decodeURI(url.pathname)).replace(process.cwd() + sep, ''), + ), ), - ), - }; + }; + } catch (error) { + oops(error as Error); + + return { css: '', urls: [] }; + } }; const extractCssFromLessResult = ({ @@ -67,13 +69,15 @@ const extractCssFromLessResult = ({ * Concats multiple files into one. */ const compileLess: Compiler = (options: Less.Options) => (path: string) => - tryExecute, Error>(() => - pipe((path: string) => - readFile(path, { encoding: 'utf8' }).then( - pipe((data: string) => render(data, options), extractCssFromLessResult), - ), - )(path), - ).extract((error) => (oops(error), resolve({ css: '', urls: [] }))); + readFile(path, { encoding: 'utf8' }) + .then( + pipe((data: string) => render(data, options), extractCssFromLessResult), + ) + .catch((error: Error) => { + oops(error.message); + + return { css: '', urls: [] }; + }); const compileCSS: Compiler = () => (path: string) => readFile(path, { encoding: 'utf8' }).then((css) => ({ css, urls: [] }));