Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(angular): remove import of missing environment file #14640

Merged
merged 2 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,12 @@ module.exports = withModuleFederation(config);"
`;

exports[`MF Remote App Generator should generate the a remote setup for standalone components 1`] = `
"import {environment} from \\"./environments/environment\\";
import {enableProdMode, importProvidersFrom} from \\"@angular/core\\";
"import {importProvidersFrom} from \\"@angular/core\\";
import {bootstrapApplication} from \\"@angular/platform-browser\\";
import {RouterModule} from \\"@angular/router\\";
import {RemoteEntryComponent} from \\"./app/remote-entry/entry.component\\";
import {appRoutes} from \\"./app/app.routes\\";

if (environment.production) {
enableProdMode();
}

bootstrapApplication(RemoteEntryComponent, {
providers: [
importProvidersFrom(
Expand Down
23 changes: 17 additions & 6 deletions packages/angular/src/generators/setup-mf/lib/fix-bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import type { Tree } from '@nrwl/devkit';
import { joinPathFragments } from '@nrwl/devkit';
import type { Schema } from '../schema';
import { getInstalledAngularMajorVersion } from '../../utils/version-utils';

export function fixBootstrap(tree: Tree, appRoot: string, options: Schema) {
const mainFilePath = joinPathFragments(appRoot, 'src/main.ts');
const bootstrapCode = tree.read(mainFilePath, 'utf-8');
const installedAngularMajor = getInstalledAngularMajorVersion(tree);
if (options.standalone) {
tree.write(`${appRoot}/src/bootstrap.ts`, standaloneBootstrapCode);
tree.write(
`${appRoot}/src/bootstrap.ts`,
standaloneBootstrapCode(installedAngularMajor === 14)
);
} else {
tree.write(joinPathFragments(appRoot, 'src/bootstrap.ts'), bootstrapCode);
}
Expand All @@ -28,17 +33,23 @@ export function fixBootstrap(tree: Tree, appRoot: string, options: Schema) {
);
}

const standaloneBootstrapCode = `import {environment} from "./environments/environment";
import {enableProdMode, importProvidersFrom} from "@angular/core";
const standaloneBootstrapCode = (
includeEnvironments: boolean = false
) => `import {importProvidersFrom} from "@angular/core";
import {bootstrapApplication} from "@angular/platform-browser";
import {RouterModule} from "@angular/router";
import {RemoteEntryComponent} from "./app/remote-entry/entry.component";
import {appRoutes} from "./app/app.routes";

if (environment.production) {
${
includeEnvironments
? `import {enableProdMode} from '@angular/core';
import {environment} from './environments/environment';
if(environment.production) {
enableProdMode();
}

`
: ``
}
bootstrapApplication(RemoteEntryComponent, {
providers: [
importProvidersFrom(
Expand Down
48 changes: 48 additions & 0 deletions packages/angular/src/generators/setup-mf/setup-mf.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,54 @@ describe('Init MF', () => {
});
});

it('should generate bootstrap with environments for ng14', async () => {
// ARRANGE
updateJson(tree, 'package.json', (json) => ({
...json,
dependencies: {
...json.dependencies,
'@angular/core': '14.1.0',
},
}));

await applicationGenerator(tree, {
name: 'ng14',
routing: true,
standalone: true,
});

// ACT
await setupMf(tree, {
appName: 'ng14',
mfType: 'host',
routing: true,
standalone: true,
});

// ASSERT
expect(tree.read('apps/ng14/src/bootstrap.ts', 'utf-8'))
.toMatchInlineSnapshot(`
"import {importProvidersFrom} from \\"@angular/core\\";
import {bootstrapApplication} from \\"@angular/platform-browser\\";
import {RouterModule} from \\"@angular/router\\";
import {RemoteEntryComponent} from \\"./app/remote-entry/entry.component\\";
import {appRoutes} from \\"./app/app.routes\\";
import {enableProdMode} from '@angular/core';
import {environment} from './environments/environment';
if(environment.production) {
enableProdMode();
}

bootstrapApplication(RemoteEntryComponent, {
providers: [
importProvidersFrom(
RouterModule.forRoot(appRoutes, {initialNavigation: 'enabledBlocking'})
)
]
});"
`);
});

it('should add a remote to dynamic host correctly', async () => {
// ARRANGE
await setupMf(tree, {
Expand Down
6 changes: 0 additions & 6 deletions packages/angular/src/utils/nx-devkit/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,9 @@ export function createApp(
tree.write(
`/apps/${appName}/src/main.ts`,
`
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
enableProdMode();
}

platformBrowserDynamic()
.bootstrapModule(AppModule)
Expand Down