-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: switch to dropcss in AMP example (#10546)
Closes #10536 Switch to using dropcss Add note about removing css in the docs Add note in docs about adding <html amp> as we don't do that anywhere. I also want to discourage people from using ⚡ since dropcss doesn't support it - people definitely will be using it so I've added a safeguard (.replace('⚡', 'amp')) --------- Co-authored-by: Puru Vijay <47742487+PuruVJ@users.noreply.github.com> Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
- Loading branch information
1 parent
7c37a2f
commit d407c75
Showing
6 changed files
with
76 additions
and
389 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,15 @@ | ||
/// <reference types="@sveltejs/kit" /> | ||
|
||
declare module 'dropcss' { | ||
interface Options { | ||
html: string; | ||
css: string; | ||
|
||
shouldDrop?: (selector: string) => boolean; | ||
didRetain?: (selector: string) => void; | ||
|
||
keepText?: boolean; | ||
} | ||
|
||
export default function dropcss(options: Options): { css: string }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,27 @@ | ||
import purify from 'purify-css'; | ||
import * as amp from '@sveltejs/amp'; | ||
import dropcss from 'dropcss'; | ||
|
||
/** @type {import('@sveltejs/kit').Handle} */ | ||
export async function handle({ event, resolve }) { | ||
let buffer = ''; | ||
|
||
const response = await resolve(event, { | ||
return await resolve(event, { | ||
transformPageChunk: ({ html, done }) => { | ||
buffer += html; | ||
|
||
if (done) { | ||
const html = amp.transform(buffer); | ||
|
||
// remove unused CSS | ||
let css = ''; | ||
const markup = html.replace( | ||
/<style amp-custom([^>]*?)>([^]+?)<\/style>/, | ||
(match, attributes, contents) => { | ||
const markup = amp | ||
.transform(buffer) | ||
.replace('⚡', 'amp') | ||
.replace(/<style amp-custom([^>]*?)>([^]+?)<\/style>/, (match, attributes, contents) => { | ||
css = contents; | ||
return `<style amp-custom${attributes}></style>`; | ||
} | ||
); | ||
}); | ||
|
||
css = purify(markup, css); | ||
css = dropcss({ css, html: markup }).css; | ||
return markup.replace('</style>', `${css}</style>`); | ||
} | ||
} | ||
}); | ||
|
||
return response; | ||
} |
Oops, something went wrong.