Skip to content

Commit

Permalink
suport minify option in filter using jstransformer (#3084)
Browse files Browse the repository at this point in the history
  • Loading branch information
pirxpilot authored May 1, 2020
1 parent beb3755 commit a941dec
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
28 changes: 16 additions & 12 deletions packages/pug-filters/lib/run-filter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
'use strict';

var jstransformer = require('jstransformer');
var uglify = require('uglify-js');
var CleanCSS = require('clean-css');
var resolve = require('resolve');

module.exports = filter;

function getMinifyTransformerName(outputFormat) {
switch (outputFormat) {
case 'js':
return 'uglify-js';
case 'css':
return 'clean-css';
}
}

function filter(name, str, options, currentDirectory, funcName) {
funcName = funcName || 'render';
var trPath;
Expand All @@ -26,17 +34,13 @@ function filter(name, str, options, currentDirectory, funcName) {
// TODO: we may want to add a way for people to separately specify "locals"
var result = tr[funcName](str, options, options).body;
if (options && options.minify) {
try {
switch (tr.outputFormat) {
case 'js':
result = uglify.minify(result, {fromString: true}).code;
break;
case 'css':
result = new CleanCSS().minify(result).styles;
break;
var minifyTranformer = getMinifyTransformerName(tr.outputFormat);
if (minifyTranformer) {
try {
result = filter(minifyTranformer, result, null, currentDirectory);
} catch (ex) {
// better to fail to minify than output nothing
}
} catch (ex) {
// better to fail to minify than output nothing
}
}
return result;
Expand Down
4 changes: 1 addition & 3 deletions packages/pug-filters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
"pug"
],
"dependencies": {
"clean-css": "^4.2.3",
"constantinople": "^4.0.1",
"jstransformer": "1.0.0",
"pug-error": "^1.3.3",
"pug-walk": "^1.1.8",
"resolve": "^1.15.1",
"uglify-js": "^2.6.1"
"resolve": "^1.15.1"
},
"devDependencies": {
"get-repo": "^1.0.0",
Expand Down

0 comments on commit a941dec

Please sign in to comment.