Skip to content

Commit 3bf3295

Browse files
filipesilvahansl
authored andcommitted
fix(@angular-devkit/build-optimizer): don't add imports to files with custom webpack require
Libraries using the custom `__webpack_require__` won't correctly load new imports. Partially address #6196.
1 parent dcb45b4 commit 3bf3295

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import * as ts from 'typescript';
1111
export function testImportTslib(content: string) {
1212
const regex = /var (__extends|__decorate|__metadata|__param) = \(.*\r?\n( .*\r?\n)*\};/;
1313

14-
return regex.test(content);
14+
// This transform introduces import/require() calls, but this won't work properly on libraries
15+
// built with Webpack. These libraries use __webpack_require__() calls instead, which will break
16+
// with a new import that wasn't part of it's original module list.
17+
// We ignore this transform for such libraries.
18+
const webpackRequireRegex = /__webpack_require__/;
19+
20+
return regex.test(content) && !webpackRequireRegex.test(content);
1521
}
1622

1723
export function getImportTslibTransformer(): ts.TransformerFactory<ts.SourceFile> {

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

+15
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,19 @@ describe('import-tslib', () => {
9696

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

0 commit comments

Comments
 (0)