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

feat: transform 'styles' only in decorator #261

Merged
merged 1 commit into from
Jun 30, 2019

Conversation

wtho
Copy link
Collaborator

@wtho wtho commented Apr 11, 2019

This PR is another implementation change in the transformation behavior. It is a proposal and open for discussion.

Motivation

See #254

Until now we transformed 'templateUrl', 'styleUrls' and 'styles'
everywhere they were assigned.

This even transformed code like this:
console.log({ styles: {...}}) => console.log({ styles: [] })

The new implementation of the AstTransformer only transforms property
assignments to 'styles' if it is placed inside a decorator.

templateUrl and styleUrls are still transformed everywhere if assigned to a string, as Angular might expect them to be an actual Angular-Template or CSS code. This will crash the tests, if they are passed to the @Component decorator.

Example

A new unit test explains the new implementation:

Untransformed:

  const assignmentsToNotBeTransformed = {
    styles: [{
      color: 'red'
    }]
  };

  const assignmentsToBeTransformed = {
    styleUrls: ['./some-styles.css'],
    templateUrl: './some-styles.css'
  };

Transformed:

var assignmentsToNotBeTransformed = {
    styles: [{
            color: 'red'
        }]
};
var assignmentsToBeTransformed = {
    styleUrls: [],
    template: require('./some-styles.css')
};

Caveats

There were always caveats and there still will be.

  • This preset replaces styles wherever it decides they are not required. This design-decision was made due to the unit-test nature of jest. To test your styles, better use an e2e framework. The style stripping should speed up your tests, but might hinder you from testing applied styles.
  • templateUrl and styleUrls are transformed anywhere in the code. This enables you to declare them somewhere in your unit tests and then plug them into a component, e. g. using
      TestBed.configureTestingModule({
        declarations: [
          AlertFollowComponent,
        ],
      }).overrideComponent(AlertFollowComponent, {
        set: {
          templateUrl: '../__mocks__/alert-follow-stub.component.html',
        },
      });
    At the same time, there will be side-effects on variables named templateUrl and styleUrls if you use them in a different context.

EDIT:

  • The implementation lead to introduce more code in the ASTTransformer, this means, while it is more granular in its behavior, it is also more difficult to maintain.

Closes #254

@wtho
Copy link
Collaborator Author

wtho commented Apr 11, 2019

Actually it might be smart to split them up into two transformers, didn't think about it earlier

@ahnpnl
Copy link
Collaborator

ahnpnl commented Apr 25, 2019

I think we should do benchmarking with this change as well.

@wtho
Copy link
Collaborator Author

wtho commented Apr 25, 2019

Yeah!

I still definitely want to split it up before merging.

@wtho wtho force-pushed the feat/ast-transformer-strict-styles branch 2 times, most recently from 252144e to 6272b7f Compare June 10, 2019 23:02
@wtho
Copy link
Collaborator Author

wtho commented Jun 10, 2019

  • Split the AstTransformers in two and implemented the more careful behavior for styles, it only gets removed in @Component-Decorators.
  • Assignments with the names templateUrl and styleUrls will still be transformed anywhere.

Will fix the flatMap issue in the next days.

@ahnpnl do you have any suggestions how to approach the benchmarking?

@wtho wtho force-pushed the feat/ast-transformer-strict-styles branch 4 times, most recently from f5af978 to 689478c Compare June 18, 2019 16:26
@wtho
Copy link
Collaborator Author

wtho commented Jun 18, 2019

  • replaced flatMap with reduce in transformer
  • added README and CHANGELOG notes
  • added tests for styles
  • rebased on top of master

src/StripStylesTransformer.ts Outdated Show resolved Hide resolved
src/StripStylesTransformer.ts Show resolved Hide resolved
src/StripStylesTransformer.ts Show resolved Hide resolved
InlineHtmlStripStylesTransformer.js
InlineFilesTransformer.js
StripStylesTransformer.js
TransformUtils.js
Copy link
Owner

Choose a reason for hiding this comment

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

I think it's time for a build/ directory – ideally in a separate PR :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will start with that once this is done!

@wtho wtho force-pushed the feat/ast-transformer-strict-styles branch from 689478c to 04bcba5 Compare June 19, 2019 16:12
Until now 'templateUrl', 'styleUrls' and 'styles' were transformed
everywhere, where they were assigned.

The new implementation of the AstTransformers splits it in two:
* InlineFilesTransformer, which inlines `templateUrl` and removes
  `styleUrls` file references
* StripStylesTransformer, which removes the `styles` property, but only
  if it is assigned inside the `@component` decorator

Tests were added to ensure the transformers behave as desired in edge
cases regarding `styleUrls` and `styles`.
@wtho wtho force-pushed the feat/ast-transformer-strict-styles branch from 04bcba5 to 351e919 Compare June 30, 2019 15:31
@wtho
Copy link
Collaborator Author

wtho commented Jun 30, 2019

In last force-push (to 351e919), after @thymikee's approval I fixed minor issues (that I thought I pushed before):

  • fixed CSS statements like body: { display: none } - removed :
  • removed tests that test basic signatures (what TS does anyway)
  • joined two filter assignments in StripStylesTransformer.ts
  • replaced switch with single case by if

@thymikee
Copy link
Owner

@ahnpnl anything to add or can e merge it? :)

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jun 30, 2019

LGTM 🎉

@thymikee thymikee merged commit f01fd00 into thymikee:master Jun 30, 2019
@ahnpnl ahnpnl added this to the next milestone Sep 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

InlineHtmlStripStylesTransformer is not strict enough
3 participants