Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1385d7a

Browse files
author
Dimitar Tachev
authoredFeb 6, 2019
chore(ns-support-hmr-ng transformer): support app options variable (#790)
1 parent 6a9ce33 commit 1385d7a

File tree

2 files changed

+232
-190
lines changed

2 files changed

+232
-190
lines changed
 

‎transformers/ns-support-hmr-ng.spec.ts

Lines changed: 222 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { tags } from "@angular-devkit/core";
22
import { createTypescriptContext, transformTypescript } from "@ngtools/webpack/src/transformers";
33
import { nsReplaceBootstrap } from './ns-replace-bootstrap';
4-
import { nsSupportHmrNg, getHandleHmrOptionsCode, getAcceptMainModuleCode, GeneratedDynamicAppOptions } from "./ns-support-hmr-ng";
4+
import { nsSupportHmrNg, getHmrOptionsCode, getAcceptMainModuleCode, GeneratedDynamicAppOptions } from "./ns-support-hmr-ng";
55
import { AngularCompilerPlugin } from "@ngtools/webpack";
66

77
describe("@ngtools/webpack transformers", () => {
@@ -14,282 +14,321 @@ describe("@ngtools/webpack transformers", () => {
1414
const handleHmrOptionsDeclaration = `var ${GeneratedDynamicAppOptions} = {};`;
1515
const nsStaticPlatformCall = `nativescript_angular_platform_Generated.platformNativeScript`;
1616
const nsDynamicPlatformCall = `nativescript_angular_platform_Generated.platformNativeScriptDynamic`;
17-
const handleHmrOptionsCode = getHandleHmrOptionsCode("AppModule", "./app/app.module");
17+
const handleHmrOptionsCode = getHmrOptionsCode("AppModule", "./app/app.module");
1818
const acceptMainModuleCode = getAcceptMainModuleCode("./app/app.module");
19-
const handleHmrOptionsAotCode = getHandleHmrOptionsCode("AppModuleNgFactory", "./test-file.ts.ngfactory");
19+
const handleHmrOptionsAotCode = getHmrOptionsCode("AppModuleNgFactory", "./test-file.ts.ngfactory");
2020
const acceptMainModuleAotCode = getAcceptMainModuleCode("./test-file.ts.ngfactory");
21+
const getAppOptions = (currentAppOptionsString?: string) => {
22+
return `Object.assign(${currentAppOptionsString || "{}"}, ${GeneratedDynamicAppOptions})`
23+
};
24+
const defaultAppOptions = getAppOptions();
2125
const testCases = [{
2226
name: "should handle HMR when platformNativeScriptDynamic is called without arguments",
2327
rawFile: `
24-
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
25-
import { AppModule } from "./app/app.module";
26-
platformNativeScriptDynamic().bootstrapModule(AppModule);
27-
`,
28+
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
29+
import { AppModule } from "./app/app.module";
30+
platformNativeScriptDynamic().bootstrapModule(AppModule);
31+
`,
2832
transformedFile: `
29-
${handleHmrPlatformDynamicImport}
30-
import { AppModule } from "./app/app.module";
33+
${handleHmrPlatformDynamicImport}
34+
import { AppModule } from "./app/app.module";
3135
32-
${handleHmrOptionsDeclaration}
33-
${handleHmrOptionsCode}
34-
${acceptMainModuleCode}
36+
${handleHmrOptionsDeclaration}
37+
${handleHmrOptionsCode}
38+
${acceptMainModuleCode}
3539
36-
${nsDynamicPlatformCall}(${GeneratedDynamicAppOptions}).bootstrapModule(AppModule);
37-
`,
40+
${nsDynamicPlatformCall}(${defaultAppOptions}).bootstrapModule(AppModule);
41+
`,
3842
transformedFileWithAot: `
39-
${handleHmrPlatformStaticImport}
40-
${handleAotPlatformStaticImport}
41-
${handleAotNgFactoryImport}
43+
${handleHmrPlatformStaticImport}
44+
${handleAotPlatformStaticImport}
45+
${handleAotNgFactoryImport}
4246
43-
${handleHmrOptionsDeclaration}
44-
${handleHmrOptionsAotCode}
45-
${acceptMainModuleAotCode}
47+
${handleHmrOptionsDeclaration}
48+
${handleHmrOptionsAotCode}
49+
${acceptMainModuleAotCode}
4650
47-
${nsStaticPlatformCall}(${GeneratedDynamicAppOptions}).bootstrapModuleFactory(${nsFactoryImportName}.AppModuleNgFactory);
48-
`
51+
${nsStaticPlatformCall}(${defaultAppOptions}).bootstrapModuleFactory(${nsFactoryImportName}.AppModuleNgFactory);
52+
`
4953
},
5054
{
5155
name: "should not handle HMR when the AppModule import cannot be found",
5256
rawFile: `
53-
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
54-
platformNativeScriptDynamic().bootstrapModule(SyntaxErrorModule);
55-
`,
57+
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
58+
platformNativeScriptDynamic().bootstrapModule(SyntaxErrorModule);
59+
`,
5660
transformedFile: `
57-
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
58-
platformNativeScriptDynamic().bootstrapModule(SyntaxErrorModule);
59-
`,
61+
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
62+
platformNativeScriptDynamic().bootstrapModule(SyntaxErrorModule);
63+
`,
6064
transformedFileWithAot: `
61-
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
62-
platformNativeScriptDynamic().bootstrapModule(SyntaxErrorModule);
63-
`
65+
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
66+
platformNativeScriptDynamic().bootstrapModule(SyntaxErrorModule);
67+
`
6468
},
6569
{
6670
name: "(known limitation) should not handle HMR when the platformNativeScriptDynamic method is renamed",
6771
rawFile: `
68-
import { platformNativeScriptDynamic as x } from "nativescript-angular/platform";
69-
x().bootstrapModule(SyntaxErrorModule);
70-
`,
72+
import { platformNativeScriptDynamic as x } from "nativescript-angular/platform";
73+
x().bootstrapModule(SyntaxErrorModule);
74+
`,
7175
transformedFile: `
72-
import { platformNativeScriptDynamic as x } from "nativescript-angular/platform";
73-
x().bootstrapModule(SyntaxErrorModule);
74-
`,
76+
import { platformNativeScriptDynamic as x } from "nativescript-angular/platform";
77+
x().bootstrapModule(SyntaxErrorModule);
78+
`,
7579
transformedFileWithAot: `
76-
import { platformNativeScriptDynamic as x } from "nativescript-angular/platform";
77-
x().bootstrapModule(SyntaxErrorModule);
78-
`
80+
import { platformNativeScriptDynamic as x } from "nativescript-angular/platform";
81+
x().bootstrapModule(SyntaxErrorModule);
82+
`
7983
},
8084
{
8185
name: "(known limitation) should not handle HMR when the bootstrapModule method is renamed",
8286
rawFile: `
83-
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
87+
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
8488
85-
const x = platformNativeScriptDynamic().bootstrapModule;
86-
x(SyntaxErrorModule);
87-
`,
89+
const x = platformNativeScriptDynamic().bootstrapModule;
90+
x(SyntaxErrorModule);
91+
`,
8892
transformedFile: `
89-
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
93+
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
9094
91-
const x = platformNativeScriptDynamic().bootstrapModule;
92-
x(SyntaxErrorModule);
93-
`,
95+
const x = platformNativeScriptDynamic().bootstrapModule;
96+
x(SyntaxErrorModule);
97+
`,
9498
transformedFileWithAot: `
95-
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
99+
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
96100
97-
const x = platformNativeScriptDynamic().bootstrapModule;
98-
x(SyntaxErrorModule);
99-
`
101+
const x = platformNativeScriptDynamic().bootstrapModule;
102+
x(SyntaxErrorModule);
103+
`
100104
},
101105
{
102106
name: "should handle HMR when AOT is manually configured",
103107
rawFile: `
104-
import { platformNativeScript } from "nativescript-angular/platform-static";
105-
import { AppModuleNgFactory } from "./app/app.module.ngfactory";
106-
platformNativeScript().bootstrapModuleFactory(AppModuleNgFactory);
107-
`,
108+
import { platformNativeScript } from "nativescript-angular/platform-static";
109+
import { AppModuleNgFactory } from "./app/app.module.ngfactory";
110+
platformNativeScript().bootstrapModuleFactory(AppModuleNgFactory);
111+
`,
108112
transformedFile: `
109-
${handleHmrPlatformStaticImport}
110-
import { AppModuleNgFactory } from "./app/app.module.ngfactory";
113+
${handleHmrPlatformStaticImport}
114+
import { AppModuleNgFactory } from "./app/app.module.ngfactory";
111115
112-
${handleHmrOptionsDeclaration}
113-
${getHandleHmrOptionsCode("AppModuleNgFactory", "./app/app.module.ngfactory")}
114-
${getAcceptMainModuleCode("./app/app.module.ngfactory")}
116+
${handleHmrOptionsDeclaration}
117+
${getHmrOptionsCode("AppModuleNgFactory", "./app/app.module.ngfactory")}
118+
${getAcceptMainModuleCode("./app/app.module.ngfactory")}
115119
116-
${nsStaticPlatformCall}(${GeneratedDynamicAppOptions}).bootstrapModuleFactory(AppModuleNgFactory);
117-
`,
120+
${nsStaticPlatformCall}(${defaultAppOptions}).bootstrapModuleFactory(AppModuleNgFactory);
121+
`,
118122
transformedFileWithAot: `
119-
${handleHmrPlatformStaticImport}
120-
import { AppModuleNgFactory } from "./app/app.module.ngfactory";
123+
${handleHmrPlatformStaticImport}
124+
import { AppModuleNgFactory } from "./app/app.module.ngfactory";
121125
122-
${handleHmrOptionsDeclaration}
123-
${getHandleHmrOptionsCode("AppModuleNgFactory", "./app/app.module.ngfactory")}
124-
${getAcceptMainModuleCode("./app/app.module.ngfactory")}
126+
${handleHmrOptionsDeclaration}
127+
${getHmrOptionsCode("AppModuleNgFactory", "./app/app.module.ngfactory")}
128+
${getAcceptMainModuleCode("./app/app.module.ngfactory")}
125129
126-
${nsStaticPlatformCall}(${GeneratedDynamicAppOptions}).bootstrapModuleFactory(AppModuleNgFactory);
127-
`
130+
${nsStaticPlatformCall}(${defaultAppOptions}).bootstrapModuleFactory(AppModuleNgFactory);
131+
`
128132
},
129133
{
130134
name: "should handle HMR when platformNativeScriptDynamic is called without arguments and non default app module",
131135
customAppModuleName: "CustomModule",
132136
rawFile: `
133-
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
134-
import { CustomModule } from "./custom/custom.module";
135-
platformNativeScriptDynamic().bootstrapModule(CustomModule);
136-
`,
137+
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
138+
import { CustomModule } from "./custom/custom.module";
139+
platformNativeScriptDynamic().bootstrapModule(CustomModule);
140+
`,
137141
transformedFile: `
138-
${handleHmrPlatformDynamicImport}
139-
import { CustomModule } from "./custom/custom.module";
142+
${handleHmrPlatformDynamicImport}
143+
import { CustomModule } from "./custom/custom.module";
140144
141-
${handleHmrOptionsDeclaration}
142-
${getHandleHmrOptionsCode("CustomModule", "./custom/custom.module")}
143-
${getAcceptMainModuleCode("./custom/custom.module")}
145+
${handleHmrOptionsDeclaration}
146+
${getHmrOptionsCode("CustomModule", "./custom/custom.module")}
147+
${getAcceptMainModuleCode("./custom/custom.module")}
144148
145-
${nsDynamicPlatformCall}(${GeneratedDynamicAppOptions}).bootstrapModule(CustomModule);
146-
`,
149+
${nsDynamicPlatformCall}(${defaultAppOptions}).bootstrapModule(CustomModule);
150+
`,
147151
transformedFileWithAot: `
148-
${handleHmrPlatformStaticImport}
149-
${handleAotPlatformStaticImport}
150-
${handleAotNgFactoryImport}
152+
${handleHmrPlatformStaticImport}
153+
${handleAotPlatformStaticImport}
154+
${handleAotNgFactoryImport}
151155
152-
${handleHmrOptionsDeclaration}
153-
${getHandleHmrOptionsCode("CustomModuleNgFactory", "./test-file.ts.ngfactory")}
154-
${getAcceptMainModuleCode("./test-file.ts.ngfactory")}
156+
${handleHmrOptionsDeclaration}
157+
${getHmrOptionsCode("CustomModuleNgFactory", "./test-file.ts.ngfactory")}
158+
${getAcceptMainModuleCode("./test-file.ts.ngfactory")}
155159
156-
${nsStaticPlatformCall}(${GeneratedDynamicAppOptions}).bootstrapModuleFactory(${nsFactoryImportName}.CustomModuleNgFactory);
157-
`
160+
${nsStaticPlatformCall}(${defaultAppOptions}).bootstrapModuleFactory(${nsFactoryImportName}.CustomModuleNgFactory);
161+
`
158162
},
159163
{
160164
name: "should handle HMR when platformNativeScriptDynamic is called from * import",
161165
rawFile: `
162-
import * as nsNgPlatform from "nativescript-angular/platform";
163-
import { AppModule } from "./app/app.module";
164-
nsNgPlatform.platformNativeScriptDynamic().bootstrapModule(AppModule);
165-
`,
166+
import * as nsNgPlatform from "nativescript-angular/platform";
167+
import { AppModule } from "./app/app.module";
168+
nsNgPlatform.platformNativeScriptDynamic().bootstrapModule(AppModule);
169+
`,
166170
transformedFile: `
167-
${handleHmrPlatformDynamicImport}
168-
import { AppModule } from "./app/app.module";
171+
${handleHmrPlatformDynamicImport}
172+
import { AppModule } from "./app/app.module";
169173
170-
${handleHmrOptionsDeclaration}
171-
${handleHmrOptionsCode}
172-
${acceptMainModuleCode}
174+
${handleHmrOptionsDeclaration}
175+
${handleHmrOptionsCode}
176+
${acceptMainModuleCode}
173177
174-
${nsDynamicPlatformCall}(${GeneratedDynamicAppOptions}).bootstrapModule(AppModule);
175-
`,
178+
${nsDynamicPlatformCall}(${defaultAppOptions}).bootstrapModule(AppModule);
179+
`,
176180
transformedFileWithAot: `
177-
${handleHmrPlatformStaticImport}
178-
${handleAotPlatformStaticImport}
179-
${handleAotNgFactoryImport}
181+
${handleHmrPlatformStaticImport}
182+
${handleAotPlatformStaticImport}
183+
${handleAotNgFactoryImport}
180184
181-
${handleHmrOptionsDeclaration}
182-
${handleHmrOptionsAotCode}
183-
${acceptMainModuleAotCode}
185+
${handleHmrOptionsDeclaration}
186+
${handleHmrOptionsAotCode}
187+
${acceptMainModuleAotCode}
184188
185-
${nsStaticPlatformCall}(${GeneratedDynamicAppOptions}).bootstrapModuleFactory(${nsFactoryImportName}.AppModuleNgFactory);
186-
`
189+
${nsStaticPlatformCall}(${defaultAppOptions}).bootstrapModuleFactory(${nsFactoryImportName}.AppModuleNgFactory);
190+
`
187191
},
188192
{
189193
name: "should handle HMR when platformNativeScriptDynamic is called with inline appOptions",
190194
rawFile: `
191-
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
192-
import { AppModule } from "./app/app.module";
193-
platformNativeScriptDynamic({ bootInExistingPage: true }).bootstrapModule(AppModule);
194-
`,
195+
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
196+
import { AppModule } from "./app/app.module";
197+
platformNativeScriptDynamic({ bootInExistingPage: true }).bootstrapModule(AppModule);
198+
`,
195199
transformedFile: `
196-
${handleHmrPlatformDynamicImport}
197-
import { AppModule } from "./app/app.module";
200+
${handleHmrPlatformDynamicImport}
201+
import { AppModule } from "./app/app.module";
198202
199-
var ${GeneratedDynamicAppOptions} = { bootInExistingPage: true };
200-
${handleHmrOptionsCode}
201-
${acceptMainModuleCode}
203+
${handleHmrOptionsDeclaration}
204+
${handleHmrOptionsCode}
205+
${acceptMainModuleCode}
202206
203-
${nsDynamicPlatformCall}(${GeneratedDynamicAppOptions}).bootstrapModule(AppModule);
204-
`,
207+
${nsDynamicPlatformCall}(${getAppOptions("{ bootInExistingPage: true }")}).bootstrapModule(AppModule);
208+
`,
205209
transformedFileWithAot: `
206-
${handleHmrPlatformStaticImport}
207-
${handleAotPlatformStaticImport}
208-
${handleAotNgFactoryImport}
210+
${handleHmrPlatformStaticImport}
211+
${handleAotPlatformStaticImport}
212+
${handleAotNgFactoryImport}
209213
210-
var ${GeneratedDynamicAppOptions} = { bootInExistingPage: true };
211-
${handleHmrOptionsAotCode}
212-
${acceptMainModuleAotCode}
214+
${handleHmrOptionsDeclaration}
215+
${handleHmrOptionsAotCode}
216+
${acceptMainModuleAotCode}
213217
214-
${nsStaticPlatformCall}(${GeneratedDynamicAppOptions}).bootstrapModuleFactory(${nsFactoryImportName}.AppModuleNgFactory);
215-
`
218+
${nsStaticPlatformCall}(${getAppOptions("{ bootInExistingPage: true }")}).bootstrapModuleFactory(${nsFactoryImportName}.AppModuleNgFactory);
219+
`
216220
},
221+
{
222+
name: "should handle HMR when platformNativeScriptDynamic is called with appOptions variable",
223+
rawFile: `
224+
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
225+
import { AppModule } from "./app/app.module";
226+
const appOptions = { bootInExistingPage: true };
227+
platformNativeScriptDynamic(appOptions).bootstrapModule(AppModule);
228+
`,
229+
transformedFile: `
230+
${handleHmrPlatformDynamicImport}
231+
import { AppModule } from "./app/app.module";
232+
233+
var ${GeneratedDynamicAppOptions} = {};
234+
${handleHmrOptionsCode}
235+
${acceptMainModuleCode}
236+
237+
const appOptions = { bootInExistingPage: true };
238+
239+
${nsDynamicPlatformCall}(${getAppOptions("appOptions")}).bootstrapModule(AppModule);
240+
`,
241+
transformedFileWithAot: `
242+
${handleHmrPlatformStaticImport}
243+
${handleAotPlatformStaticImport}
244+
${handleAotNgFactoryImport}
245+
246+
var ${GeneratedDynamicAppOptions} = {};
247+
${handleHmrOptionsAotCode}
248+
${acceptMainModuleAotCode}
249+
250+
const appOptions = { bootInExistingPage: true };
251+
252+
${nsStaticPlatformCall}(${getAppOptions("appOptions")}).bootstrapModuleFactory(${nsFactoryImportName}.AppModuleNgFactory);
253+
`
254+
},
255+
217256
{
218257
name: "should handle HMR when platformNativeScriptDynamic is called with multiple arguments",
219258
rawFile: `
220-
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
221-
import { AppModule } from "./app/app.module";
222-
platformNativeScriptDynamic({ bootInExistingPage: true }, ["provider1", "provider2"]).bootstrapModule(AppModule);
223-
`,
259+
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
260+
import { AppModule } from "./app/app.module";
261+
platformNativeScriptDynamic({ bootInExistingPage: true }, ["provider1", "provider2"]).bootstrapModule(AppModule);
262+
`,
224263
transformedFile: `
225-
${handleHmrPlatformDynamicImport}
226-
import { AppModule } from "./app/app.module";
264+
${handleHmrPlatformDynamicImport}
265+
import { AppModule } from "./app/app.module";
227266
228-
var ${GeneratedDynamicAppOptions} = { bootInExistingPage: true };
229-
${handleHmrOptionsCode}
230-
${acceptMainModuleCode}
267+
${handleHmrOptionsDeclaration}
268+
${handleHmrOptionsCode}
269+
${acceptMainModuleCode}
231270
232-
${nsDynamicPlatformCall}(${GeneratedDynamicAppOptions}, ["provider1", "provider2"]).bootstrapModule(AppModule);
233-
`,
271+
${nsDynamicPlatformCall}(${getAppOptions("{ bootInExistingPage: true }")}, ["provider1", "provider2"]).bootstrapModule(AppModule);
272+
`,
234273
transformedFileWithAot: `
235-
${handleHmrPlatformStaticImport}
236-
${handleAotPlatformStaticImport}
237-
${handleAotNgFactoryImport}
274+
${handleHmrPlatformStaticImport}
275+
${handleAotPlatformStaticImport}
276+
${handleAotNgFactoryImport}
238277
239-
var ${GeneratedDynamicAppOptions} = { bootInExistingPage: true };
240-
${handleHmrOptionsAotCode}
241-
${acceptMainModuleAotCode}
278+
${handleHmrOptionsDeclaration}
279+
${handleHmrOptionsAotCode}
280+
${acceptMainModuleAotCode}
242281
243-
${nsStaticPlatformCall}(${GeneratedDynamicAppOptions}, ["provider1", "provider2"]).bootstrapModuleFactory(${nsFactoryImportName}.AppModuleNgFactory);
244-
`
282+
${nsStaticPlatformCall}(${getAppOptions("{ bootInExistingPage: true }")}, ["provider1", "provider2"]).bootstrapModuleFactory(${nsFactoryImportName}.AppModuleNgFactory);
283+
`
245284
},
246285
{
247286
name: "should accept HMR before the user when custom handling is in place",
248287
rawFile: `
249-
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
250-
import { AppModule } from "./app/app.module";
288+
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
289+
import { AppModule } from "./app/app.module";
251290
252-
if (module["hot"]) {
253-
module["hot"].accept(["./app/app.module"], function () {
254-
// customHandling
255-
});
256-
}
291+
if (module["hot"]) {
292+
module["hot"].accept(["./app/app.module"], function () {
293+
// customHandling
294+
});
295+
}
257296
258-
platformNativeScriptDynamic().bootstrapModule(AppModule);
259-
`,
297+
platformNativeScriptDynamic().bootstrapModule(AppModule);
298+
`,
260299
transformedFile: `
261-
${handleHmrPlatformDynamicImport}
262-
import { AppModule } from "./app/app.module";
300+
${handleHmrPlatformDynamicImport}
301+
import { AppModule } from "./app/app.module";
263302
264-
${handleHmrOptionsDeclaration}
265-
${handleHmrOptionsCode}
266-
${acceptMainModuleCode}
303+
${handleHmrOptionsDeclaration}
304+
${handleHmrOptionsCode}
305+
${acceptMainModuleCode}
267306
268-
if (module["hot"]) {
269-
module["hot"].accept(["./app/app.module"], function () {
270-
// customHandling
271-
});
272-
}
307+
if (module["hot"]) {
308+
module["hot"].accept(["./app/app.module"], function () {
309+
// customHandling
310+
});
311+
}
273312
274-
${nsDynamicPlatformCall}(${GeneratedDynamicAppOptions}).bootstrapModule(AppModule);
275-
`,
313+
${nsDynamicPlatformCall}(${defaultAppOptions}).bootstrapModule(AppModule);
314+
`,
276315
transformedFileWithAot: `
277-
${handleHmrPlatformStaticImport}
278-
${handleAotPlatformStaticImport}
279-
${handleAotNgFactoryImport}
280-
281-
${handleHmrOptionsDeclaration}
282-
${handleHmrOptionsAotCode}
283-
${acceptMainModuleAotCode}
284-
285-
if (module["hot"]) {
286-
module["hot"].accept(["./app/app.module"], function () {
287-
// customHandling
288-
});
289-
}
290-
291-
${nsStaticPlatformCall}(${GeneratedDynamicAppOptions}).bootstrapModuleFactory(${nsFactoryImportName}.AppModuleNgFactory);
292-
`
316+
${handleHmrPlatformStaticImport}
317+
${handleAotPlatformStaticImport}
318+
${handleAotNgFactoryImport}
319+
320+
${handleHmrOptionsDeclaration}
321+
${handleHmrOptionsAotCode}
322+
${acceptMainModuleAotCode}
323+
324+
if (module["hot"]) {
325+
module["hot"].accept(["./app/app.module"], function () {
326+
// customHandling
327+
});
328+
}
329+
330+
${nsStaticPlatformCall}(${defaultAppOptions}).bootstrapModuleFactory(${nsFactoryImportName}.AppModuleNgFactory);
331+
`
293332
}
294333
];
295334
testCases.forEach((testCase: any) => {

‎transformers/ns-support-hmr-ng.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,26 @@ function handleHmrSupportCore(mainFile: ts.SourceFile, importNodesInFile: ts.Nod
7373
currentAppOptionsInitializationNode = nativeScriptPlatformCallNode.arguments[0];
7474
}
7575

76-
const optionsDeclaration = ts.createVariableDeclaration(GeneratedDynamicAppOptions, undefined, currentAppOptionsInitializationNode);
76+
const optionsDeclaration = ts.createVariableDeclaration(GeneratedDynamicAppOptions, undefined, ts.createObjectLiteral());
7777
const optionsDeclarationList = ts.createVariableDeclarationList([optionsDeclaration]);
7878
const optionsStatement = ts.createVariableStatement(undefined, optionsDeclarationList);
7979

80-
const handleHmrOptionsNode = ts.createIdentifier(getHandleHmrOptionsCode(appModuleName, appModulePath));
80+
const setHmrOptionsNode = ts.createIdentifier(getHmrOptionsCode(appModuleName, appModulePath));
8181

8282
const acceptHmrNode = ts.createIdentifier(getAcceptMainModuleCode(appModulePath));
8383

84-
const newNsDynamicCallArgs = ts.createNodeArray([ts.createIdentifier(GeneratedDynamicAppOptions), ...nativeScriptPlatformCallNode.arguments.slice(1)]);
84+
const objectAssignNode = ts.createPropertyAccess(ts.createIdentifier("Object"), ts.createIdentifier("assign"));
85+
const extendAppOptionsNode = ts.createCall(objectAssignNode, undefined, [currentAppOptionsInitializationNode, ts.createIdentifier(GeneratedDynamicAppOptions)]);
86+
87+
const newNsDynamicCallArgs = ts.createNodeArray([extendAppOptionsNode, ...nativeScriptPlatformCallNode.arguments.slice(1)]);
8588
const nsPlatformPath = findNativeScriptPlatformPathInSource(mainFile);
8689
const nsPlatformText = getExpressionName(nativeScriptPlatformCallNode.expression);
8790
const newNsDynamicCallNode = ts.createCall(ts.createPropertyAccess(ts.createIdentifier(NsNgPlatformStarImport), ts.createIdentifier(nsPlatformText)), [], newNsDynamicCallArgs);
8891

8992
return [
9093
...insertStarImport(mainFile, ts.createIdentifier(NsNgPlatformStarImport), nsPlatformPath, firstImportNode, true),
9194
new AddNodeOperation(mainFile, lastImportNode, undefined, optionsStatement),
92-
new AddNodeOperation(mainFile, lastImportNode, undefined, handleHmrOptionsNode),
95+
new AddNodeOperation(mainFile, lastImportNode, undefined, setHmrOptionsNode),
9396
new AddNodeOperation(mainFile, lastImportNode, undefined, acceptHmrNode),
9497
new ReplaceNodeOperation(mainFile, nativeScriptPlatformCallNode, newNsDynamicCallNode)
9598
];
@@ -98,15 +101,15 @@ function handleHmrSupportCore(mainFile: ts.SourceFile, importNodesInFile: ts.Nod
98101
export const GeneratedDynamicAppOptions = "options_Generated";
99102
const NsNgPlatformStarImport = "nativescript_angular_platform_Generated";
100103

101-
export function getHandleHmrOptionsCode(appModuleName: string, appModulePath: string) {
104+
export function getHmrOptionsCode(appModuleName: string, appModulePath: string) {
102105
return `
103106
if (module["hot"]) {
104-
${GeneratedDynamicAppOptions} = Object.assign(${GeneratedDynamicAppOptions}, {
107+
${GeneratedDynamicAppOptions} = {
105108
hmrOptions: {
106109
moduleTypeFactory: function () { return require("${appModulePath}").${appModuleName}; },
107110
livesyncCallback: function (platformReboot) { setTimeout(platformReboot, 0); }
108111
}
109-
});
112+
};
110113
}
111114
`
112115
}

0 commit comments

Comments
 (0)
This repository has been archived.