Skip to content

Commit 9208efa

Browse files
committed
Report parse errors on polyfill fail
1 parent 117e2bb commit 9208efa

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

src/polyfill.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
type SizingProperty,
3333
} from './syntax.js';
3434
import { transformCSS } from './transform.js';
35+
import { cssParseErrors } from './utils.js';
3536

3637
const platformWithCache = { ...platform, _c: new Map() };
3738

@@ -590,14 +591,37 @@ export async function polyfill(
590591

591592
// fetch CSS from stylesheet and inline style
592593
let styleData = await fetchCSS(options.elements, options.excludeInlineStyles);
593-
594-
// pre parse CSS styles that we need to cascade
595-
const cascadeCausedChanges = cascadeCSS(styleData);
596-
if (cascadeCausedChanges) {
597-
styleData = transformCSS(styleData);
594+
let rules: AnchorPositions = {};
595+
let inlineStyles: Map<HTMLElement, Record<string, string>> | undefined;
596+
try {
597+
// pre parse CSS styles that we need to cascade
598+
const cascadeCausedChanges = cascadeCSS(styleData);
599+
if (cascadeCausedChanges) {
600+
styleData = transformCSS(styleData);
601+
}
602+
// parse CSS
603+
const parsedCSS = await parseCSS(styleData);
604+
rules = parsedCSS.rules;
605+
inlineStyles = parsedCSS.inlineStyles;
606+
} catch (error) {
607+
if (cssParseErrors.length > 0) {
608+
// eslint-disable-next-line no-console
609+
console.group(
610+
`The CSS anchor positioning polyfill was not applied due to ${
611+
cssParseErrors.length === 1
612+
? 'a CSS parse error.'
613+
: 'CSS parse errors'
614+
}.`,
615+
);
616+
cssParseErrors.forEach((err) => {
617+
// eslint-disable-next-line no-console
618+
console.warn(err.formattedMessage);
619+
});
620+
// eslint-disable-next-line no-console
621+
console.groupEnd();
622+
}
623+
throw error;
598624
}
599-
// parse CSS
600-
const { rules, inlineStyles } = await parseCSS(styleData);
601625

602626
if (Object.values(rules).length) {
603627
// update source code

src/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
List,
77
Selector as CssTreeSelector,
88
SelectorList,
9+
SyntaxParseError,
910
Value,
1011
} from 'css-tree';
1112
import generate from 'css-tree/generator';
@@ -17,6 +18,8 @@ import type { Selector } from './dom.js';
1718

1819
export const INSTANCE_UUID = nanoid();
1920

21+
export const cssParseErrors = [] as SyntaxParseError[];
22+
2023
// https://github.com/import-js/eslint-plugin-import/issues/3019
2124

2225
export interface DeclarationWithValue extends Declaration {
@@ -32,9 +35,7 @@ export function getAST(cssText: string) {
3235
parseAtrulePrelude: false,
3336
parseCustomProperty: true,
3437
onParseError: (err) => {
35-
const errorPrelude =
36-
'Invalid CSS could not be parsed. CSS Anchor Positioning Polyfill was not applied.\n\n';
37-
throw new Error(errorPrelude + err.formattedMessage, { cause: err });
38+
cssParseErrors.push(err);
3839
},
3940
});
4041
}

0 commit comments

Comments
 (0)