Skip to content

Commit

Permalink
Add Micro frontend host and remote app in Nx Workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
serifcolakel committed Mar 28, 2024
1 parent 681dfc1 commit cd91400
Show file tree
Hide file tree
Showing 65 changed files with 10,083 additions and 1,537 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Expand Up @@ -14,5 +14,6 @@
"typescript",
"typescriptreact"
],
"eslint.format.enable": true
"eslint.format.enable": true,
"editor.formatOnSave": true
}
10 changes: 10 additions & 0 deletions apps/container-e2e/.eslintrc.json
@@ -0,0 +1,10 @@
{
"extends": ["plugin:cypress/recommended", "../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
}
]
}
6 changes: 6 additions & 0 deletions apps/container-e2e/cypress.config.ts
@@ -0,0 +1,6 @@
import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset';
import { defineConfig } from 'cypress';

export default defineConfig({
e2e: nxE2EPreset(__dirname),
});
34 changes: 34 additions & 0 deletions apps/container-e2e/project.json
@@ -0,0 +1,34 @@
{
"name": "container-e2e",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/container-e2e/src",
"projectType": "application",
"targets": {
"e2e": {
"executor": "@nx/cypress:cypress",
"options": {
"cypressConfig": "apps/container-e2e/cypress.config.ts",
"devServerTarget": "container:serve:development",
"testingType": "e2e",
"baseUrl": "http://localhost:3000"
},
"configurations": {
"production": {
"devServerTarget": "container:serve:production"
},
"ci": {
"devServerTarget": "container:serve-static"
}
}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["apps/container-e2e/**/*.{js,ts}"]
}
}
},
"tags": [],
"implicitDependencies": ["container"]
}
13 changes: 13 additions & 0 deletions apps/container-e2e/src/e2e/app.cy.ts
@@ -0,0 +1,13 @@
import { getGreeting } from '../support/app.po';

describe('container', () => {
beforeEach(() => cy.visit('/'));

it('should display welcome message', () => {
// Custom command example, see `../support/commands.ts` file
cy.login('my-email@something.com', 'myPassword');

// Function helper example, see `../support/app.po.ts` file
getGreeting().contains('Welcome container');
});
});
4 changes: 4 additions & 0 deletions apps/container-e2e/src/fixtures/example.json
@@ -0,0 +1,4 @@
{
"name": "Using fixtures to represent data",
"email": "hello@cypress.io"
}
1 change: 1 addition & 0 deletions apps/container-e2e/src/support/app.po.ts
@@ -0,0 +1 @@
export const getGreeting = () => cy.get('h1');
33 changes: 33 additions & 0 deletions apps/container-e2e/src/support/commands.ts
@@ -0,0 +1,33 @@
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************

// eslint-disable-next-line @typescript-eslint/no-namespace
declare namespace Cypress {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Chainable<Subject> {
login(email: string, password: string): void;
}
}
//
// -- This is a parent command --
Cypress.Commands.add('login', (email, password) => {
window.console.log('Custom command example: Login', email, password);
});
//
// -- This is a child command --
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
17 changes: 17 additions & 0 deletions apps/container-e2e/src/support/e2e.ts
@@ -0,0 +1,17 @@
// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands';
10 changes: 10 additions & 0 deletions apps/container-e2e/tsconfig.json
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"sourceMap": false,
"outDir": "../../dist/out-tsc",
"allowJs": true,
"types": ["cypress", "node"]
},
"include": ["src/**/*.ts", "src/**/*.js", "cypress.config.ts"]
}
11 changes: 11 additions & 0 deletions apps/container/.babelrc
@@ -0,0 +1,11 @@
{
"presets": [
[
"@nx/react/babel",
{
"runtime": "automatic"
}
]
],
"plugins": []
}
18 changes: 18 additions & 0 deletions apps/container/.eslintrc.json
@@ -0,0 +1,18 @@
{
"extends": ["plugin:@nx/react", "../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
11 changes: 11 additions & 0 deletions apps/container/jest.config.ts
@@ -0,0 +1,11 @@
/* eslint-disable */
export default {
displayName: 'container',
preset: '../../jest.preset.js',
transform: {
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
'^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nx/react/babel'] }],
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/apps/container',
};
8 changes: 8 additions & 0 deletions apps/container/module-federation.config.ts
@@ -0,0 +1,8 @@
import { ModuleFederationConfig } from '@nx/webpack';

const config: ModuleFederationConfig = {
name: 'container',
remotes: ['info'],
};

export default config;
109 changes: 109 additions & 0 deletions apps/container/project.json
@@ -0,0 +1,109 @@
{
"name": "container",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/container/src",
"projectType": "application",
"targets": {
"build": {
"executor": "@nx/webpack:webpack",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"compiler": "babel",
"outputPath": "dist/apps/container",
"index": "apps/container/src/index.html",
"baseHref": "/",
"main": "apps/container/src/main.ts",
"tsConfig": "apps/container/tsconfig.app.json",
"assets": [
"apps/container/src/favicon.ico",
"apps/container/src/assets"
],
"styles": ["apps/container/src/styles.css"],
"scripts": [],
"isolatedConfig": true,
"webpackConfig": "apps/container/webpack.config.ts"
},
"configurations": {
"development": {
"extractLicenses": false,
"optimization": false,
"sourceMap": true,
"vendorChunk": true
},
"production": {
"fileReplacements": [
{
"replace": "apps/container/src/environments/environment.ts",
"with": "apps/container/src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"webpackConfig": "apps/container/webpack.config.prod.ts"
}
}
},
"serve": {
"executor": "@nx/react:module-federation-dev-server",
"defaultConfiguration": "development",
"options": {
"buildTarget": "container:build",
"hmr": true,
"port": 3000
},
"configurations": {
"development": {
"buildTarget": "container:build:development"
},
"production": {
"buildTarget": "container:build:production",
"hmr": false
}
}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["apps/container/**/*.{ts,tsx,js,jsx}"]
}
},
"serve-static": {
"executor": "@nx/web:file-server",
"defaultConfiguration": "production",
"options": {
"buildTarget": "container:build",
"watch": false,
"port": 3000
},
"configurations": {
"development": {
"buildTarget": "container:build:development"
},
"production": {
"buildTarget": "container:build:production"
}
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "apps/container/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
},
"tags": []
}
1 change: 1 addition & 0 deletions apps/container/src/app/app.module.css
@@ -0,0 +1 @@
/* Your styles goes here. */
27 changes: 27 additions & 0 deletions apps/container/src/app/app.spec.tsx
@@ -0,0 +1,27 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */
import { render } from '@testing-library/react';
import { BrowserRouter } from 'react-router-dom';

import App from './app';

describe('App', () => {
it('should render successfully', () => {
const { baseElement } = render(
<BrowserRouter>
<App />
</BrowserRouter>
);

expect(baseElement).toBeTruthy();
});

it('should have a greeting as the title', () => {
const { getByText } = render(
<BrowserRouter>
<App />
</BrowserRouter>
);

expect(getByText(/Welcome container/gi)).toBeTruthy();
});
});
25 changes: 25 additions & 0 deletions apps/container/src/app/app.tsx
@@ -0,0 +1,25 @@
import * as React from 'react';
import { Link, Route, Routes } from 'react-router-dom';

const Info = React.lazy(() => import('info/InfoContainer'));

export function App() {
return (
<React.Suspense fallback={null}>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/info">Info</Link>
</li>
</ul>
<Routes>
<Route element={<div>Welcome container</div>} path="/" />
<Route element={<Info />} path="/info" />
</Routes>
</React.Suspense>
);
}

export default App;
Empty file.
21 changes: 21 additions & 0 deletions apps/container/src/bootstrap.tsx
@@ -0,0 +1,21 @@
import { StrictMode } from 'react';
import * as ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';

import App from './app/app';

const element = document.getElementById('root');

if (!element) {
throw new Error('Root element not found');
}

const root = ReactDOM.createRoot(element);

root.render(
<StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</StrictMode>
);
3 changes: 3 additions & 0 deletions apps/container/src/environments/environment.prod.ts
@@ -0,0 +1,3 @@
export const environment = {
production: true,
};
6 changes: 6 additions & 0 deletions apps/container/src/environments/environment.ts
@@ -0,0 +1,6 @@
// This file can be replaced during build by using the `fileReplacements` array.
// When building for production, this file is replaced with `environment.prod.ts`.

export const environment = {
production: false,
};
Binary file added apps/container/src/favicon.ico
Binary file not shown.

0 comments on commit cd91400

Please sign in to comment.