Commit 43949ea4 authored by cgm616's avatar cgm616

Uglify js and css in metalsmith chain

parent 13b72763
......@@ -18,10 +18,13 @@ var nested = require('metalsmith-nested');
var writemetadata = require('metalsmith-writemetadata');
var dates = require('metalsmith-date-formatter');
var ignore = require('metalsmith-ignore');
var uglify = require('uglify-es');
var multimatch = require('multimatch');
var metallic = require('metalsmith-metallic');
var csso = require('csso');
var fs = require('fs')
var path = require('path')
var fs = require('fs');
var path = require('path');
var shouldServe = false;
......@@ -31,6 +34,30 @@ process.argv.forEach(function(arg) {
}
});
function metalsmithJsUglify(pattern) {
return function(files, metalsmith, done){
setImmediate(done);
Object.keys(files).forEach(function(file){
if(multimatch([file], pattern).indexOf(file) !== -1) {
var newContents = uglify.minify(files[file].contents.toString()).code;
files[file].contents = Buffer.from(newContents);
}
});
};
}
function metalsmithCssUglify(pattern) {
return function(files, metalsmith, done) {
setImmediate(done);
Object.keys(files).forEach(file => {
if(multimatch([file], pattern).indexOf(file) !== -1) {
var newContents = csso.minify(files[file].contents.toString()).css;
files[file].contents = Buffer.from(newContents);
}
});
};
}
Metalsmith(__dirname) // instantiate Metalsmith in the cwd
.metadata({
title: "cgm616",
......@@ -72,6 +99,8 @@ Metalsmith(__dirname) // instantiate Metalsmith in the cwd
collection: 'articles'
}))
.use(autoprefixer())
.use(metalsmithJsUglify(['**/*.js']))
.use(metalsmithCssUglify(['**/*.css']))
.use(nested({
directory: 'nested',
generated: 'layouts'
......
......@@ -2,6 +2,7 @@
"name": "cgm616.me",
"private": true,
"dependencies": {
"csso": "^3.1.1",
"handlebars": "^4.0.5",
"metalsmith": "^2.1.0",
"metalsmith-autoprefixer": "^1.1.1",
......@@ -26,5 +27,7 @@
"metalsmith-watch": "^1.0.3",
"metalsmith-word-count": "0.0.4",
"metalsmith-writemetadata": "^0.4.5",
"multimatch": "^2.1.0",
"uglify-es": "^3.0.27"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment