-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
2,253 additions
and
639 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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Added as an optimization for render blocking CSS which is dragging down site vitals: | ||
- extracts CSS surrounded by @critical (determined by one of the available online analysis tools) | ||
- removes the critical CSS from the styles.css bundle rspack/webpack spits out | ||
- adds the critical CSS as an inline style tag to the <head> element of HTML pages | ||
*/ | ||
|
||
const fs = require('fs/promises'); | ||
const path = require('path'); | ||
const glob = require('glob'); | ||
const cheerio = require('cheerio'); | ||
const { default: pMap } = require('p-map'); // Import the CommonJS version | ||
|
||
async function customPostCssPlugin(context, options) { | ||
return { | ||
name: "custom-postcss", | ||
configurePostCss(postCssOptions) { | ||
try { | ||
// adds the postcss-critical-css to current postcss plugin | ||
const postCssCriticalCss = require('postcss-critical-css')(options); | ||
if (!postCssOptions.plugins.includes(postCssCriticalCss)) { | ||
postCssOptions.plugins.push(postCssCriticalCss); | ||
} | ||
// adds the postcss-discard to current postcss plugin | ||
const postCssDiscard = require('postcss-discard')({ | ||
rules: [ | ||
{ | ||
selector: '@critical *' // Targets everything inside @critical | ||
} | ||
] | ||
}); | ||
if (!postCssOptions.plugins.includes(postCssDiscard)) { | ||
postCssOptions.plugins.push(postCssDiscard); | ||
} | ||
} catch (error) { | ||
console.error("Error occurred in custom-postcss configuration:", error); | ||
throw error; // Re-throw the error to stop the build | ||
} | ||
return postCssOptions; | ||
}, | ||
postBuild: async function({ siteConfig = {}, routesPaths = [], outDir }) { | ||
console.log("Running postBuild hook from the customPostCss plugin"); | ||
const criticalCssFile = path.join(outDir, 'assets', 'css', 'critical.css'); // generated in configurePostCss above | ||
try { | ||
const criticalCss = await fs.readFile(criticalCssFile, 'utf8'); | ||
const htmlFiles = findHtmlFiles(outDir); | ||
|
||
await pMap( | ||
htmlFiles, | ||
async (htmlFile) => { | ||
const htmlPath = path.join(outDir, htmlFile); | ||
try { | ||
let htmlContent = await fs.readFile(htmlPath, 'utf8'); | ||
const $ = cheerio.load(htmlContent); // Load HTML with Cheerio | ||
const head = $('head'); | ||
|
||
if (head.length > 0 &&!head.find('style[critical-css]').length) { | ||
head.append(`<style critical-css="">${criticalCss}</style>`); // Append with Cheerio | ||
htmlContent = $.html(); // Get the updated HTML | ||
await fs.writeFile(htmlPath, htmlContent, 'utf8'); | ||
console.log(`Injected critical CSS into ${htmlFile}`); | ||
} else if (head.length === 0) { | ||
console.warn(`Could not find a <head> tag in ${htmlFile}. Critical CSS not injected.`); | ||
} else { | ||
console.log(`Critical CSS already injected into ${htmlFile}. Skipping.`); | ||
} | ||
} catch (err) { | ||
console.error(`Error processing ${htmlFile}:`, err); | ||
} | ||
}, | ||
{ concurrency: 8 } | ||
); | ||
} catch (error) { | ||
console.error("Error processing critical CSS:", error); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
// Helper function to find all HTML files in a directory (recursive) | ||
function findHtmlFiles(outDir) { | ||
return glob.sync('**/*.html', { cwd: outDir }); // Find all .html files recursively | ||
} | ||
|
||
module.exports = customPostCssPlugin |
Oops, something went wrong.