Skip to content

Commit

Permalink
Assign default config
Browse files Browse the repository at this point in the history
  • Loading branch information
siva-sundar committed Feb 21, 2018
1 parent 3962fb0 commit a4cd393
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 24 deletions.
2 changes: 1 addition & 1 deletion ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function(defaults) {
skip: {
elements: ['pre', 'address'],
classes: ['description'],
components: ['foo-bar']
components: ['foo-bar', 'no-minify']
}
}
});
Expand Down
11 changes: 1 addition & 10 deletions hbs-minifier-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ const hasLeadingOrTrailingWhiteSpace = Util.hasLeadingOrTrailingWhiteSpace;
const stripNoMinifyBlocks = Util.stripNoMinifyBlocks;
const canTrimBlockStatementContent = Util.canTrimBlockStatementContent;
const canTrimElementNodeContent = Util.canTrimElementNodeContent;
const assignDefaultValues = Util.assignDefaultValues;

class BasePlugin {
static createASTPlugin(config) {
config = config || {};
let preStack = [];
let visitor = {
TextNode(node) {
Expand Down Expand Up @@ -56,10 +54,6 @@ class BasePlugin {

exit(node) {
node.body = stripNoMinifyBlocks(node.body);

if (preStack[preStack.length - 1] === node) {
preStack.pop();
}
},
},

Expand Down Expand Up @@ -101,15 +95,12 @@ class BasePlugin {
}

module.exports = function(config) {
config = config || {};
config = assignDefaultValues(config);

return class HBSMinifierPlugin extends BasePlugin {

transform(ast) {
let startLoc = ast.loc ? ast.loc.start : {};
/*
checking for line and column to avoid registering the plugin for ProgramNode inside a BlockStatement.
Checking for line and column to avoid registering the plugin for ProgramNode inside a BlockStatement since transform is called for all ProgramNodes in Ember 2.15.X. Removing this would result in minifying all the TextNodes.
*/
if (startLoc.line !== 1 || startLoc.column !== 0) {
return ast;
Expand Down
2 changes: 1 addition & 1 deletion hbs-minifier-plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const defaultConfig = {
skip: {
elements: ['pre', 'address'],
classes: ['description'],
components: ['foo-bar']
components: ['foo-bar', 'no-minify']
}
};
const glimmer = require('@glimmer/syntax');
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env node */
'use strict';
let objectHash = require('object-hash');
const Util = require('./utils/helpers');

module.exports = {
name: 'ember-hbs-minifier',
Expand All @@ -10,7 +11,7 @@ module.exports = {
let options = app.options || {};
let config = options['ember-hbs-minifier'] || {};

let HbsMinifierPlugin = require('./hbs-minifier-plugin')(config.skip || {});
let HbsMinifierPlugin = require('./hbs-minifier-plugin')(Util.assignDefaultValues(config.skip));
registry.add('htmlbars-ast-plugin', {
name: 'hbs-minifier-plugin',
plugin: HbsMinifierPlugin,
Expand Down
22 changes: 11 additions & 11 deletions utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function isClassIncluded(chars, classes) {
});
}

function canTrimUnnecessaryWhiteSpace(value, config) {
function canTrimWhiteSpaceBasedOnClassNames(value, configClassNames) {
/*
1. If no value is provided for class, the we can minify the content.
2. If all classNames need to be preserved, then we must preserve the whitespace.
Expand Down Expand Up @@ -73,15 +73,15 @@ function canTrimUnnecessaryWhiteSpace(value, config) {
if (!value) {
return true;
}
if (config.classes === 'all') {
if (configClassNames === 'all') {
return false;
}
let type = value.type;

if (type === 'TextNode') {
return !isClassIncluded(value.chars, config.classes);
return !isClassIncluded(value.chars, configClassNames);
} else if (type === 'StringLiteral') {
return !isClassIncluded(value.value, config.classes);
return !isClassIncluded(value.value, configClassNames);
} else if (type === 'PathExpression') {
return false;
} else if (type === 'MustacheStatement') {
Expand All @@ -90,7 +90,7 @@ function canTrimUnnecessaryWhiteSpace(value, config) {
if (['if', 'unless'].indexOf(value.path.original) !== -1) {
let params = value.params;
for (let i = 1; i < params.length; i++) {
canTrim = canTrimUnnecessaryWhiteSpace(params[i], config);
canTrim = canTrimWhiteSpaceBasedOnClassNames(params[i], configClassNames);
if (!canTrim) {
break;
}
Expand All @@ -101,7 +101,7 @@ function canTrimUnnecessaryWhiteSpace(value, config) {
let parts = value.parts;

return parts.every((part) => {
return canTrimUnnecessaryWhiteSpace(part, config);
return canTrimWhiteSpaceBasedOnClassNames(part, configClassNames);
});
}
return true;
Expand All @@ -112,25 +112,25 @@ function canTrimBlockStatementContent(node, config) {
// If a block or all the blocks is/are skiped (or) named as 'no-minify' then we need to preserve the whitespace.
let componentName = node.path.original;
let components = config.components;
return !(components.indexOf(componentName) !== -1 || componentName === 'no-minify' || components === 'all');
return !(components.indexOf(componentName) !== -1 || components === 'all');
}

function canTrimElementNodeContent(node, config) {
// If a element or all the element is/are skiped then we need to preserve the whitespace.
let elements = config.elements;
let tag = node.tag;
if (elements.indexOf(tag) !== -1 || elements === 'all' || tag === 'pre') {
if (elements.indexOf(tag) !== -1 || elements === 'all') {
return false;
}
let classAttributes = getElementAttribute(node, 'class');
return classAttributes ? canTrimUnnecessaryWhiteSpace(classAttributes, config) : true;
return classAttributes ? canTrimWhiteSpaceBasedOnClassNames(classAttributes, config.classes) : true;
}

function assignDefaultValues(config) {
config = config || {};
let elements = config.elements || [];
let elements = config.elements || ['pre'];
let classes = config.classes || [];
let components = config.components || [];
let components = config.components || ['no-minify'];

return {
elements,
Expand Down

0 comments on commit a4cd393

Please sign in to comment.