Skip to content

Commit

Permalink
fix(@ngtools/webpack): remove no longer needed component styles worka…
Browse files Browse the repository at this point in the history
…round

The underlying Angular compiler issue was corrected prior to the final release of 12.0.0 via angular/angular@a5fe8b9
  • Loading branch information
clydin authored and filipesilva committed Jun 18, 2021
1 parent b150093 commit 12c14b5
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 78 deletions.
6 changes: 0 additions & 6 deletions packages/ngtools/webpack/src/ivy/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import * as path from 'path';
import * as ts from 'typescript';
import { NgccProcessor } from '../ngcc_processor';
import { WebpackResourceLoader } from '../resource_loader';
import { workaroundStylePreprocessing } from '../transformers';
import { normalizePath } from './paths';

export function augmentHostWithResources(
Expand Down Expand Up @@ -363,11 +362,6 @@ export function augmentHostWithCaching(
);

if (file) {
// Temporary workaround for upstream transform resource defect
if (file && !file.isDeclarationFile && file.text.includes('@Component')) {
workaroundStylePreprocessing(file);
}

cache.set(fileName, file);
}

Expand Down
72 changes: 0 additions & 72 deletions packages/ngtools/webpack/src/transformers/replace_resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,75 +337,3 @@ function getDecoratorOrigin(

return null;
}

export function workaroundStylePreprocessing(sourceFile: ts.SourceFile): void {
const visitNode: ts.Visitor = (node: ts.Node) => {
if (ts.isClassDeclaration(node) && node.decorators?.length) {
for (const decorator of node.decorators) {
visitDecoratorWorkaround(decorator);
}
}

return ts.forEachChild(node, visitNode);
};

ts.forEachChild(sourceFile, visitNode);
}

function visitDecoratorWorkaround(node: ts.Decorator): void {
if (!ts.isCallExpression(node.expression)) {
return;
}

const decoratorFactory = node.expression;
if (
!ts.isIdentifier(decoratorFactory.expression) ||
decoratorFactory.expression.text !== 'Component'
) {
return;
}

const args = decoratorFactory.arguments;
if (args.length !== 1 || !ts.isObjectLiteralExpression(args[0])) {
// Unsupported component metadata
return;
}

const objectExpression = args[0] as ts.ObjectLiteralExpression;

// check if a `styles` property is present
let hasStyles = false;
for (const element of objectExpression.properties) {
if (!ts.isPropertyAssignment(element) || ts.isComputedPropertyName(element.name)) {
continue;
}

if (element.name.text === 'styles') {
hasStyles = true;
break;
}
}

if (hasStyles) {
return;
}

const nodeFactory = ts.factory;

// add a `styles` property to workaround upstream compiler defect
const emptyArray = nodeFactory.createArrayLiteralExpression();
const stylePropertyName = nodeFactory.createIdentifier('styles');
const styleProperty = nodeFactory.createPropertyAssignment(stylePropertyName, emptyArray);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(stylePropertyName.parent as any) = styleProperty;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(emptyArray.parent as any) = styleProperty;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(styleProperty.parent as any) = objectExpression;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(objectExpression.properties as any) = nodeFactory.createNodeArray([
...objectExpression.properties,
styleProperty,
]);
}

0 comments on commit 12c14b5

Please sign in to comment.