Skip to content

Commit

Permalink
fix(eleventy-plugin-styles): fixed URL selection with in Critical
Browse files Browse the repository at this point in the history
docs(eleventy-plugin-styles): updated CHANGELOG.MD
  • Loading branch information
Olezhka-web committed Sep 26, 2023
1 parent 8013680 commit 6d635d4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/eleventy-plugin-styles/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

- Added query hash to style URL.

## [1.4.4] - 2021-08-17
## [1.4.4] - 2022-08-17

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions packages/eleventy-plugin-styles/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export const createPublicUrlInjector =
(html: string): string =>
html.replace(originalUrl, `${publicUrl}?${uid()}`);

export const findStyles = (html: string) =>
rip(html, STYLESHEET_LINK_REGEXP, not(isRemoteLink));
export const findStyles = (html: string, regex = STYLESHEET_LINK_REGEXP) =>
rip(html, regex, not(isRemoteLink));

export const bindLinkerWithStyles =
<Options>(linker: Linker<Options>) =>
Expand Down
6 changes: 6 additions & 0 deletions packages/eleventy-plugin-styles/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/** Match stylesheet link and get URL of file from HTML. */
export const STYLESHEET_LINK_REGEXP =
/<link\s+[^>]*href=(?:'|")([^"]+\.(?:css|scss|sass|less))(?:'|")[^>]*>/g;

export const STYLESHEET_LINK_WITH_HASH_REGEXP =
/<link\s+[^>]*href=(?:'|")([^"]+\.(?:css|scss|sass|less)(?:\?\w*)?)(?:'|")[^>]*>/g;

export const CLEAR_STYLE_LINK_REGEXP =
/(.+?\.(?:css|scss|sass|less))\?[a-zA-Z0-9]+/;
12 changes: 11 additions & 1 deletion packages/eleventy-plugin-styles/src/critical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { generate } from 'critical';

import { URL_DELIMITER } from '@eleventy-packages/common';

import { findStyles } from './bundle';
import {
CLEAR_STYLE_LINK_REGEXP,
STYLESHEET_LINK_WITH_HASH_REGEXP,
} from './constants';

/** Object that is passed to rebase function in critical package. */
export interface Asset {
readonly url: string;
Expand Down Expand Up @@ -55,7 +61,11 @@ export const separateCriticalCSS = ({
criticalOptions,
}: CriticalCreatorOptions): Promise<CriticalResult> =>
generate({
html,
html: findStyles(html, STYLESHEET_LINK_WITH_HASH_REGEXP).reduce(
(html, link) =>
html.replace(link, link.match(CLEAR_STYLE_LINK_REGEXP)?.[1] || link),
html,
),
base: buildDirectory,
inline: true,
extract: true,
Expand Down

0 comments on commit 6d635d4

Please sign in to comment.