Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suport minify option in filter using jstransformer #3084

Merged
merged 3 commits into from May 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 14 additions & 12 deletions packages/pug-filters/lib/run-filter.js
@@ -1,11 +1,17 @@
'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 @@ -24,17 +30,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
Expand Up @@ -6,13 +6,11 @@
"pug"
],
"dependencies": {
"clean-css": "^4.1.11",
"constantinople": "^3.0.1",
"jstransformer": "1.0.0",
"pug-error": "^1.3.2",
"pug-walk": "^1.1.7",
"resolve": "^1.1.6",
"uglify-js": "^2.6.1"
"resolve": "^1.1.6"
},
"devDependencies": {
"get-repo": "^1.0.0",
Expand Down