@@ -4,19 +4,23 @@ use serde_json::json;
44
55use crate :: { codeblock:: CodeBlock , Config , Context , Result } ;
66
7- pub ( super ) struct Writer {
7+ pub ( super ) struct Writer < ' a > {
88 changed_only : bool ,
9+ config : & ' a Config ,
910 chapter_to_angular_file : Vec < ( String , String ) > ,
1011}
1112
12- impl Writer {
13- pub ( super ) fn new ( changed_only : bool ) -> Self {
13+ impl < ' a > Writer < ' a > {
14+ pub ( super ) fn new ( config : & ' a Config , changed_only : bool ) -> Self {
1415 Self {
1516 changed_only,
17+ config,
1618 chapter_to_angular_file : Vec :: new ( ) ,
1719 }
1820 }
21+ }
1922
23+ impl Writer < ' _ > {
2024 pub ( super ) fn write < P : AsRef < Path > > ( & self , path : P , contents : & str ) -> Result < ( ) > {
2125 if self . changed_only
2226 && matches ! ( fs:: read_to_string( & path) , Ok ( existing) if existing. eq( contents) )
@@ -52,19 +56,34 @@ impl Writer {
5256
5357 let mut main_script = Vec :: with_capacity ( 1 + code_blocks. len ( ) ) ;
5458
55- main_script. push (
56- "\n \
57- import {NgZone, type ApplicationRef, type Provider, type EnvironmentProviders, type Type} from '@angular/core';\n \
58- import {bootstrapApplication} from '@angular/platform-browser';\n \
59- const zone = new NgZone({});\n \
60- function makeProviders(component: Type<unknown> & {rootProviders?: readonly (Provider | EnvironmentProviders)[] | null | undefined}) {\n \
61- return [{provide: NgZone, useValue: zone}, ...(component.rootProviders ?? [])];\n \
62- }\n \
63- const applications: Promise<ApplicationRef>[] = [];\n \
64- (globalThis as any).mdBookAngular = {zone, applications};\n \
65- "
66- . to_owned ( ) ,
67- ) ;
59+ if self . config . zoneless {
60+ main_script. push (
61+ "\n \
62+ import {provideZonelessChangeDetection, type ApplicationRef, type Provider, type EnvironmentProviders, type Type} from '@angular/core';\n \
63+ import {bootstrapApplication} from '@angular/platform-browser';\n \
64+ function makeProviders(component: Type<unknown> & {rootProviders?: readonly (Provider | EnvironmentProviders)[] | null | undefined}) {\n \
65+ return [provideZonelessChangeDetection(), ...(component.rootProviders ?? [])];\n \
66+ }\n \
67+ const applications: Promise<ApplicationRef>[] = [];\n \
68+ (globalThis as any).mdBookAngular = {zone: null, applications};\n \
69+ "
70+ . to_owned ( ) ,
71+ ) ;
72+ } else {
73+ main_script. push (
74+ "\n \
75+ import {NgZone, type ApplicationRef, type Provider, type EnvironmentProviders, type Type} from '@angular/core';\n \
76+ import {bootstrapApplication} from '@angular/platform-browser';\n \
77+ const zone = new NgZone({});\n \
78+ function makeProviders(component: Type<unknown> & {rootProviders?: readonly (Provider | EnvironmentProviders)[] | null | undefined}) {\n \
79+ return [{provide: NgZone, useValue: zone}, ...(component.rootProviders ?? [])];\n \
80+ }\n \
81+ const applications: Promise<ApplicationRef>[] = [];\n \
82+ (globalThis as any).mdBookAngular = {zone, applications};\n \
83+ "
84+ . to_owned ( ) ,
85+ ) ;
86+ }
6887
6988 for ( code_block_index, code_block) in code_blocks. into_iter ( ) . enumerate ( ) {
7089 self . write (
@@ -99,15 +118,12 @@ impl Writer {
99118 Ok ( ( ) )
100119 }
101120
102- pub ( super ) fn write_main < P : AsRef < Path > > ( & self , config : & Config , root : P ) -> Result < ( ) > {
103- let mut main_script =
104- Vec :: with_capacity ( 3 + config. polyfills . len ( ) + self . chapter_to_angular_file . len ( ) ) ;
105-
106- if !config. polyfills . contains ( & "zone.js" . to_owned ( ) ) {
107- main_script. push ( "import 'zone.js';" . to_owned ( ) ) ;
108- }
121+ pub ( super ) fn write_main < P : AsRef < Path > > ( & self , root : P ) -> Result < ( ) > {
122+ let mut main_script = Vec :: with_capacity (
123+ 3 + self . config . polyfills . len ( ) + self . chapter_to_angular_file . len ( ) ,
124+ ) ;
109125
110- for polyfill in & config. polyfills {
126+ for polyfill in & self . config . polyfills {
111127 main_script. push ( format ! ( "import '{polyfill}';" ) ) ;
112128 }
113129
@@ -129,8 +145,8 @@ impl Writer {
129145 Ok ( ( ) )
130146 }
131147
132- pub ( super ) fn write_tsconfig ( & self , config : & Config ) -> Result < ( ) > {
133- let tsconfig = if let Some ( tsconfig) = & config. tsconfig {
148+ pub ( super ) fn write_tsconfig ( & self ) -> Result < ( ) > {
149+ let tsconfig = if let Some ( tsconfig) = & self . config . tsconfig {
134150 json ! ( { "extends" : tsconfig. to_string_lossy( ) } )
135151 } else {
136152 json ! ( {
@@ -149,7 +165,7 @@ impl Writer {
149165 } ;
150166
151167 self . write (
152- config. angular_root_folder . join ( "tsconfig.json" ) ,
168+ self . config . angular_root_folder . join ( "tsconfig.json" ) ,
153169 & serde_json:: to_string ( & tsconfig) ?,
154170 )
155171 . context ( "failed to write tsconfig.json" ) ?;
0 commit comments