Skip to content

Commit 2586a0e

Browse files
committed
fix(@angular-devkit/build-optimizer): remove decorators calls when tslib helpers are inlined
Closes #18682
1 parent 179b6ca commit 2586a0e

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

packages/angular_devkit/build_optimizer/src/transforms/scrub-file.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function isAngularCoreImport(node: ts.ImportDeclaration, isAngularCoreFile: bool
141141
}
142142

143143
// Relative imports from a Angular core file are also core imports.
144-
if (isAngularCoreFile && (importText.startsWith('./') || importText.startsWith('../'))) {
144+
if (isAngularCoreFile && importText.startsWith('.')) {
145145
return true;
146146
}
147147

@@ -571,11 +571,14 @@ function isTslibHelper(
571571
return false;
572572
}
573573

574-
for (const name of tslibImports) {
575-
for (const dec of symbol.declarations) {
576-
if (ts.isImportSpecifier(dec) && name.elements.includes(dec)) {
577-
return true;
578-
}
574+
for (const dec of symbol.declarations) {
575+
if (ts.isImportSpecifier(dec) && tslibImports.some(name => name.elements.includes(dec))) {
576+
return true;
577+
}
578+
579+
// Handle inline helpers `var __decorate = (this...`
580+
if (ts.isVariableDeclaration(dec)) {
581+
return true;
579582
}
580583
}
581584

packages/angular_devkit/build_optimizer/src/transforms/scrub-file_spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,49 @@ describe('scrub-file', () => {
118118
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
119119
});
120120

121+
it('removes Angular decorators calls when __decorate is inlined', () => {
122+
const output = tags.stripIndent`
123+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
124+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
125+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
126+
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;
127+
return c > 3 && r && Object.defineProperty(target, key, r), r;
128+
};
129+
130+
import { Component, Injectable } from '@angular/core';
131+
var Clazz = (function () {
132+
function Clazz() { }
133+
return Clazz;
134+
}());
135+
`;
136+
137+
const input = tags.stripIndent`
138+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
139+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
140+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
141+
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;
142+
return c > 3 && r && Object.defineProperty(target, key, r), r;
143+
};
144+
145+
import { Component, Injectable } from '@angular/core';
146+
var Clazz = (function () {
147+
function Clazz() { }
148+
Clazz = __decorate([
149+
Injectable(),
150+
Component({
151+
selector: 'app-root',
152+
templateUrl: './app.component.html',
153+
styleUrls: ['./app.component.css']
154+
})
155+
], Clazz);
156+
return Clazz;
157+
}());
158+
`;
159+
160+
expect(testScrubFile(input)).toBeTruthy();
161+
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
162+
});
163+
121164
it('removes constructor parameter metadata in __decorate', () => {
122165
const output = tags.stripIndent`
123166
import { __decorate, __metadata } from "tslib";

0 commit comments

Comments
 (0)