Skip to content

Commit

Permalink
Merge pull request #105 from Turbo87/config
Browse files Browse the repository at this point in the history
Specify individual AST plugin config per test
  • Loading branch information
Turbo87 committed Apr 28, 2019
2 parents aa3df48 + 3ac77fc commit 79e7abf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
15 changes: 12 additions & 3 deletions hbs-minifier-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const trailingWhiteSpace = /[ \t\r\n]+$/;
const WHITESPACE = /^[ \t\r\n]+$/;

function createGlimmerPlugin(config) {
normalizeConfig(config);

// in this stack we track the nodes that cause us to skip the minification
// e.g. `{{#no-minify}} ... {{/no-minify}}` blocks or `<pre></pre>` tags
// depending on the configuration
Expand Down Expand Up @@ -191,7 +193,7 @@ function canTrimWhiteSpaceBasedOnClassNames(value, configClassNames) {


function shouldSkipBlockStatement(node, config) {
let components = config.components;
let components = config.skip.components;
if (components === 'all') {
return true;
}
Expand All @@ -202,7 +204,7 @@ function shouldSkipBlockStatement(node, config) {
}

function shouldSkipElementNode(node, config) {
let elements = config.elements;
let elements = config.skip.elements;
if (elements === 'all') {
return true;
}
Expand All @@ -218,7 +220,14 @@ function shouldSkipClass(node, config) {
return false;
}

return !canTrimWhiteSpaceBasedOnClassNames(classAttrNode.value, config.classes);
return !canTrimWhiteSpaceBasedOnClassNames(classAttrNode.value, config.skip.classes);
}

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

module.exports = {
Expand Down
34 changes: 19 additions & 15 deletions hbs-minifier-plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ expect.addSnapshotSerializer({
},
});

const defaultConfig = {
skip: {
elements: ['pre', 'address'],
classes: ['description'],
components: ['foo-bar', 'no-minify']
}
};

for (let version of versions) {
let moduleName = 'HBS Minifier plugin';
if (version !== DEFAULT) {
Expand Down Expand Up @@ -97,31 +89,43 @@ for (let version of versions) {
});

it('does not minify `tagNames` specified in .hbs-minifyrc.js', function() {
let config = {
skip: { elements: ['address'] },
};

assert(`<address>
Box 564,
<b>
Disneyland
</b>
<br>
<u> USA </u>
</address>`);
</address>`, config);
});

it('does not minify `classNames` specified in .hbs-minifyrc.js', function() {
let config = {
skip: { classes: ['description'] },
};

assert(`<div class="description">
1
<span>
2
</span>
</div>`);
</div>`, config);
});

it('does not minify `components` specified in .hbs-minifyrc.js', function() {
let config = {
skip: { components: ['foo-bar'] },
};

assert(`{{#foo-bar}}
<span>
yield content
</span>
{{/foo-bar}}`);
{{/foo-bar}}`, config);
});

it('minifies `tagNames` that are not specified in .hbs-minifyrc.js', function() {
Expand Down Expand Up @@ -152,17 +156,17 @@ for (let version of versions) {
{{/my-component}}`);
});

function assert(template) {
let ast = process(template);
function assert(template, config = {}) {
let ast = process(template, config);
expect(ast).toMatchSnapshot();

let printed = glimmer.print(ast);
expect(`${template}\n---\n${printed}`).toMatchSnapshot();
}

function process(template) {
function process(template, config) {
let plugin = () => {
return require('./hbs-minifier-plugin').createGlimmerPlugin(defaultConfig.skip);
return require('./hbs-minifier-plugin').createGlimmerPlugin(config);
};
return glimmer.preprocess(template, {
plugins: {
Expand Down
15 changes: 1 addition & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
let options = app.options || {};
let config = options['ember-hbs-minifier'] || {};

let HbsMinifierPlugin = require('./hbs-minifier-plugin').createRegistryPlugin(assignDefaultValues(config.skip));
let HbsMinifierPlugin = require('./hbs-minifier-plugin').createRegistryPlugin(config);
registry.add('htmlbars-ast-plugin', {
name: 'hbs-minifier-plugin',
plugin: HbsMinifierPlugin,
Expand All @@ -28,16 +28,3 @@ module.exports = {
this._setupPreprocessorRegistry(app);
}
};

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

return {
elements,
classes,
components
};
}

0 comments on commit 79e7abf

Please sign in to comment.