Skip to content

feat: build thin externalization client #852

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 3 commits into from
Jun 3, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# distribution
*dist
*umd

# misc
.DS_Store
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
It was necessary to support WDS below `4.8.0` (published in April 2022),
but is no-longer needed as a direct integration with WDS is now possible.

### Features

- Added helper script to better support use cases where React and/or React-DOM are externalized (#852)

## 0.5.15 (3 Jun 2024)

### Fixes
Expand Down
16 changes: 14 additions & 2 deletions docs/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,21 @@ but do note that React DevTools does not inject hooks over a frame boundary (`if
**Externalise React Refresh**

If all solutions above are not applicable, you can also externalise `react-refresh/runtime` together with React.
We provide an entrypoint to easily achieve this - `@pmmmwh/react-refresh-webpack-plugin/umd/client.min.js`.

Using this, however, would require you to ensure the injected entry from this plugin is executed before React.
You can check out [this sandbox](https://codesandbox.io/s/react-refresh-externals-14fpn) for an example on how this could be done.
If you would like to use the provided script, ensure that it is loaded before React and/or React-DOM.
You can load this script via any CDN for `npm`, such as `jsDelivr` and `unpkg`:

```html
<!-- if you prefer jsDelivr -->
<script src=" https://cdn.jsdelivr.net/npm/@pmmmwh/react-refresh-webpack-plugin@^0.6.0/umd/client.min.js "></script>

<!-- if you prefer unpkg -->
<script src="https://unpkg.com/@pmmmwh/react-refresh-webpack-plugin@^0.6.0/umd/client.min.js"></script>
```

If you don't want to use the provided script,
you can check out [this sandbox](https://codesandbox.io/s/react-refresh-externals-14fpn) for an example on how this could be done manually.

## Running multiple instances of React Refresh simultaneously

Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"options",
"overlay",
"sockets",
"types"
"types",
"umd"
],
"packageManager": "[email protected]+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610",
"scripts": {
Expand All @@ -54,8 +55,9 @@
"types:clean": "rimraf types",
"types:compile": "tsc -p tsconfig.json",
"types:prune-private": "del \"types/*/*\" \"!types/{lib,loader,options}/{index,types}.d.ts\"",
"generate-types": "run-s types:clean types:compile types:prune-private \"format --loglevel silent\"",
"prepublishOnly": "yarn generate-types"
"generate:client-external": "webpack",
"generate:types": "run-s types:clean types:compile types:prune-private \"format --log-level silent\"",
"prepublishOnly": "run-p generate:*"
},
"dependencies": {
"ansi-html": "^0.0.9",
Expand Down Expand Up @@ -97,6 +99,7 @@
"puppeteer": "^22.10.0",
"react-refresh": "^0.14.2",
"sourcemap-validator": "^2.1.0",
"terser-webpack-plugin": "^5.3.10",
"type-fest": "^4.18.3",
"typescript": "~5.4.5",
"webpack": "^5.91.0",
Expand Down
25 changes: 25 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const path = require('node:path');
const TerserPlugin = require('terser-webpack-plugin');

module.exports = {
mode: 'production',
entry: {
client: './client/ReactRefreshEntry.js',
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
terserOptions: {
format: { comments: false },
},
}),
],
nodeEnv: 'development',
},
output: {
filename: '[name].min.js',
path: path.resolve(__dirname, 'umd'),
},
};