Skip to content

Commit 081edf3

Browse files
alan-agius4kyliau
authored andcommitted
fix(@angular-devkit/build-optimizer): add tslib replacement at top of file
Fixes #12568
1 parent 395df5f commit 081edf3

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

packages/angular_devkit/build_optimizer/src/build-optimizer/build-optimizer_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ describe('build-optimizer', () => {
5858
`;
5959
// tslint:disable:max-line-length
6060
const output = tags.oneLine`
61-
${imports}
6261
import { __extends } from "tslib";
62+
${imports}
6363
var ChangeDetectionStrategy = /*@__PURE__*/ (function (ChangeDetectionStrategy) {
6464
ChangeDetectionStrategy[ChangeDetectionStrategy["OnPush"] = 0] = "OnPush";
6565
ChangeDetectionStrategy[ChangeDetectionStrategy["Default"] = 1] = "Default";

packages/angular_devkit/build_optimizer/src/transforms/import-tslib.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as ts from 'typescript';
1111
* @deprecated From 0.9.0
1212
*/
1313
export function testImportTslib(content: string) {
14-
const regex = /var (__extends|__decorate|__metadata|__param) = \(.*\r?\n( .*\r?\n)*\};/;
14+
const regex = /var (__extends|__decorate|__metadata|__param) = \(.*\r?\n\s+(.*\r?\n)*\s*\};/;
1515

1616
return regex.test(content);
1717
}
@@ -21,10 +21,12 @@ export function getImportTslibTransformer(): ts.TransformerFactory<ts.SourceFile
2121

2222
const transformer: ts.Transformer<ts.SourceFile> = (sf: ts.SourceFile) => {
2323

24+
const tslibImports: (ts.VariableStatement | ts.ImportDeclaration)[] = [];
25+
2426
// Check if module has CJS exports. If so, use 'require()' instead of 'import'.
2527
const useRequire = /exports.\S+\s*=/.test(sf.getText());
2628

27-
const visitor: ts.Visitor = (node: ts.Node): ts.Node => {
29+
const visitor: ts.Visitor = (node: ts.Node): ts.Node | undefined => {
2830

2931
// Check if node is a TS helper declaration and replace with import if yes
3032
if (ts.isVariableStatement(node)) {
@@ -35,23 +37,36 @@ export function getImportTslibTransformer(): ts.TransformerFactory<ts.SourceFile
3537

3638
if (isHelperName(name)) {
3739
// TODO: maybe add a few more checks, like checking the first part of the assignment.
40+
const tslibImport = createTslibImport(name, useRequire);
41+
tslibImports.push(tslibImport);
3842

39-
return createTslibImport(name, useRequire);
43+
return undefined;
4044
}
4145
}
4246
}
4347

4448
return ts.visitEachChild(node, visitor, context);
4549
};
4650

47-
return ts.visitEachChild(sf, visitor, context);
51+
const sfUpdated = ts.visitEachChild(sf, visitor, context);
52+
53+
// Add tslib imports before any other statement
54+
return tslibImports.length > 0
55+
? ts.updateSourceFileNode(sfUpdated, [
56+
...tslibImports,
57+
...sfUpdated.statements,
58+
])
59+
: sfUpdated;
4860
};
4961

5062
return transformer;
5163
};
5264
}
5365

54-
function createTslibImport(name: string, useRequire = false): ts.Node {
66+
function createTslibImport(
67+
name: string,
68+
useRequire = false,
69+
): ts.VariableStatement | ts.ImportDeclaration {
5570
if (useRequire) {
5671
// Use `var __helper = /*@__PURE__*/ require("tslib").__helper`.
5772
const requireCall = ts.createCall(ts.createIdentifier('require'), undefined,

packages/angular_devkit/build_optimizer/src/transforms/import-tslib_spec.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ describe('import-tslib', () => {
3131
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
3232
});
3333

34+
it('replaces wrapped __extends', () => {
35+
// tslint:disable:max-line-length
36+
const input = tags.stripIndent`
37+
export default function appGlobal() {
38+
var __extends = (this && this.__extends) || function (d, b) {
39+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
40+
function __() { this.constructor = d; }
41+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
42+
};
43+
}
44+
`;
45+
const output = tags.stripIndent`
46+
import { __extends } from "tslib";
47+
export default function appGlobal() { }
48+
`;
49+
50+
expect(testImportTslib(input)).toBeTruthy();
51+
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
52+
});
53+
3454
it('replaces __decorate', () => {
3555
// tslint:disable:max-line-length
3656
const input = tags.stripIndent`
@@ -98,18 +118,4 @@ describe('import-tslib', () => {
98118
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
99119
});
100120

101-
it('tests false for files using __webpack_require__', () => {
102-
const input = tags.stripIndent`
103-
function __webpack_require__(moduleId) {
104-
var __extends = (this && this.__extends) || function (d, b) {
105-
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
106-
function __() { this.constructor = d; }
107-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
108-
};
109-
exports.meaning = 42;
110-
}
111-
`;
112-
113-
expect(testImportTslib(input)).toBeFalsy();
114-
});
115121
});

0 commit comments

Comments
 (0)