Skip to content

Commit b2f69ee

Browse files
clydinhansl
authored andcommitted
fix(@angular-devkit/build-optimizer): limit actions on webpack bundles
1 parent 44e1b23 commit b2f69ee

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ export function buildOptimizer(options: BuildOptimizerOptions): TransformJavascr
8787
content = readFileSync(inputFilePath as string, 'UTF-8');
8888
}
8989

90+
if (!content) {
91+
return {
92+
content: null,
93+
sourceMap: null,
94+
emitSkipped: true,
95+
};
96+
}
97+
98+
const isWebpackBundle = content.indexOf('__webpack_require__') !== -1;
99+
90100
// Determine which transforms to apply.
91101
const getTransforms = [];
92102

@@ -119,7 +129,11 @@ export function buildOptimizer(options: BuildOptimizerOptions): TransformJavascr
119129
getTransforms.unshift(getPrefixClassesTransformer);
120130
}
121131

122-
if (ignoreTest || testImportTslib(content)) {
132+
// This transform introduces import/require() calls, but this won't work properly on libraries
133+
// built with Webpack. These libraries use __webpack_require__() calls instead, which will break
134+
// with a new import that wasn't part of it's original module list.
135+
// We ignore this transform for such libraries.
136+
if (!isWebpackBundle && (ignoreTest || testImportTslib(content))) {
123137
getTransforms.unshift(getImportTslibTransformer);
124138
}
125139

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ 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-
// 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);
14+
return regex.test(content);
2115
}
2216

2317
export function getImportTslibTransformer(): ts.TransformerFactory<ts.SourceFile> {
@@ -35,8 +29,7 @@ export function getImportTslibTransformer(): ts.TransformerFactory<ts.SourceFile
3529
const declarations = node.declarationList.declarations;
3630

3731
if (declarations.length === 1 && ts.isIdentifier(declarations[0].name)) {
38-
// NOTE: the replace is unnecessary with TS2.5+; tests currently run with TS2.4
39-
const name = (declarations[0].name as ts.Identifier).text.replace(/^___/, '__');
32+
const name = (declarations[0].name as ts.Identifier).text;
4033

4134
if (isHelperName(name)) {
4235
// TODO: maybe add a few more checks, like checking the first part of the assignment.

0 commit comments

Comments
 (0)