-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(@ngtools/webpack): replace resources should effect only class dec…
…orators (#12503) At the moment we are processing all property assignments in object literals irrespective if they are in a decorator or not. With this change we will process only property assignments found under in a component decorator. Fixes #12488, Fixes #6007, Fixes: #6498 and Fixes: #8295
- Loading branch information
1 parent
bd4dc38
commit 96606b3
Showing
6 changed files
with
512 additions
and
127 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
68 changes: 68 additions & 0 deletions
68
packages/ngtools/webpack/src/transformers/find_resources.ts
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 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
import * as ts from 'typescript'; | ||
import { collectDeepNodes } from './ast_helpers'; | ||
import { getResourceUrl } from './replace_resources'; | ||
|
||
export function findResources(sourceFile: ts.SourceFile): string[] { | ||
const resources: string[] = []; | ||
const decorators = collectDeepNodes<ts.Decorator>(sourceFile, ts.SyntaxKind.Decorator); | ||
|
||
for (const node of decorators) { | ||
if (!ts.isCallExpression(node.expression)) { | ||
continue; | ||
} | ||
|
||
const decoratorFactory = node.expression; | ||
const args = decoratorFactory.arguments; | ||
if (args.length !== 1 || !ts.isObjectLiteralExpression(args[0])) { | ||
// Unsupported component metadata | ||
continue; | ||
} | ||
|
||
ts.visitNodes( | ||
(args[0] as ts.ObjectLiteralExpression).properties, | ||
(node: ts.ObjectLiteralElementLike) => { | ||
if (!ts.isPropertyAssignment(node) || ts.isComputedPropertyName(node.name)) { | ||
return node; | ||
} | ||
|
||
const name = node.name.text; | ||
switch (name) { | ||
case 'templateUrl': | ||
const url = getResourceUrl(node.initializer); | ||
|
||
if (url) { | ||
resources.push(url); | ||
} | ||
break; | ||
|
||
case 'styleUrls': | ||
if (!ts.isArrayLiteralExpression(node.initializer)) { | ||
return node; | ||
} | ||
|
||
ts.visitNodes(node.initializer.elements, (node: ts.Expression) => { | ||
const url = getResourceUrl(node); | ||
|
||
if (url) { | ||
resources.push(url); | ||
} | ||
|
||
return node; | ||
}); | ||
break; | ||
} | ||
|
||
return node; | ||
}, | ||
); | ||
} | ||
|
||
return resources; | ||
} |
49 changes: 49 additions & 0 deletions
49
packages/ngtools/webpack/src/transformers/find_resources_spec.ts
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,49 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies | ||
import * as ts from 'typescript'; | ||
import { findResources } from './find_resources'; | ||
|
||
|
||
describe('@ngtools/webpack transformers', () => { | ||
describe('find_resources', () => { | ||
it('should return resources', () => { | ||
const input = tags.stripIndent` | ||
import { Component } from '@angular/core'; | ||
@Component({ | ||
selector: 'app-root', | ||
templateUrl: './app.component.html', | ||
styleUrls: ['./app.component.css', './app.component.2.css'] | ||
}) | ||
export class AppComponent { | ||
title = 'app'; | ||
} | ||
`; | ||
|
||
const result = findResources(ts.createSourceFile('temp.ts', input, ts.ScriptTarget.ES2015)); | ||
expect(result).toEqual([ | ||
'./app.component.html', | ||
'./app.component.css', | ||
'./app.component.2.css', | ||
]); | ||
}); | ||
|
||
it('should not return resources if they are not in decorator', () => { | ||
const input = tags.stripIndent` | ||
const foo = { | ||
templateUrl: './app.component.html', | ||
styleUrls: ['./app.component.css', './app.component.2.css'] | ||
} | ||
`; | ||
|
||
const result = findResources(ts.createSourceFile('temp.ts', input, ts.ScriptTarget.ES2015)); | ||
expect(result).toEqual([]); | ||
}); | ||
}); | ||
}); |
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.