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

Minify Templates based on a white list #14

Merged
merged 4 commits into from
Mar 14, 2018

Conversation

siva-sundar
Copy link
Contributor

@siva-sundar siva-sundar commented Sep 29, 2017

Implements #9

@siva-sundar siva-sundar force-pushed the patch_hbs_minify_config branch 20 times, most recently from 98c5fd7 to 53dde0b Compare October 4, 2017 05:58
@siva-sundar siva-sundar changed the title [WIP] Minify Templates based on a white list Minify Templates based on a white list Oct 4, 2017
index.js Outdated Show resolved Hide resolved
@siva-sundar siva-sundar force-pushed the patch_hbs_minify_config branch 2 times, most recently from 93b3586 to d49229f Compare October 4, 2017 14:08
@Turbo87
Copy link
Collaborator

Turbo87 commented Oct 14, 2017

@siva-sundar I'm still seeing tests that have changed. I thought you adjusted the PR?

@siva-sundar
Copy link
Contributor Author

siva-sundar commented Oct 15, 2017

Since we are collapsing the leading and trailing whitespaces into a single whitespace, the existing test cases needed to be updated. Let me know if there is anything else needs to be done.

@Turbo87
Copy link
Collaborator

Turbo87 commented Oct 16, 2017

@siva-sundar that is a separate change though that seems unrelated to the intent of this PR which was adding support for whitelisting components. please note that the amount of changes in this PR make it very hard to review and it is usually much easier for maintainers to review smaller PRs that only change or add single features.

@siva-sundar siva-sundar changed the title Minify Templates based on a white list [WIP] Minify Templates based on a white list Oct 17, 2017
@siva-sundar
Copy link
Contributor Author

siva-sundar commented Oct 17, 2017

I have moved the changes other than the whitelisting into a separate PR(#16). Once it is merged, I'll update this PR.

Copy link
Collaborator

@Turbo87 Turbo87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't have much time atm as I'm going on vacation today for a week. I'll have a detailed look once I'm back.

class BasePlugin {
constructor(env) {
env = env || {};
this.moduleName = env.moduleName;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems unused now

ember-cli-build.js Outdated Show resolved Hide resolved
hbs-minifier-plugin.test.js Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
@Turbo87 Turbo87 force-pushed the patch_hbs_minify_config branch 2 times, most recently from e84a15b to ea4ee45 Compare February 5, 2018 18:08
@Turbo87
Copy link
Collaborator

Turbo87 commented Feb 5, 2018

@siva-sundar I've rebased your branch on top of all the changes that fixed our CI builds. It seems that some tests are now failing though... 😞

@siva-sundar
Copy link
Contributor Author

siva-sundar commented Feb 7, 2018

@Turbo87 Tests are failing since htmlbars adds text node boundaries (at the begining and the end) of a template file.

Currently I am checking for two text nodes here. But for ember <= 2.9.1, there will be an additional textNode created to mark the closeBoundary.

Shall I update the test case based on a version check ?

@Turbo87
Copy link
Collaborator

Turbo87 commented Feb 8, 2018

Shall I update the test case based on a version check ?

I've updated the existing tests to essentially .filter(el => el.textContent !== '') and then did the elements.length check afterwards. This was enough to make it pass on all 2.x versions of Ember without any explicit version checks.

@siva-sundar siva-sundar force-pushed the patch_hbs_minify_config branch 5 times, most recently from 0e459ea to 1b517e2 Compare February 8, 2018 14:12
@siva-sundar
Copy link
Contributor Author

siva-sundar commented Feb 8, 2018

I have updated the test cases. Can you have a look at it?

*/
if (startLoc.line !== 1 || startLoc.column !== 0) {
return ast;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this is necessary. Isn't the transform() function only called for top-level elements? The AST traversal should be handled by the traverse() call below.

Copy link
Contributor Author

@siva-sundar siva-sundar Feb 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ember 2.15, the transform is called for all the Program Nodes. I am not sure why. Please check this pipeline and the changes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is a bug in Ember 2.15 that is already fixed again afaik

/cc @rwjblue

Copy link
Contributor Author

@siva-sundar siva-sundar Feb 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Turbo87 Do you want this issue to be patched up or should I keep it as an issue for that particular Ember versions?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should no guard against that. The transform that we do should be idempotent so the only advantage of this guard should be less work to do for the buggy versions.

module.exports = HBSMinifierPlugin;
module.exports = function(config) {
config = config || {};
config = assignDefaultValues(config);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am missing the previous default config where (without any configuration) <pre> elements and the {{#no-minify}} "component" were skipped.

Copy link
Contributor Author

@siva-sundar siva-sundar Feb 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll check for no-minify and pre tag here and here respectively.

utils/helpers.js Outdated
}


const canTrimBlockStatementContent = function(node, config) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please convert those to named functions instead of const + anonymous function

utils/helpers.js Outdated
if (config.components.indexOf(componentName) !== -1 || componentName === 'no-minify' || config.components === 'all') {
return false;
}
return true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be simplified by returning the inverse of the existing condition

utils/helpers.js Outdated

const assignDefaultValues = function(config) {
config = config || {};
let elements = config.elements || 'all';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we skipping all by default?

@siva-sundar
Copy link
Contributor Author

siva-sundar commented Feb 19, 2018

I have updated the PR with the changes mentioned. Please take a look again.

static createASTPlugin() {
class BasePlugin {
static createASTPlugin(config) {
config = config || {};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think at the point config should not be optional anymore.

_setupPreprocessorRegistry(app) {
let registry = app.registry;
let options = app.options || {};
let config = options['ember-hbs-minifier'] || {};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the point at which we should apply the config defaults. If config.skip is not set we set it to { elements: ['pre'], components: ['no-minify'] } by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, if the user provides a config, he need to pass the defaults (pre and no-minify) along with his config like below. Is it ok?

{
  skip: {
    elements: ['pre', 'div'],
    components: ['no-minify', 'my-component']
  }
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I think that makes sense. otherwise it wouldn't be possible to not skip <pre>.

utils/helpers.js Outdated
});
}

function canTrimUnnecessaryWhiteSpace(value, config) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the method name should in some way suggest that this is handling values of the class property

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to pass in the complete config here or would config.classes be enough?

@Turbo87 Turbo87 merged commit 46dd8d9 into mainmatter:master Mar 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants