Skip to content

Update typescript sample #3824

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

Merged
merged 5 commits into from
Apr 23, 2024
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
16 changes: 0 additions & 16 deletions dashboard-admin-react-rspack-material-ui/.vscode/launch.json

This file was deleted.

5 changes: 3 additions & 2 deletions different-react-versions-typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

This example demos the ability to load two separate versions of react (v16.6.3 and v18.2.0).

Read more about TypeScript and Federation: https://module-federation.io/guide/basic/type-prompt.html

> Check the javascript version of this example [here](../different-react-versions/README.md).

Module Federation allows us to create an adapter which attaches a hooks-friendly version to render a section of thr app using modern versions.
Expand Down Expand Up @@ -62,8 +64,7 @@ This is a generic component type, so you can pass the generic parameter to the c

```jsx
// remeber to add path alias to your tsconfig.base.json at the root of the workspace and the type definition file of the remote component
// this demo contains an example that reproduce that but you can check in the gist below
// https://gist.github.com/brunos3d/80235047c74b27573234c774ed474ef8
// Read more here: https://module-federation.io/guide/basic/type-prompt.html
import type { ButtonProps } from 'app2/Button';

<ReactAdapterConsumer<ButtonProps>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './compiled-types/Button';
export { default } from './compiled-types/Button';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './compiled-types/ModernReactComponent';
export { default } from './compiled-types/ModernReactComponent';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';
export interface ButtonProps {
color?: 'blue' | 'red';
}
declare const Button: React.FC<ButtonProps>;
export default Button;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import ReactAdapterProvider from './ReactAdapterProvider';
export interface ModernReactComponentProps {
children?: React.ReactNode;
input: string;
}
declare const ModernReactComponent: React.FC<ModernReactComponentProps>;
export declare const Adapted: React.ForwardRefExoticComponent<ModernReactComponentProps & React.RefAttributes<ReactAdapterProvider<ModernReactComponentProps>>>;
export default ModernReactComponent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
export declare type ReactAdapterProviderProps<P = {}> = P & {
component: React.FunctionComponent<P> | React.ComponentClass<P> | keyof React.ReactHTML | string;
children?: React.ReactNode;
};
export interface ReactAdapterProviderState {
Component: React.ReactNode;
}
declare class ReactAdapterProvider<P = {}> extends React.Component<ReactAdapterProviderProps<P>, ReactAdapterProviderState> {
private refHold;
constructor(props: ReactAdapterProviderProps<P>);
init: (hydrate?: boolean) => void;
componentDidUpdate(): void;
componentDidMount(): void;
render(): JSX.Element;
}
export default ReactAdapterProvider;
4 changes: 2 additions & 2 deletions different-react-versions-typescript/app1/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare module 'app2/Button' {
export * from 'app2/src/components/Button';
export * from 'app2/src/components/Button';
}

declare module 'app2/ModernComponent' {
export * from 'app2/src/components/ModernReactComponent';
export * from 'app2/src/components/ModernReactComponent';
}
4 changes: 2 additions & 2 deletions different-react-versions-typescript/app1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "different-react-versions-typescript_app1",
"version": "0.0.0",
"scripts": {
"legacy:start": "webpack-cli serve",
"legacy:start": "webpack serve",
"start": "rspack serve",
"legacy:build": "webpack --mode production",
"build": "rspack --mode production",
Expand Down Expand Up @@ -32,4 +32,4 @@
"@rspack/cli": "^0.6.2",
"@rspack/dev-server": "^0.6.2"
}
}
}
5 changes: 2 additions & 3 deletions different-react-versions-typescript/app1/rspack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { ModuleFederationPlugin } = require('@rspack/core').container;
const { ModuleFederationPlugin } = require('@module-federation/enhanced/rspack');
const path = require('path');
const deps = require('./package.json').dependencies;

Expand Down Expand Up @@ -46,9 +46,8 @@ module.exports = {
plugins: [
new ModuleFederationPlugin({
name: 'app1',
library: { type: 'var', name: 'app1' },
remotes: {
app2: 'app2',
app2: 'app2@http://localhost:3002/remoteEntry.js',
},
shared: {
...deps,
Expand Down
6 changes: 5 additions & 1 deletion different-react-versions-typescript/app1/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {},
"compilerOptions": {
"paths": {
"*": ["./@mf-types/*"]
}
},
"include": ["src/**/*", "**/*.d.ts"],
"exclude": ["node_modules"]
}
4 changes: 2 additions & 2 deletions different-react-versions-typescript/app1/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ module.exports = {
plugins: [
new ModuleFederationPlugin({
name: 'app1',
library: { type: 'var', name: 'app1' },
dts:true,
remotes: {
app2: 'app2',
app2: 'app2@http://localhost:3002/remoteEntry.js',
},
shared: {
...deps,
Expand Down
3 changes: 1 addition & 2 deletions different-react-versions-typescript/app2/rspack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { ModuleFederationPlugin } = require('@rspack/core').container;
const { ModuleFederationPlugin } = require('@module-federation/enhanced/rspack');
const path = require('path');

module.exports = {
Expand Down Expand Up @@ -44,7 +44,6 @@ module.exports = {
plugins: [
new ModuleFederationPlugin({
name: 'app2',
library: { type: 'var', name: 'app2' },
filename: 'remoteEntry.js',
exposes: {
'./Button': './src/components/Button',
Expand Down
6 changes: 5 additions & 1 deletion different-react-versions-typescript/app2/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {},
"compilerOptions": {
"paths": {
"*": ["./@mf-types/*"]
}
},
"include": ["src/**/*", "**/*.d.ts"],
"exclude": ["node_modules"]
}
1 change: 0 additions & 1 deletion different-react-versions-typescript/app2/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ module.exports = {
plugins: [
new ModuleFederationPlugin({
name: 'app2',
library: { type: 'var', name: 'app2' },
filename: 'remoteEntry.js',
exposes: {
'./Button': './src/components/Button',
Expand Down
3 changes: 1 addition & 2 deletions different-react-versions-typescript/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"lib": ["dom", "dom.iterable", "esnext"],
"baseUrl": ".",
"paths": {
"@app1/*": ["./app1/src/*"],
"@app2/*": ["./app2/src/*"]
"*": ["./@mf-types/*"]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down
3 changes: 0 additions & 3 deletions dynamic-remotes-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@ Similar to browser side dynamic remotes.
This allows you to dynamically load remote containers on the server.

`pnpm run start` will initiate a build and http server, then node will execute a simple test.

## runtime-container
This example shows the same thing but how to do it outside of the webpack runtime (outside a webpack bundle). See the script `import-remote.js`, which is not compiled by webpack. Run the command `yarn import-remote` or simply, `node import-remote.js`
19 changes: 19 additions & 0 deletions dynamic-remotes-node/app1/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const {ModuleFederationPlugin} = require('@module-federation/enhanced/rspack');
module.exports = {
entry: './index.js',
mode: 'development',
output: {
library: {type: 'commonjs-module',}
},
target: 'async-node',
plugins: [
new ModuleFederationPlugin({
remoteType: 'script',
name: 'app1',
runtimePlugins: [require.resolve('@module-federation/node/runtimePlugin')],
exposes: {
'./noop': './noop.js',
}
}),
]
}
1 change: 1 addition & 0 deletions dynamic-remotes-node/app1/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
remoteType: 'script',
isServer: true,
name: 'app1',
useRuntimePlugin: true,
exposes: {
'./noop': './noop.js',
}
Expand Down
22 changes: 22 additions & 0 deletions dynamic-remotes-node/app2/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const {ModuleFederationPlugin} = require('@module-federation/enhanced/rspack');
module.exports = {
entry: './index.js',
mode: 'development',
target: 'async-node',
output: {
library: {type: 'commonjs-module',}
},
plugins: [
new ModuleFederationPlugin({
remoteType: 'script',
isServer: true,
name: 'app2',
library: {type: 'commonjs-module',},
runtimePlugins: [require.resolve('@module-federation/node/runtimePlugin')],
filename: 'remoteEntry.js',
exposes: {
'./sample': './expose-sample.js',
}
}),
]
}
1 change: 1 addition & 0 deletions dynamic-remotes-node/app2/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
remoteType: 'script',
isServer: true,
name: 'app2',
useRuntimePlugin: true,
library: {type: 'commonjs-module',},
filename: 'remoteEntry.js',
exposes: {
Expand Down
22 changes: 0 additions & 22 deletions dynamic-remotes-node/import-remote.js

This file was deleted.

15 changes: 10 additions & 5 deletions dynamic-remotes-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@
"license": "MIT",
"scripts": {
"serve": "concurrently 'serve -s app1/dist -l 3001' 'serve -s app2/dist -l 3002'",
"build": "rimraf dist/server && concurrently 'cd app1; webpack --config ./webpack.config.js' 'cd runtime-container; webpack --config ./webpack.config.js' 'cd app2; webpack --config ./webpack.config.js'",
"start": "pnpm build && concurrently 'pnpm serve' 'sleep 5 && node app1/dist/main.js'",
"import-remote": "node import-remote.js"
"build:legacy": "rimraf dist/server && concurrently 'cd app1; webpack --config ./webpack.config.js' 'cd runtime-container; webpack --config ./webpack.config.js' 'cd app2; webpack --config ./webpack.config.js'",
"build": "rimraf dist/server && concurrently 'cd app1; rspack --config ./rspack.config.js' 'cd runtime-container; rspack --config ./rspack.config.js' 'cd app2; rspack --config ./rspack.config.js'",
"start:legacy": "pnpm build:legacy && concurrently 'pnpm serve' 'sleep 5 && node app1/dist/main.js'",
"start": "pnpm build && concurrently 'pnpm serve' 'sleep 5 && node app1/dist/main.js'"
},
"dependencies": {
"@module-federation/node": "^2.2.0",
"@module-federation/enhanced": "^0.1.9",
"@module-federation/runtime": "^0.1.9",
"concurrently": "^8.0.1",
"webpack": "^5.91.0",
"rimraf": "^5.0.5",
"webpack-cli": "^5.1.4"
"webpack-cli": "^5.1.4",
"@rspack/cli": "^0.6.2",
"@rspack/core": "^0.6.2",
"@rspack/dev-server": "^0.6.2"
}
}
}
26 changes: 26 additions & 0 deletions dynamic-remotes-node/runtime-container/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const {ModuleFederationPlugin} = require('@module-federation/enhanced/rspack');

module.exports = {
entry: './index.js',
mode: 'development',
target: 'async-node',
output: {
library: {type: 'commonjs-module',}
},
optimization: {
runtimeChunk: 'single',
},
plugins: [
new ModuleFederationPlugin({
remoteType: 'script',
isServer: true,
name: 'app2',
library: {type: 'commonjs-module',},
runtimePlugins: [require.resolve('@module-federation/node/runtimePlugin')],
filename: 'remoteEntry.js',
exposes: {
'./sample': './expose-sample.js',
}
}),
]
}
1 change: 1 addition & 0 deletions dynamic-remotes-node/runtime-container/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
remoteType: 'script',
isServer: true,
name: 'app2',
useRuntimePlugin: true,
library: {type: 'commonjs-module',},
filename: 'remoteEntry.js',
exposes: {
Expand Down
Loading