-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: transform 'styles' only in decorator
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`.
- Loading branch information
Showing
10 changed files
with
467 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
node_modules | ||
InlineHtmlStripStylesTransformer.js | ||
InlineFilesTransformer.js | ||
StripStylesTransformer.js | ||
TransformUtils.js | ||
*.log | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Code is inspired by | ||
* https://github.com/kulshekhar/ts-jest/blob/25e1c63dd3797793b0f46fa52fdee580b46f66ae/src/transformers/hoist-jest.spec.ts | ||
* | ||
*/ | ||
|
||
import * as tsc from 'typescript'; | ||
import * as transformer from '../StripStylesTransformer'; | ||
|
||
const CODE_WITH_STYLES_AND_OTHER_ASSIGNMENTS = ` | ||
import { Component } from '@angular/core'; | ||
@SomeDecorator({ | ||
value: 'test', | ||
styles: [ | ||
':host { background-color: red }' | ||
], | ||
}) | ||
@Component({ | ||
templateUrl: './page.html', | ||
styleUrls: [ | ||
'./fancy-styles.css', | ||
'./basic-styles.scss' | ||
], | ||
styles: [ | ||
'body { display: none }', | ||
'html { background-color: red }' | ||
], | ||
unaffectedProperty: 'whatever' | ||
}) | ||
export class AngularComponent { | ||
} | ||
`; | ||
|
||
const CODE_WITH_ASSIGNMENT_OUTSIDE_DECORATOR = ` | ||
const assignmentsToNotBeTransformed = { | ||
styles: [{ | ||
color: 'red' | ||
}] | ||
}; | ||
`; | ||
|
||
const createFactory = () => { | ||
return transformer.factory({ compilerModule: tsc } as any); | ||
}; | ||
const transpile = (source: string) => | ||
tsc.transpileModule(source, { transformers: { before: [createFactory()] } }); | ||
|
||
describe('inlining template and stripping styles', () => { | ||
it('should have correct signature', () => { | ||
expect(transformer.name).toBe('angular-component-strip-styles'); | ||
expect(typeof transformer.version).toBe('number'); | ||
expect(transformer.version).toBeGreaterThan(0); | ||
expect(typeof transformer.factory).toBe('function'); | ||
}); | ||
|
||
it('should not strip styleUrls assignment', () => { | ||
const out = transpile(CODE_WITH_STYLES_AND_OTHER_ASSIGNMENTS); | ||
|
||
expect(out.outputText).toMatchSnapshot(); | ||
}); | ||
|
||
it('should not transform styles outside decorator', () => { | ||
const out = transpile(CODE_WITH_ASSIGNMENT_OUTSIDE_DECORATOR); | ||
|
||
expect(out.outputText).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.