Skip to content

Commit 01f9a35

Browse files
authored
WRN-19694: Support for React18 (#92)
* update react version Enact-DCO-1.0-Signed-off-by: Mikyung Kim (mikyung27.kim@lge.com) * update dependencies Enact-DCO-1.0-Signed-off-by: Mikyung Kim (mikyung27.kim@lge.com) * WRN-19694: Add react18 support Enact-DCO-1.0-Signed-off-by: Mikyung Kim (mikyung27.kim@lge.com) * fix Enact-DCO-1.0-Signed-off-by: Mikyung Kim (mikyung27.kim@lge.com)
1 parent d555759 commit 01f9a35

File tree

8 files changed

+22
-17
lines changed

8 files changed

+22
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* `IlibPlugin`:
66
* Added `ILIB_ADDITIONAL_RESOURCES_PATH` to defined constants if provided.
77
* Added `publicPath` option to specify webpack public path.
8+
* `PrerenderPlugin`: Added React18 support for `ReactDOMClient.hydrateRoot` instead of `ReactDOMClient.createRoot` for prerendered apps.
89

910
# 4.1.4 (February 18, 2022)
1011

npm-shrinkwrap.json

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"eslint-plugin-react-hooks": "^4.3.0",
7272
"html-webpack-plugin": "^5.3.2",
7373
"prettier": "^2.6.0",
74-
"react": "^17.0.2",
74+
"react": "^18.0.0",
7575
"webpack": "5.64.4"
7676
}
7777
}

plugins/PrerenderPlugin/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,15 @@ class PrerenderPlugin {
320320
});
321321

322322
compiler.hooks.emit.tapAsync('PrerenderPlugin', (compilation, callback) => {
323-
// Replace ReactDOM.render to ReactDOM.hydrate from main.js
323+
// Replace ReactDOMClient.createRoot to ReactDOMClient.hydrateRoot from main.js
324324
const data = compilation.assets[opts.chunk].source();
325-
const regex = /render(?=(?:(?!render|getElementById).)*getElementById\(('|")root)/;
326-
const replacedData = data.replace(regex, 'hydrate');
325+
const createRootRegex = /createRoot\)(\(container|\(document\.getElementById\(('|")root('|")\))/;
326+
const hydrateRootData = data.replace(
327+
createRootRegex,
328+
`hydrateRoot)(document.getElementById(\'root\'), appElement`
329+
);
330+
const renderRegex = /root.render\(appElement\);/;
331+
const replacedData = hydrateRootData.replace(renderRegex, '');
327332

328333
emitAsset(compilation, opts.chunk, replacedData);
329334

plugins/PrerenderPlugin/templates.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ const startup = (screenTypes, jsAssets) => `
5858
if(typeof updateEnvironment === 'function') {
5959
updateEnvironment();
6060
}
61-
if(typeof App === 'object' && (typeof ReactDOM === 'object')) {
62-
ReactDOM.hydrate(App['default'] || App, document.getElementById('root'));
61+
if(typeof App === 'object' && (typeof ReactDOMClient === 'object')) {
62+
ReactDOMClient.hydrateRoot(App['default'] || App, document.getElementById('root'));
6363
} else {
6464
console.log('ERROR: Snapshot app not found');
6565
}

plugins/SnapshotPlugin/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class SnapshotPlugin {
6565
apply(compiler) {
6666
const opts = this.options;
6767
const app = helper.appRoot();
68-
const reactDOM = path.resolve(path.join(app, 'node_modules', 'react-dom'));
68+
const reactDOMClient = path.resolve(path.join(app, 'node_modules', 'react-dom/client'));
6969
opts.blob = getBlobName(opts.args);
7070

7171
// Ignore packages that don't exists so snapshot helper can skip them
@@ -86,10 +86,10 @@ class SnapshotPlugin {
8686
compiler.hooks.normalModuleFactory.tap('SnapshotPlugin', factory => {
8787
factory.hooks.beforeResolve.tap('SnapshotPlugin', result => {
8888
if (!result) return;
89-
if (result.request === 'react-dom') {
89+
if (result.request === 'react-dom/client') {
9090
// When the request originates from the injected helper, point to real 'react-dom'
9191
if (result.contextInfo.issuer === SnapshotPlugin.helperJS) {
92-
result.request = reactDOM;
92+
result.request = reactDOMClient;
9393
} else {
9494
result.request = SnapshotPlugin.helperJS;
9595
}

plugins/SnapshotPlugin/mock-window.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
* mock-window.js
44
*
5-
* A helper utility meant to simulate a basic window object for ReactDOM usage during snapshot execution.
5+
* A helper utility meant to simulate a basic window object for ReactDOMClient usage during snapshot execution.
66
*/
77

88
var orig = {}, listeners = [], nop = function() {};

plugins/SnapshotPlugin/snapshot-helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ if (typeof window == 'undefined'
7676
ExecutionEnvironment.canUseEventListeners = true;
7777
ExecutionEnvironment.canUseViewport = true;
7878
ExecutionEnvironment.isInWorker = false;
79-
module.exports = global.ReactDOM = require('react-dom');
79+
module.exports = global.ReactDOMClient = require('react-dom/client');
8080
mockWindow.deactivate();
8181
} else {
82-
module.exports = global.ReactDOM = require('react-dom');
82+
module.exports = global.ReactDOMClient = require('react-dom/client');
8383
}

0 commit comments

Comments
 (0)