-
Notifications
You must be signed in to change notification settings - Fork 123
feat(react-externals): automate bundle serving and fix typings #2331
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@lynx-js/react-umd': minor | ||
| --- | ||
|
|
||
| Add standalone UMD build of the ReactLynx runtime. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # @lynx-js/react-umd | ||
|
|
||
| This package provides a standalone, Universal Module Definition (UMD) build of the `ReactLynx` runtime. It is designed to be used as an **external bundle**, allowing multiple Lynx components or applications to load a single React runtime instance over the network, which improves load time, caches efficiency, and reduces memory usage. | ||
|
|
||
| ## Purpose | ||
|
|
||
| When building Lynx applications, the ReactLynx runtime is typically bundled directly into the application instance. However, for advanced use cases like micro-frontends or dynamically loading remote components, it's highly beneficial to expose ReactLynx as an external dependency. `react-umd` pre-bundles ReactLynx so that it can be loaded on-demand and shared across different parts of your Lynx app. | ||
|
|
||
| ## Building | ||
|
|
||
| To build the development and production bundles locally: | ||
|
|
||
| ```bash | ||
| pnpm build | ||
| ``` | ||
|
|
||
| The script will automatically execute: | ||
|
|
||
| - `pnpm build:development` (sets `NODE_ENV=development`) | ||
| - `pnpm build:production` (sets `NODE_ENV=production`) | ||
|
|
||
| This generates the following artifacts in the `dist/` directory: | ||
|
|
||
| - `react-dev.lynx.bundle` | ||
| - `react-prod.lynx.bundle` | ||
|
|
||
| ## Usage as an External Bundle | ||
|
|
||
| For a full working example of how to serve and consume this external bundle, see the [`react-externals` example](../../examples/react-externals/README.md) in this repository. | ||
|
|
||
| Typically, you will use `@lynx-js/external-bundle-rsbuild-plugin` to map `@lynx-js/react` and its internal modules directly to the URL where this UMD bundle is served in your Lynx config file (eg. `lynx.config.ts`). | ||
|
|
||
| ### 1. Consuming in a Custom Component Bundle (`rslib.config.ts`) | ||
|
|
||
| If you are building your own external UI component library that relies on React, you should map the React imports to the global variable exposed by this UMD bundle. | ||
|
|
||
| ```ts | ||
| // rslib-comp-lib.config.ts | ||
| export default defineExternalBundleRslibConfig({ | ||
| output: { | ||
| externals: { | ||
| '@lynx-js/react': ['ReactLynx', 'React'], | ||
| '@lynx-js/react/internal': ['ReactLynx', 'ReactInternal'], | ||
| '@lynx-js/react/jsx-dev-runtime': ['ReactLynx', 'ReactJSXDevRuntime'], | ||
| '@lynx-js/react/jsx-runtime': ['ReactLynx', 'ReactJSXRuntime'], | ||
| '@lynx-js/react/lepus/jsx-dev-runtime': [ | ||
| 'ReactLynx', | ||
| 'ReactJSXLepusDevRuntime', | ||
| ], | ||
| '@lynx-js/react/lepus/jsx-runtime': ['ReactLynx', 'ReactJSXLepusRuntime'], | ||
| '@lynx-js/react/experimental/lazy/import': [ | ||
| 'ReactLynx', | ||
| 'ReactLazyImport', | ||
| ], | ||
| '@lynx-js/react/legacy-react-runtime': [ | ||
| 'ReactLynx', | ||
| 'ReactLegacyRuntime', | ||
| ], | ||
| '@lynx-js/react/runtime-components': ['ReactLynx', 'ReactComponents'], | ||
| '@lynx-js/react/worklet-runtime/bindings': [ | ||
| 'ReactLynx', | ||
| 'ReactWorkletRuntime', | ||
| ], | ||
| '@lynx-js/react/debug': ['ReactLynx', 'ReactDebug'], | ||
| 'preact': ['ReactLynx', 'Preact'], | ||
| }, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ### 2. Resolving the Bundle in a Lynx App (`lynx.config.ts`) | ||
|
|
||
| In the host application, configure the Rsbuild plugin to load the React UMD bundle into the correct engine layers (Background Thread and Main Thread) when the app starts. | ||
|
|
||
| ```ts | ||
| // lynx.config.ts | ||
| import { pluginExternalBundle } from '@lynx-js/external-bundle-rsbuild-plugin'; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [ | ||
| pluginExternalBundle({ | ||
| externals: { | ||
| '@lynx-js/react': { | ||
| libraryName: ['ReactLynx', 'React'], | ||
| url: `http://<your-server>/react.lynx.bundle`, | ||
| background: { sectionPath: 'ReactLynx' }, | ||
| mainThread: { sectionPath: 'ReactLynx__main-thread' }, | ||
| async: false, | ||
| }, | ||
| // ... include similar configurations for other @lynx-js/react/* subpaths | ||
| }, | ||
| }), | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| > Note: Ensure you map both the `background` and `mainThread` configurations properly so that React successfully attaches across the threaded architecture. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| { | ||
| "name": "@lynx-js/react-umd", | ||
| "version": "0.0.1-alpha-1", | ||
|
upupming marked this conversation as resolved.
|
||
| "description": "UMD build for ReactLynx", | ||
| "keywords": [ | ||
| "ReactLynx", | ||
| "rspeedy", | ||
| "externals", | ||
| "external bundle", | ||
| "umd" | ||
| ], | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/lynx-family/lynx-stack.git", | ||
| "directory": "packages/react-umd" | ||
| }, | ||
| "license": "Apache-2.0", | ||
| "author": { | ||
| "name": "Yiming Li", | ||
| "email": "yimingli.cs@gmail.com" | ||
| }, | ||
| "type": "module", | ||
| "exports": { | ||
| "./dev": "./dist/react-dev.lynx.bundle", | ||
| "./prod": "./dist/react-prod.lynx.bundle" | ||
| }, | ||
| "files": [ | ||
| "dist", | ||
| "CHANGELOG.md", | ||
| "README.md" | ||
| ], | ||
| "scripts": { | ||
| "build": "rimraf dist && pnpm build:development && pnpm build:production", | ||
| "build:development": "cross-env NODE_ENV=development rslib build", | ||
| "build:production": "rslib build" | ||
| }, | ||
| "devDependencies": { | ||
| "@lynx-js/lynx-bundle-rslib-config": "workspace:*", | ||
| "@lynx-js/react": "workspace:*", | ||
| "@lynx-js/react-rsbuild-plugin": "workspace:*", | ||
| "cross-env": "^7.0.3", | ||
| "rimraf": "^6.1.3" | ||
| } | ||
|
colinaaa marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...ct-externals/external-bundle/ReactLynx.ts → packages/react-umd/src/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "extends": "../../tsconfig.json", | ||
| "compilerOptions": { | ||
| "composite": true, | ||
| }, | ||
| "include": [ | ||
| "src", | ||
| ], | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.