import { Rule } from 'postcss'; import { ChildNode } from 'domhandler'; /** * Copyright 2018 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ type SplitIterator = (item: T, index: number, a: T[], b?: T[]) => boolean; declare function filterSelectors(this: Rule, predicate: SplitIterator): void; declare module 'postcss' { interface Node { _other?: Rule; $$remove?: boolean; $$markedSelectors?: string[]; filterSelectors?: typeof filterSelectors; } } /** * Copyright 2018 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ declare module 'domhandler' { interface Node { nodeName: string; id: string; className: string; insertBefore: (child: ChildNode, referenceNode: ChildNode | null) => ChildNode; appendChild: (child: ChildNode) => ChildNode; removeChild: (child: ChildNode) => void; remove: () => void; textContent: string; setAttribute: (name: string, value: string) => void; removeAttribute: (name: string) => void; getAttribute: (name: string) => string; hasAttribute: (name: string) => boolean; getAttributeNode: (name: string) => undefined | { specified: true; value: string; }; exists: (sel: string) => boolean; querySelector: (sel: string) => Node; querySelectorAll: (sel: string) => Node[]; $$external?: boolean; $$name?: string; $$reduce?: boolean; $$links?: ChildNode[]; } } declare class Beasties { #selectorCache = /* @__PURE__ */ new Map(); options; logger; fs; constructor(options = {}) { this.options = Object.assign({ logLevel: "info", path: "", publicPath: "", reduceInlineStyles: true, pruneSource: false, additionalStylesheets: [], allowRules: [] }, options); this.logger = this.options.logger || createLogger(this.options.logLevel); } /** * Read the contents of a file from the specified filesystem or disk */ readFile(filename) { const fs = this.fs; return new Promise((resolve, reject) => { const callback = (err, data) => { if (err) reject(err); else resolve(data.toString()); }; if (fs && fs.readFile) { fs.readFile(filename, callback); } else { readFile(filename, "utf-8", callback); } }); } /** * Write content to a file */ writeFile(filename, data) { const fs = this.fs; return new Promise((resolve, reject) => { const callback = (err) => { if (err) reject(err); else resolve(); }; if (fs && fs.writeFile) { fs.writeFile(filename, data, callback); } else { writeFile(filename, data, callback); } }); } /** * Apply critical CSS processing to the html */ async process(html) { const start = Date.now(); const document = createDocument(html); if (this.options.additionalStylesheets.length > 0) { await this.embedAdditionalStylesheet(document); } if (this.options.external !== false) { const externalSheets = [...document.querySelectorAll('link[rel="stylesheet"]')]; const hasCustomEmbed = this.embedLinkedStylesheet !== Beasties.prototype.embedLinkedStylesheet; if (hasCustomEmbed) { for (const link of externalSheets) { await this.embedLinkedStylesheet(link, document); } } else { const sheets = await Promise.all( externalSheets.map((link) => this.fetchStylesheet(link, document)) ); for (const sheet of sheets) { if (sheet) { this.embedFetchedStylesheet(sheet, document); } } } } const styles = this.getAffectedStyleTags(document); for (const style of styles) { this.processStyle(style, document); } if (this.options.mergeStylesheets !== false && styles.length !== 0) { this.mergeStylesheets(document); } const output = serializeDocument(document); const end = Date.now(); this.logger.info?.(`Time ${end - start}ms`); return output; } /** * Get the style tags that need processing */ getAffectedStyleTags(document) { const styles = [...document.querySelectorAll("style")]; if (this.options.reduceInlineStyles === false) { return styles.filter((style) => style.$$external); } return styles; } mergeStylesheets(document) { const styles = this.getAffectedStyleTags(document); if (styles.length === 0) { this.logger.warn?.( "Merging inline stylesheets into a single