Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
updated optimize script
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Mar 13, 2024
1 parent 5a3aac7 commit 4370d82
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/deploy-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ jobs:
- name: Build the website
run: npm run build

- name: Optimize static content
run: npm run optimize

- name: Creating symlinks to old version
run: |
ln -s ../nightwatchjs.org out/v17
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ jobs:
- name: Install dependencies
run: npm install

- name: Prebuild #required ensure api pages are generated before the ejs files are processed
run: npm run prebuild

- name: Build the website
run: npm run build

- name: Optimize static content
run: npm run optimize

- name: Creating symlinks to old version
run: |
ln -s ../nightwatchjs.org out/v17
Expand Down
54 changes: 54 additions & 0 deletions build/optimize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {generate} from 'critical';
import {minify} from 'html-minifier';
import {readFileSync, writeFileSync} from 'fs';
import path from 'path';

// Inline critical CSS
generate({
inline: true,
base: 'out/',
src: 'index.html',
target: {
html: 'index.html',
uncritical: 'css/landing/style.css'
},
dimensions: [
{
height: 200,
width: 500
},
{
height: 900,
width: 1024
},
{
height: 900,
width: 1400
}
],
// ignore CSS rules
ignoreInlinedStyles: true
}).then(({css, html, uncritical}) => {
console.log('Critical CSS generated', css.length);
console.log('Uncritical CSS generated', uncritical.length);
}).catch(err => {
console.error('Critical CSS generation failed', err);
}).then(() => {
// Minify HTML
const htmlPath = path.resolve('out/index.html');
const htmlContent = readFileSync(htmlPath, 'utf8');
const minifiedHTMLContent = minify(htmlContent, {
collapseWhitespace: true,
collapseInlineTagWhitespace: true,
conservativeCollapse: true,
minifyCSS: true,
minifyJS: true,
removeComments: true,
useShortDoctype: true,
html5: true
});

writeFileSync(htmlPath, minifiedHTMLContent, 'utf8');
console.log('Wrote minified HTML to', htmlPath, minifiedHTMLContent.length);
});

46 changes: 0 additions & 46 deletions build/optimize.mjs

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"serve": "postdoc preview",
"clone:docs": "rm -rf nightwatch-docs/ && git clone https://github.com/nightwatchjs/nightwatch-docs.git",
"clone:nightwatch": "rm -rf nightwatch/ && git clone https://github.com/nightwatchjs/nightwatch.git",
"build": "postdoc build && node build/optimize.mjs",
"build": "postdoc build",
"optimize": "node build/optimize.js",
"netlify": "npm run clone:docs && npm run clone:nightwatch && npm run build",
"partytown": "partytown copylib public/~partytown",
"eslint": "eslint src test --quiet"
Expand Down

0 comments on commit 4370d82

Please sign in to comment.