fix(ivy): move setClassMetadata calls into a pure iife (#33337)
This commit transforms the setClassMetadata calls generated by ngtsc from: ```typescript /*@__PURE__*/ setClassMetadata(...); ``` to: ```typescript /*@__PURE__*/ (function() { setClassMetadata(...); })(); ``` Without the IIFE, terser won't remove these function calls because the function calls have arguments that themselves are function calls or other impure expressions. In order to make the whole block be DCE-ed by terser, we wrap it into IIFE and mark the IIFE as pure. It should be noted that this change doesn't have any impact on CLI* with build-optimizer, which removes the whole setClassMetadata block within the webpack loader, so terser or webpack itself don't get to see it at all. This is done to prevent cross-chunk retention issues caused by webpack's internal module registry. * actually we do expect a short-term size regression while https://github.com/angular/angular-cli/pull/16228 is merged and released in the next rc of the CLI. But long term this change does nothing to CLI + build-optimizer configuration and is done primarly to correct the seemingly correct but non-function PURE annotation that builds not using build-optimizer could rely on. PR Close #33337
This commit is contained in:
@ -139,10 +139,10 @@ runInEachFileSystem(() => {
|
||||
const jsContents = fs.readFile(_(`/node_modules/test-package/index.js`)).replace(/\s+/g, ' ');
|
||||
expect(jsContents)
|
||||
.toContain(
|
||||
'/*@__PURE__*/ ɵngcc0.ɵsetClassMetadata(FooDirective, ' +
|
||||
'/*@__PURE__*/ (function () { ɵngcc0.ɵsetClassMetadata(FooDirective, ' +
|
||||
'[{ type: Directive, args: [{ selector: \'[foo]\' }] }], ' +
|
||||
'function () { return []; }, ' +
|
||||
'{ bar: [{ type: Input }] });');
|
||||
'{ bar: [{ type: Input }] }); })();');
|
||||
});
|
||||
|
||||
it('should not add `const` in ES5 generated code', () => {
|
||||
|
@ -222,10 +222,10 @@ A.ɵcmp = ɵngcc0.ɵɵdefineComponent({ type: A, selectors: [["a"]], decls: 1, v
|
||||
|
||||
const addAdjacentStatementsSpy = testFormatter.addAdjacentStatements as jasmine.Spy;
|
||||
expect(addAdjacentStatementsSpy.calls.first().args[2]).toEqual(`// TRANSPILED
|
||||
/*@__PURE__*/ ɵngcc0.ɵsetClassMetadata(A, [{
|
||||
/*@__PURE__*/ (function () { ɵngcc0.ɵsetClassMetadata(A, [{
|
||||
type: Component,
|
||||
args: [{ selector: 'a', template: '{{ person!.name }}' }]
|
||||
}], null, null);`);
|
||||
}], null, null); })();`);
|
||||
});
|
||||
|
||||
|
||||
@ -274,10 +274,10 @@ A.ɵdir = ɵngcc0.ɵɵdefineDirective({ type: A, selectors: [["", "a", ""]] });`
|
||||
.toEqual(jasmine.objectContaining(
|
||||
{name: 'A', decorators: [jasmine.objectContaining({name: 'Directive'})]}));
|
||||
expect(addAdjacentStatementsSpy.calls.first().args[2]).toEqual(`// TRANSPILED
|
||||
/*@__PURE__*/ ɵngcc0.ɵsetClassMetadata(A, [{
|
||||
/*@__PURE__*/ (function () { ɵngcc0.ɵsetClassMetadata(A, [{
|
||||
type: Directive,
|
||||
args: [{ selector: '[a]' }]
|
||||
}], null, null);`);
|
||||
}], null, null); })();`);
|
||||
});
|
||||
|
||||
it('should call removeDecorators with the source code, a map of class decorators that have been analyzed',
|
||||
@ -568,7 +568,7 @@ UndecoratedBase.ɵdir = ɵngcc0.ɵɵdefineDirective({ type: UndecoratedBase, vie
|
||||
decorationAnalyses, switchMarkerAnalyses, privateDeclarationsAnalyses);
|
||||
const addAdjacentStatementsSpy = testFormatter.addAdjacentStatements as jasmine.Spy;
|
||||
expect(addAdjacentStatementsSpy.calls.first().args[2])
|
||||
.toContain(`/*@__PURE__*/ ɵngcc0.setClassMetadata(`);
|
||||
.toContain(`/*@__PURE__*/ (function () { ɵngcc0.setClassMetadata(`);
|
||||
const addImportsSpy = testFormatter.addImports as jasmine.Spy;
|
||||
expect(addImportsSpy.calls.first().args[1]).toEqual([
|
||||
{specifier: './r3_symbols', qualifier: 'ɵngcc0'}
|
||||
@ -588,7 +588,7 @@ UndecoratedBase.ɵdir = ɵngcc0.ɵɵdefineDirective({ type: UndecoratedBase, vie
|
||||
decorationAnalyses, switchMarkerAnalyses, privateDeclarationsAnalyses);
|
||||
const addAdjacentStatementsSpy = testFormatter.addAdjacentStatements as jasmine.Spy;
|
||||
expect(addAdjacentStatementsSpy.calls.first().args[2])
|
||||
.toContain(`/*@__PURE__*/ setClassMetadata(`);
|
||||
.toContain(`/*@__PURE__*/ (function () { setClassMetadata(`);
|
||||
const addImportsSpy = testFormatter.addImports as jasmine.Spy;
|
||||
expect(addImportsSpy.calls.first().args[1]).toEqual([]);
|
||||
});
|
||||
|
Reference in New Issue
Block a user