Created
February 6, 2018 16:18
-
-
Save XOP/2f5c025f102d149a284f16a993279e93 to your computer and use it in GitHub Desktop.
static-post-processing-2016
This file contains hidden or 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
var fs = require('fs'); | |
var path = require('path'); | |
// used deps | |
var inlineCSS = require('inline-css'); | |
var htmlMinify = require('html-minifier').minify; | |
// working paths | |
var projectFolder = path.resolve(__dirname, '../../'); | |
var workingFolder = path.join(projectFolder, 'build/emails/'); | |
var destFolder = path.join(projectFolder, 'emails/'); | |
var source = fs.readdirSync(workingFolder); | |
// preparations | |
try { | |
fs.readdirSync(destFolder); | |
} catch (err) { | |
fs.mkdirSync(destFolder); | |
} | |
// processing html files | |
source.forEach(function (fileName) { | |
if (fileName.indexOf('.html') > -1) { | |
// CSS inlining | |
var htmlSource = fs.readFileSync(path.join(workingFolder, fileName), 'utf8'); | |
var cssSource = fs.readFileSync(path.join(workingFolder, 'styles.css'), 'utf8'); | |
var processPipe = inlineCSS(htmlSource, { | |
url: './', | |
extraCss: cssSource, | |
applyStyleTags: false, | |
removeStyleTags: false, | |
applyLinkTags: false | |
}); | |
// stripping react attributes | |
// stripping extra classnames | |
processPipe = processPipe.then(function (html) { | |
return html | |
.replace(/data-reactid="([.\w]+)"/g, '') | |
.replace(/class="([.\w]+)"/g, ''); | |
}); | |
// HTML minifying | |
processPipe = processPipe.then(function (html){ | |
return htmlMinify(html, { | |
collapseWhitespace: true, | |
caseSensitive: true, | |
collapseInlineTagWhitespace: true | |
}); | |
}); | |
processPipe.then(function (html){ | |
fs.writeFileSync(path.resolve(destFolder, fileName), html, 'utf8'); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment