Skip to content

Commit

Permalink
fix(eleventy-plugin-styles): fixed error handling during style compil…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
Olezhka-web committed Sep 27, 2023
1 parent 6d635d4 commit 9c6af5f
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions packages/eleventy-plugin-styles/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -32,21 +28,27 @@ type Compiler = <Options extends object>(
* 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 = ({
Expand All @@ -67,13 +69,15 @@ const extractCssFromLessResult = ({
* Concats multiple files into one.
*/
const compileLess: Compiler = (options: Less.Options) => (path: string) =>
tryExecute<Promise<CompilerResult>, 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: [] }));
Expand Down

0 comments on commit 9c6af5f

Please sign in to comment.