-
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 we transformed 'templateUrl', 'styleUrls' and 'styles' everywhere they were assigned. The new implementation of the AstTransformer splits it in two: * InlineFilesTransformer, which inlines `templateUrl` and removes `styleUrls` files * StripStylesTransformer, which removes the `styles` property, but only inside the `@Component` decorator
- Loading branch information
Showing
9 changed files
with
445 additions
and
112 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
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 strip styleUrl 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
46 changes: 46 additions & 0 deletions
46
__tests__/__snapshots__/StripStylesTransformer.test.ts.snap
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,46 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`inlining template and stripping styles should not transform styles outside decorator 1`] = ` | ||
"var assignmentsToNotBeTransformed = { | ||
styles: [{ | ||
color: 'red' | ||
}] | ||
}; | ||
" | ||
`; | ||
|
||
exports[`inlining template and stripping styles should strip styleUrl assignment 1`] = ` | ||
"\\"use strict\\"; | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
if (typeof Reflect === \\"object\\" && typeof Reflect.decorate === \\"function\\") r = Reflect.decorate(decorators, target, key, desc); | ||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | ||
return c > 3 && r && Object.defineProperty(target, key, r), r; | ||
}; | ||
Object.defineProperty(exports, \\"__esModule\\", { value: true }); | ||
var core_1 = require(\\"@angular/core\\"); | ||
var AngularComponent = /** @class */ (function () { | ||
function AngularComponent() { | ||
} | ||
AngularComponent = __decorate([ | ||
SomeDecorator({ | ||
value: 'test', | ||
styles: [ | ||
':host { background-color: red }' | ||
], | ||
}), | ||
core_1.Component({ | ||
templateUrl: './page.html', | ||
styleUrls: [ | ||
'./fancy-styles.css', | ||
'./basic-styles.scss' | ||
], | ||
styles: [], | ||
unaffectedProperty: 'whatever' | ||
}) | ||
], AngularComponent); | ||
return AngularComponent; | ||
}()); | ||
exports.AngularComponent = AngularComponent; | ||
" | ||
`; |
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.