Skip to content

Commit 5b40aba

Browse files
authored
enable React 18 rendering of the examples (#1)
auto-detect React 17 / 18 and adjust the rendering method
1 parent 597266e commit 5b40aba

7 files changed

+72
-11
lines changed

.eslintrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,8 @@
6868
"no-console": "off"
6969
}
7070
}
71+
],
72+
"ignorePatterns": [
73+
"webpack.config.ts"
7174
]
7275
}

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
### [1.1.1] WIP
9+
10+
- Render the examples in React 18 mode when available
11+
812
## [1.1.0] 2021-05-04
913

1014
- Support custom grid elements

examples/index-react18.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import ReactDOMClient from 'react-dom/client';
3+
4+
import App from './App';
5+
6+
// eslint-disable-next-line no-console
7+
console.debug('React 18 mode');
8+
ReactDOMClient.createRoot(document.getElementById('root')).render(<App />);

examples/index.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ import ReactDOM from 'react-dom';
33

44
import App from './App';
55

6+
// eslint-disable-next-line no-console
7+
console.debug('React 16/17 mode');
68
ReactDOM.render(<App />, document.getElementById('root'));

package-lock.json

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"jest": "^27.5.1",
9191
"jest-canvas-mock": "^2.3.1",
9292
"markdown-loader": "^7.0.0",
93+
"null-loader": "^4.0.1",
9394
"prettier": "2.6.2",
9495
"prism-themes": "^1.6.0",
9596
"prismjs": "^1.28.0",

webpack.config.ts

+23-11
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ import webpack, {Configuration} from 'webpack';
44
import HtmlWebpackPlugin from 'html-webpack-plugin';
55
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
66
import {TsconfigPathsPlugin} from 'tsconfig-paths-webpack-plugin';
7+
import * as React from 'react';
78

89
const webpackConfig = (env): Configuration => {
10+
let reactMajorVersion = +React.version.split('.')[0];
11+
if (reactMajorVersion >= 18) {
12+
console.log('React 18 detected');
13+
} else {
14+
console.log('React 16/17 detected');
15+
}
16+
917
const conf: Configuration = {
10-
entry: './examples/index.tsx',
18+
entry: reactMajorVersion >= 18 ? './examples/index-react18.tsx' : './examples/index.tsx',
1119
...(env.production || !env.development ? {} : {devtool: 'eval-source-map'}),
1220
resolve: {
1321
alias: {
@@ -32,12 +40,7 @@ const webpackConfig = (env): Configuration => {
3240
options: {
3341
transpileOnly: true,
3442
configFile: 'examples/tsconfig.json'
35-
},
36-
exclude: /dist/
37-
},
38-
{
39-
test: /\.jsx$/,
40-
use: 'raw-loader'
43+
}
4144
},
4245
{
4346
test: /\.css$/i,
@@ -63,16 +66,25 @@ const webpackConfig = (env): Configuration => {
6366
DEBUG: !env.production || env.development
6467
}
6568
},
66-
VERSION: JSON.stringify(require('./package.json').version)
69+
VERSION: JSON.stringify(require('./package.json').version),
70+
MAPBOX_TOKEN: JSON.stringify(process.env.MAPBOX_TOKEN)
6771
})
6872
],
6973
devServer: {
70-
port: 8030,
71-
allowedHosts: ['all']
74+
port: 8030
7275
}
7376
};
7477

75-
if (env.production) {
78+
if (reactMajorVersion < 18) {
79+
// This is needed for React 16/17 as otherwise ts-loader
80+
// will pick `index-react18.tsx` and will fail transpiling it
81+
conf.module.rules.unshift({
82+
test: /index-react18\.tsx?$/,
83+
loader: 'null-loader'
84+
});
85+
}
86+
87+
if (!env.development) {
7688
conf.plugins.push(
7789
new ForkTsCheckerWebpackPlugin({
7890
eslint: {

0 commit comments

Comments
 (0)