From c492c03c3464475fc5f47d5b31490ce748e760b7 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 29 Nov 2017 17:44:15 -0500 Subject: [PATCH] improve node.name parsing, fix silent errors --- lib/index.js | 48 +++++++++++++++++++++++---------------- lib/modules/minifyCss.js | 2 +- lib/modules/minifyJs.js | 1 - lib/modules/minifyJson.js | 1 - lib/modules/minifySvg.js | 6 ++--- 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/lib/index.js b/lib/index.js index 63db2b9..6c6bf2c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,29 +2,39 @@ const util = require('reshape-plugin-util') const path = require('path') const modules = require('indx')(path.join(__dirname, 'modules')) -module.exports = function reshapeMinify (opts = {}) { - opts = Object.assign({ - collapseWhitespace: true, - conservativeCollapse: false, - aggressiveCollapse: false, - removeComments: true, - minifyCss: true, - minifyJs: true, - minifyJson: true, - minifySvg: true - }, opts) - +module.exports = function reshapeMinify ({ + collapseWhitespace = true, + conservativeCollapse = false, + aggressiveCollapse = false, + removeComments = true, + minifyCss = true, + minifyJs = true, + minifyJson = true, + minifySvg = true +} = {}) { return function minifyPlugin (tree, ctx) { - return util.modifyNodes(tree, () => true, (node) => { - node = modules.collapseWhitespace(node, opts) + return util.modifyNodes(tree, () => true, node => { + node = modules.collapseWhitespace(node, { conservativeCollapse, aggressiveCollapse }) node = modules.booleanAttributes(node) node = modules.emptyAttrs(node) node = modules.redundantAttrs(node) - if (opts.minifyCss) node = modules.minifyCss(node) - if (opts.minifyJs) node = modules.minifyJs(node) - if (opts.minifyJson) node = modules.minifyJson(node) - if (opts.minifySvg) node = modules.minifySvg(node, ctx) - if (opts.removeComments) node = modules.removeComments(node) + + if (removeComments) node = modules.removeComments(node) + switch (node.name) { + case 'style': + if (minifyCss) node = modules.minifyCss(node) + break + case 'script': + if (minifyJs) node = modules.minifyJs(node) + if (minifyJson) node = modules.minifyJson(node) + break + case 'svg': + if (minifySvg) node = modules.minifySvg(node, ctx) + break + default: + break + } + return node }) } diff --git a/lib/modules/minifyCss.js b/lib/modules/minifyCss.js index 884a12e..05bd9f3 100644 --- a/lib/modules/minifyCss.js +++ b/lib/modules/minifyCss.js @@ -1,9 +1,9 @@ const cssnano = require('cssnano') module.exports = (node) => { - if (node.name !== 'style') return node if (!node.content || !node.content.length) return node return cssnano .process(node.content[0].content) .then((res) => { node.content[0].content = res.css; return node }) + .catch(err => { throw err }) } diff --git a/lib/modules/minifyJs.js b/lib/modules/minifyJs.js index ec01b70..6d98760 100644 --- a/lib/modules/minifyJs.js +++ b/lib/modules/minifyJs.js @@ -2,7 +2,6 @@ const uglify = require('uglify-js') module.exports = (node) => { // get rid of anything that's not a script - if (node.name !== 'script') return node if (!node.content || !node.content.length) return node if (node.attrs && node.attrs.type && !node.attrs.type[0].content.match(/.*\/javascript/)) { return node diff --git a/lib/modules/minifyJson.js b/lib/modules/minifyJson.js index 9437818..472eb16 100644 --- a/lib/modules/minifyJson.js +++ b/lib/modules/minifyJson.js @@ -1,5 +1,4 @@ module.exports = (node) => { - if (node.name !== 'script') return node if (!node.attrs) return node if (!node.attrs.type) return node if (!node.attrs.type[0].content.match(/(\/|\+)json/)) return node diff --git a/lib/modules/minifySvg.js b/lib/modules/minifySvg.js index 1423df7..12042e7 100644 --- a/lib/modules/minifySvg.js +++ b/lib/modules/minifySvg.js @@ -3,11 +3,11 @@ const SVGO = require('svgo') const svgo = new SVGO() module.exports = (node, ctx) => { - if (node.name !== 'svg') return node return new Promise((resolve, reject) => { const rendered = ctx.generator([node])() - svgo.optimize(rendered, (res) => { - resolve(reshapeParser(res.data, ctx)) + svgo.optimize(rendered, ({ error, data }) => { + if (error) reject(new Error(error)) + resolve(reshapeParser(data, ctx)) }) }) }