@@ -23,14 +23,51 @@ import {
2323 removePackageJsonDependency ,
2424} from '@schematics/angular/utility/dependencies' ;
2525import { JSONFile , JSONPath } from '@schematics/angular/utility/json-file' ;
26- import { getWorkspace } from '@schematics/angular/utility/workspace' ;
26+ import { getWorkspace , updateWorkspace } from '@schematics/angular/utility/workspace' ;
2727import { Builders } from '@schematics/angular/utility/workspace-models' ;
2828
2929import { Schema } from './schema' ;
3030
3131const localizeType = `@angular/localize` ;
32+ const localizePolyfill = '@angular/localize/init' ;
3233const localizeTripleSlashType = `/// <reference types="@angular/localize" />` ;
3334
35+ function addPolyfillToConfig ( projectName : string ) : Rule {
36+ return updateWorkspace ( ( workspace ) => {
37+ const project = workspace . projects . get ( projectName ) ;
38+ if ( ! project ) {
39+ throw new SchematicsException ( `Invalid project name '${ projectName } '.` ) ;
40+ }
41+
42+ const isLocalizePolyfill = ( path : string ) => path . startsWith ( '@angular/localize' ) ;
43+
44+ for ( const target of project . targets . values ( ) ) {
45+ switch ( target . builder ) {
46+ case Builders . Karma :
47+ case Builders . Server :
48+ case Builders . Browser :
49+ case Builders . BrowserEsbuild :
50+ case Builders . Application :
51+ target . options ??= { } ;
52+ const value = target . options [ 'polyfills' ] ;
53+ if ( typeof value === 'string' ) {
54+ if ( ! isLocalizePolyfill ( value ) ) {
55+ target . options [ 'polyfills' ] = [ value , localizePolyfill ] ;
56+ }
57+ } else if ( Array . isArray ( value ) ) {
58+ if ( ! ( value as string [ ] ) . some ( isLocalizePolyfill ) ) {
59+ value . push ( localizePolyfill ) ;
60+ }
61+ } else {
62+ target . options [ 'polyfills' ] = [ localizePolyfill ] ;
63+ }
64+
65+ break ;
66+ }
67+ }
68+ } ) ;
69+ }
70+
3471function addTypeScriptConfigTypes ( projectName : string ) : Rule {
3572 return async ( host : Tree ) => {
3673 const workspace = await getWorkspace ( host ) ;
@@ -45,6 +82,7 @@ function addTypeScriptConfigTypes(projectName: string): Rule {
4582 switch ( target . builder ) {
4683 case Builders . Karma :
4784 case Builders . Server :
85+ case Builders . BrowserEsbuild :
4886 case Builders . Browser :
4987 case Builders . Application :
5088 const value = target . options ?. [ 'tsConfig' ] ;
@@ -55,7 +93,7 @@ function addTypeScriptConfigTypes(projectName: string): Rule {
5593 break ;
5694 }
5795
58- if ( target . builder === Builders . Browser ) {
96+ if ( target . builder === Builders . Browser || target . builder === Builders . BrowserEsbuild ) {
5997 const value = target . options ?. [ 'main' ] ;
6098 if ( typeof value === 'string' ) {
6199 addTripleSlashType ( host , value ) ;
@@ -132,6 +170,7 @@ export default function (options: Schema): Rule {
132170
133171 return chain ( [
134172 addTypeScriptConfigTypes ( projectName ) ,
173+ addPolyfillToConfig ( projectName ) ,
135174 // If `$localize` will be used at runtime then must install `@angular/localize`
136175 // into `dependencies`, rather than the default of `devDependencies`.
137176 options . useAtRuntime ? moveToDependencies : noop ( ) ,
0 commit comments