Skip to content

Commit

Permalink
improve node.name parsing, fix silent errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hhsnopek committed Nov 29, 2017
1 parent 3d660b6 commit c492c03
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
48 changes: 29 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/minifyCss.js
Original file line number Diff line number Diff line change
@@ -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 })
}
1 change: 0 additions & 1 deletion lib/modules/minifyJs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion lib/modules/minifyJson.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/modules/minifySvg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})
}

0 comments on commit c492c03

Please sign in to comment.