Skip to content

Commit 7dd110f

Browse files
committed
feat: build thin externalization client (#852)
1 parent 66838ab commit 7dd110f

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
# distribution
77
*dist
8+
*umd
89

910
# misc
1011
.DS_Store

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
It was necessary to support WDS below `4.8.0` (published in April 2022),
1111
but is no-longer needed as a direct integration with WDS is now possible.
1212

13+
### Features
14+
15+
- Added helper script to better support use cases where React and/or React-DOM are externalized (#852)
16+
1317
## 0.5.16 (31 Mar 2025)
1418

1519
### Fixes

docs/TROUBLESHOOTING.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,21 @@ but do note that React DevTools does not inject hooks over a frame boundary (`if
268268
**Externalise React Refresh**
269269

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

272-
Using this, however, would require you to ensure the injected entry from this plugin is executed before React.
273-
You can check out [this sandbox](https://codesandbox.io/s/react-refresh-externals-14fpn) for an example on how this could be done.
273+
If you would like to use the provided script, ensure that it is loaded before React and/or React-DOM.
274+
You can load this script via any CDN for `npm`, such as `jsDelivr` and `unpkg`:
275+
276+
```html
277+
<!-- if you prefer jsDelivr -->
278+
<script src=" https://cdn.jsdelivr.net/npm/@pmmmwh/react-refresh-webpack-plugin@^0.6.0/umd/client.min.js "></script>
279+
280+
<!-- if you prefer unpkg -->
281+
<script src="https://unpkg.com/@pmmmwh/react-refresh-webpack-plugin@^0.6.0/umd/client.min.js"></script>
282+
```
283+
284+
If you don't want to use the provided script,
285+
you can check out [this sandbox](https://codesandbox.io/s/react-refresh-externals-14fpn) for an example on how this could be done manually.
274286

275287
## Running multiple instances of React Refresh simultaneously
276288

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"options",
3535
"overlay",
3636
"sockets",
37-
"types"
37+
"types",
38+
"umd"
3839
],
3940
"packageManager": "[email protected]+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610",
4041
"scripts": {
@@ -54,8 +55,9 @@
5455
"types:clean": "rimraf types",
5556
"types:compile": "tsc -p tsconfig.json",
5657
"types:prune-private": "del \"types/*/*\" \"!types/{lib,loader,options}/{index,types}.d.ts\"",
57-
"generate-types": "run-s types:clean types:compile types:prune-private \"format --loglevel silent\"",
58-
"prepublishOnly": "yarn generate-types"
58+
"generate:client-external": "webpack",
59+
"generate:types": "run-s types:clean types:compile types:prune-private \"format --log-level silent\"",
60+
"prepublishOnly": "run-p generate:*"
5961
},
6062
"dependencies": {
6163
"ansi-html": "^0.0.9",
@@ -97,6 +99,7 @@
9799
"puppeteer": "^22.10.0",
98100
"react-refresh": "^0.14.2",
99101
"sourcemap-validator": "^2.1.0",
102+
"terser-webpack-plugin": "^5.3.10",
100103
"type-fest": "^4.18.3",
101104
"typescript": "~5.4.5",
102105
"webpack": "^5.94.0",

webpack.config.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const path = require('node:path');
2+
const TerserPlugin = require('terser-webpack-plugin');
3+
4+
module.exports = {
5+
mode: 'production',
6+
entry: {
7+
client: './client/ReactRefreshEntry.js',
8+
},
9+
optimization: {
10+
minimize: true,
11+
minimizer: [
12+
new TerserPlugin({
13+
extractComments: false,
14+
terserOptions: {
15+
format: { comments: false },
16+
},
17+
}),
18+
],
19+
nodeEnv: 'development',
20+
},
21+
output: {
22+
filename: '[name].min.js',
23+
path: path.resolve(__dirname, 'umd'),
24+
},
25+
};

0 commit comments

Comments
 (0)