Skip to content

Instantly share code, notes, and snippets.

@arlair
Created August 16, 2017 05:19
Show Gist options
  • Save arlair/b4417f2aa59410c4bac8b9192de12945 to your computer and use it in GitHub Desktop.
Save arlair/b4417f2aa59410c4bac8b9192de12945 to your computer and use it in GitHub Desktop.
inlineCSS.js modifications from Serverless version
const path = require('path')
const fs = require('fs-extra')
const critical = require('critical')
const dir = require('node-dir')
// const distPath = path.join(__dirname, '..', '..', 'dist')
const firstArg = process.argv.slice(2)[0];
// path comes back to root, appends dist folder
var distPath = path.join(__dirname, '..', '..', firstArg, 'dist');
console.info('distPath: ' + distPath)
function inlineCSS(filePath, callBack) {
console.time('inlineCSS')
dir.readFiles(filePath, {
// match: /.html$/,
// 404.html was writing into root index.html, ignore 404 for now
match: /index.html$/,
//exclude: /^\./
}, (err, content, filename, next) => {
if (err) throw err
console.log('filename', filename)
const directoryPath = path.dirname(filename)
const newPath = path.join(directoryPath, 'index.html')
// const newPath = path.join(directoryPath, 'index-critical.html')
console.log('newPath', newPath)
critical.generate({
inline: true,
base: filePath,
src: filename,
dest: newPath,
minify: true,
dimensions: [{
height: 200,
width: 500
}, {
height: 900,
width: 1200
}]
}, (err, output) => {
// console.log(output)
})
next()
},
(err, files) => {
if (err) {
if (callBack) {
callBack(err)
}
}
if (callBack) {
callBack(null, files)
}
console.log('files', files)
console.timeEnd('inlineCSS')
}
)
}
inlineCSS(distPath)
@arlair
Copy link
Author

arlair commented Aug 16, 2017

Adapted from https://github.com/serverless/site/blob/6c6648a15dedca3c8c468115d8707c74b53edd99/scripts/build/inlineCSS.js

Not much difference:

  • Added dimensions to try include responsive CSS.
  • Only match index.html files as it was finding 404.html, but writing it as index.html.
  • Allows a parameter for the path so it can be called by different repos from a shared repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment