Skip to content

Commit 7ecf777

Browse files
authored
fix: migrate root Ionic module without config (#40)
* fix: migrate root Ionic module without config * chore: improve error messages
1 parent cca8cc1 commit 7ecf777

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

packages/cli/src/angular/migrations/standalone/0003-migrate-bootstrap-application.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,65 @@ describe("migrateBootstrapApplication", () => {
116116
`),
117117
);
118118
});
119+
120+
it("should migrate IonicModule.forRoot without explicit config", async () => {
121+
const project = new Project({ useInMemoryFileSystem: true });
122+
123+
const main = dedent(`
124+
import { enableProdMode, importProvidersFrom } from '@angular/core';
125+
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
126+
127+
128+
import { environment } from './environments/environment';
129+
import { AppComponent } from './app/app.component';
130+
import { AppRoutingModule } from './app/app-routing.module';
131+
import { BrowserModule, bootstrapApplication } from '@angular/platform-browser';
132+
import { IonicRouteStrategy, IonicModule } from '@ionic/angular';
133+
import { RouteReuseStrategy } from '@angular/router';
134+
135+
if (environment.production) {
136+
enableProdMode();
137+
}
138+
139+
bootstrapApplication(AppComponent, {
140+
providers: [
141+
importProvidersFrom(BrowserModule, IonicModule.forRoot(), AppRoutingModule),
142+
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
143+
]
144+
})
145+
.catch(err => console.log(err));
146+
`);
147+
148+
const mainSourceFile = project.createSourceFile("src/main.ts", main);
149+
150+
await migrateBootstrapApplication(project, { dryRun: false });
151+
152+
expect(dedent(mainSourceFile.getText())).toBe(
153+
dedent(`
154+
import { enableProdMode, importProvidersFrom } from '@angular/core';
155+
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
156+
157+
158+
import { environment } from './environments/environment';
159+
import { AppComponent } from './app/app.component';
160+
import { AppRoutingModule } from './app/app-routing.module';
161+
import { BrowserModule, bootstrapApplication } from '@angular/platform-browser';
162+
import { IonicRouteStrategy, provideIonicAngular } from '@ionic/angular/standalone';
163+
import { RouteReuseStrategy } from '@angular/router';
164+
165+
if (environment.production) {
166+
enableProdMode();
167+
}
168+
169+
bootstrapApplication(AppComponent, {
170+
providers: [
171+
importProvidersFrom(BrowserModule, AppRoutingModule),
172+
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
173+
provideIonicAngular({})
174+
]
175+
})
176+
.catch(err => console.log(err));
177+
`),
178+
);
179+
});
119180
});

packages/cli/src/angular/migrations/standalone/0003-migrate-bootstrap-application.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ export const migrateBootstrapApplication = async (
129129
const ionicConfigObjectLiteralExpression =
130130
importProvidersFromFunctionCallIonicModuleForRootCallExpressionArguments[0];
131131

132-
providersArray.addElement(
133-
`provideIonicAngular(${ionicConfigObjectLiteralExpression.getText()})`,
134-
);
132+
const ionicConfig = ionicConfigObjectLiteralExpression?.getText() ?? "{}";
133+
134+
providersArray.addElement(`provideIonicAngular(${ionicConfig})`);
135135

136136
// Remove the IonicModule.forRoot from the importProvidersFrom function call.
137137
importProvidersFromFunctionCall.removeArgument(

packages/cli/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ async function main() {
9696
dir: cli.dir,
9797
spinner: s,
9898
});
99-
} catch (e: any) {
99+
} catch (e) {
100100
s.stop("An error occurred during the migration.", 1);
101-
log.error(e.message);
101+
const error = e as Error;
102+
log.error(error.stack ?? error.message);
102103
}
103104

104105
outro(

0 commit comments

Comments
 (0)