Skip to content
This repository was archived by the owner on Sep 13, 2022. It is now read-only.

Commit d04f07a

Browse files
author
virgoone
committed
feat: 增加 react refresh
1 parent bbff460 commit d04f07a

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@babel/preset-react": "^7.16.7",
4646
"@babel/preset-typescript": "^7.16.7",
4747
"@babel/runtime-corejs2": "^7.16.7",
48+
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.4",
4849
"@svgr/webpack": "^6.1.2",
4950
"babel-eslint": "10.1.0",
5051
"babel-jest": "^27.4.6",
@@ -75,6 +76,7 @@
7576
"postcss-preset-env": "^7.2.0",
7677
"postcss-safe-parser": "^6.0.0",
7778
"react-dev-utils": "^12.0.0",
79+
"react-refresh": "^0.11.0",
7880
"resolve-url-loader": "^4.0.0",
7981
"sass": "^1.46.0",
8082
"sass-loader": "^12.4.0",

src/variables/variables.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const variables = {
4040
CLI_VERSION: pkg.version,
4141
BUILD_TIME: `${buildTime.toLocaleDateString()} ${buildTime.toLocaleTimeString()}`,
4242
APP_LOG: true,
43+
FAST_REFRESH: process.env.FAST_REFRESH !== 'false',
4344
...customVariables
4445
}
4546

src/webpack/webpack.config.dev.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1-
const path = require('path')
21
const webpack = require('webpack')
32
const { merge } = require('webpack-merge')
43
const ignoredFiles = require('react-dev-utils/ignoredFiles')
54
const redirectServedPath = require('react-dev-utils/redirectServedPathMiddleware')
65
const noopServiceWorkerMiddleware = require('react-dev-utils/noopServiceWorkerMiddleware')
76
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')
87
const evalSourceMapMiddleware = require('react-dev-utils/evalSourceMapMiddleware')
8+
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin')
9+
910
const { appPublic, appSrc } = require('../variables/paths')
10-
const { PUBLIC_PATH } = require('../variables/variables')
11+
const { PUBLIC_PATH, FAST_REFRESH } = require('../variables/variables')
1112
const configFactory = require('./webpack.config')
1213
const { processWebpackConfig } = require('../utils/custom-config')
13-
const paths = require('../variables/paths')
1414

1515
module.exports = () =>
1616
processWebpackConfig(
1717
merge(
1818
configFactory({
1919
plugins: [
2020
new webpack.HotModuleReplacementPlugin(),
21+
FAST_REFRESH &&
22+
new ReactRefreshWebpackPlugin({
23+
overlay: false
24+
}),
2125
new CaseSensitivePathsPlugin()
2226
]
2327
}),

src/webpack/webpack.config.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const resolve = require('resolve')
44
const path = require('path')
55
const HtmlWebpackPlugin = require('html-webpack-plugin')
66
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
7+
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin')
78
const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin')
89
const fs = require('fs-extra')
910

@@ -18,6 +19,11 @@ const { appIndex, appSrc, appHtml, appPolyfill } = require('../variables/paths')
1819
const variables = require('../variables/variables')
1920
const paths = require('../variables/paths')
2021

22+
const reactRefreshRuntimeEntry = require.resolve('react-refresh/runtime')
23+
const reactRefreshWebpackPluginRuntimeEntry = require.resolve(
24+
'@pmmmwh/react-refresh-webpack-plugin'
25+
)
26+
2127
const { __DEV__, PUBLIC_PATH: publicPath, APP_ENV } = variables
2228
const stringified = Object.keys(variables).reduce(
2329
(acc, key) => {
@@ -155,7 +161,19 @@ module.exports = ({ entry = [], plugins = [] }) => {
155161
'@': appSrc,
156162
src: appSrc
157163
},
158-
extensions: ['.ts', '.tsx', '.jsx', '.js', '.scss', '.sass', '.less']
164+
extensions: ['.ts', '.tsx', '.jsx', '.js', '.scss', '.sass', '.less'],
165+
plugins: [
166+
// Prevents users from importing files from outside of src/ (or node_modules/).
167+
// This often causes confusion because we only process files within src/ with babel.
168+
// To fix this, we prevent you from importing files out of src/ -- if you'd like to,
169+
// please link the files into your node_modules/ and let module-resolution kick in.
170+
// Make sure your source files are compiled, as they will not be processed in any way.
171+
new ModuleScopePlugin(paths.appSrc, [
172+
paths.appPackageJson,
173+
reactRefreshRuntimeEntry,
174+
reactRefreshWebpackPluginRuntimeEntry
175+
])
176+
]
159177
},
160178
module: {
161179
strictExportPresence: true,

yarn.lock

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
dependencies:
172172
"@babel/types" "^7.16.7"
173173

174-
"@babel/helper-module-imports@^7.0.0", "@babel/helper-module-imports@^7.0.0-beta.49", "@babel/helper-module-imports@^7.12.13", "@babel/helper-module-imports@^7.16.0", "@babel/helper-module-imports@^7.16.7":
174+
"@babel/helper-module-imports@^7.0.0", "@babel/helper-module-imports@^7.0.0-beta.49", "@babel/helper-module-imports@^7.12.13", "@babel/helper-module-imports@^7.16.7":
175175
version "7.16.7"
176176
resolved "https://registry.yarnpkg.com/@babel/helper-module-imports/-/helper-module-imports-7.16.7.tgz#25612a8091a999704461c8a222d0efec5d091437"
177177
integrity sha512-LVtS6TqjJHFc+nYeITRo6VLXve70xmq7wPhWTqDJusJEgGmkAACWwMiTNrvfoQo6hEhFwAIixNkvB0jPXDL8Wg==
@@ -199,7 +199,7 @@
199199
dependencies:
200200
"@babel/types" "^7.16.7"
201201

202-
"@babel/helper-plugin-utils@^7.0.0", "@babel/helper-plugin-utils@^7.10.4", "@babel/helper-plugin-utils@^7.12.13", "@babel/helper-plugin-utils@^7.13.0", "@babel/helper-plugin-utils@^7.14.5", "@babel/helper-plugin-utils@^7.16.5", "@babel/helper-plugin-utils@^7.16.7", "@babel/helper-plugin-utils@^7.8.0", "@babel/helper-plugin-utils@^7.8.3":
202+
"@babel/helper-plugin-utils@^7.0.0", "@babel/helper-plugin-utils@^7.10.4", "@babel/helper-plugin-utils@^7.12.13", "@babel/helper-plugin-utils@^7.13.0", "@babel/helper-plugin-utils@^7.14.5", "@babel/helper-plugin-utils@^7.16.7", "@babel/helper-plugin-utils@^7.8.0", "@babel/helper-plugin-utils@^7.8.3":
203203
version "7.16.7"
204204
resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.16.7.tgz#aa3a8ab4c3cceff8e65eb9e73d87dc4ff320b2f5"
205205
integrity sha512-Qg3Nk7ZxpgMrsox6HreY1ZNKdBq7K72tDSliA6dCl5f007jR4ne8iD5UzuNnCJH2xBf2BEEVGr+/OL6Gdp7RxA==
@@ -1191,6 +1191,21 @@
11911191
"@nodelib/fs.scandir" "2.1.5"
11921192
fastq "^1.6.0"
11931193

1194+
"@pmmmwh/react-refresh-webpack-plugin@^0.5.4":
1195+
version "0.5.4"
1196+
resolved "https://registry.yarnpkg.com/@pmmmwh/react-refresh-webpack-plugin/-/react-refresh-webpack-plugin-0.5.4.tgz#df0d0d855fc527db48aac93c218a0bf4ada41f99"
1197+
integrity sha512-zZbZeHQDnoTlt2AF+diQT0wsSXpvWiaIOZwBRdltNFhG1+I3ozyaw7U/nBiUwyJ0D+zwdXp0E3bWOl38Ag2BMw==
1198+
dependencies:
1199+
ansi-html-community "^0.0.8"
1200+
common-path-prefix "^3.0.0"
1201+
core-js-pure "^3.8.1"
1202+
error-stack-parser "^2.0.6"
1203+
find-up "^5.0.0"
1204+
html-entities "^2.1.0"
1205+
loader-utils "^2.0.0"
1206+
schema-utils "^3.0.0"
1207+
source-map "^0.7.3"
1208+
11941209
"@svgr/babel-plugin-add-jsx-attribute@^6.0.0":
11951210
version "6.0.0"
11961211
resolved "https://registry.yarnpkg.com/@svgr/babel-plugin-add-jsx-attribute/-/babel-plugin-add-jsx-attribute-6.0.0.tgz#bd6d1ff32a31b82b601e73672a789cc41e84fe18"
@@ -2603,6 +2618,11 @@ commander@^8.3.0:
26032618
resolved "https://registry.yarnpkg.com/commander/-/commander-8.3.0.tgz#4837ea1b2da67b9c616a67afbb0fafee567bca66"
26042619
integrity sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==
26052620

2621+
common-path-prefix@^3.0.0:
2622+
version "3.0.0"
2623+
resolved "https://registry.yarnpkg.com/common-path-prefix/-/common-path-prefix-3.0.0.tgz#7d007a7e07c58c4b4d5f433131a19141b29f11e0"
2624+
integrity sha512-QE33hToZseCH3jS0qN96O/bSh3kaw/h+Tq7ngyY9eWDUnTlTNUyqfqvCXioLe5Na5jFsL78ra/wuBU4iuEgd4w==
2625+
26062626
commondir@^1.0.1:
26072627
version "1.0.1"
26082628
resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"
@@ -2697,6 +2717,11 @@ core-js-compat@^3.18.0, core-js-compat@^3.19.1:
26972717
browserslist "^4.19.1"
26982718
semver "7.0.0"
26992719

2720+
core-js-pure@^3.8.1:
2721+
version "3.20.2"
2722+
resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.20.2.tgz#5d263565f0e34ceeeccdc4422fae3e84ca6b8c0f"
2723+
integrity sha512-CmWHvSKn2vNL6p6StNp1EmMIfVY/pqn3JLAjfZQ8WZGPOlGoO92EkX9/Mk81i6GxvoPXjUqEQnpM3rJ5QxxIOg==
2724+
27002725
core-js@^2.6.5:
27012726
version "2.6.12"
27022727
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.12.tgz#d9333dfa7b065e347cc5682219d6f690859cc2ec"
@@ -3187,6 +3212,13 @@ error-ex@^1.3.1:
31873212
dependencies:
31883213
is-arrayish "^0.2.1"
31893214

3215+
error-stack-parser@^2.0.6:
3216+
version "2.0.6"
3217+
resolved "https://registry.yarnpkg.com/error-stack-parser/-/error-stack-parser-2.0.6.tgz#5a99a707bd7a4c58a797902d48d82803ede6aad8"
3218+
integrity sha512-d51brTeqC+BHlwF0BhPtcYgF5nlzf9ZZ0ZIUQNZpc9ZB9qw5IJ2diTrBY9jlCJkTLITYPjmiX6OWCwH+fuyNgQ==
3219+
dependencies:
3220+
stackframe "^1.1.1"
3221+
31903222
es-abstract@^1.19.0, es-abstract@^1.19.1:
31913223
version "1.19.1"
31923224
resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.19.1.tgz#d4885796876916959de78edaa0df456627115ec3"
@@ -4158,7 +4190,7 @@ hpack.js@^2.1.6:
41584190
readable-stream "^2.0.1"
41594191
wbuf "^1.1.0"
41604192

4161-
html-entities@^2.3.2:
4193+
html-entities@^2.1.0, html-entities@^2.3.2:
41624194
version "2.3.2"
41634195
resolved "https://registry.yarnpkg.com/html-entities/-/html-entities-2.3.2.tgz#760b404685cb1d794e4f4b744332e3b00dcfe488"
41644196
integrity sha512-c3Ab/url5ksaT0WyleslpBEthOzWhrjQbg75y7XUsfSzi3Dgzt0l8w5e7DylRn15MTlMMD58dTfzddNS2kcAjQ==
@@ -6429,6 +6461,11 @@ react-is@^17.0.1:
64296461
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
64306462
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==
64316463

6464+
react-refresh@^0.11.0:
6465+
version "0.11.0"
6466+
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.11.0.tgz#77198b944733f0f1f1a90e791de4541f9f074046"
6467+
integrity sha512-F27qZr8uUqwhWZboondsPx8tnC3Ct3SxZA3V5WyEvujRyyNv0VYPhoBg1gZ8/MV5tubQp76Trw8lTv9hzRBa+A==
6468+
64326469
64336470
version "7.0.1"
64346471
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-7.0.1.tgz#f3a6135758459733ae2b95638056e1854e7ef507"
@@ -7064,7 +7101,7 @@ source-map@^0.5.0, source-map@^0.5.6:
70647101
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"
70657102
integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=
70667103

7067-
source-map@~0.7.2:
7104+
source-map@^0.7.3, source-map@~0.7.2:
70687105
version "0.7.3"
70697106
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
70707107
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==
@@ -7135,6 +7172,11 @@ stable@^0.1.8:
71357172
resolved "https://registry.yarnpkg.com/stable/-/stable-0.1.8.tgz#836eb3c8382fe2936feaf544631017ce7d47a3cf"
71367173
integrity sha512-ji9qxRnOVfcuLDySj9qzhGSEFVobyt1kIOSkj1qZzYLzq7Tos/oUUWvotUPQLlrsidqsK6tBH89Bc9kL5zHA6w==
71377174

7175+
stackframe@^1.1.1:
7176+
version "1.2.0"
7177+
resolved "https://registry.yarnpkg.com/stackframe/-/stackframe-1.2.0.tgz#52429492d63c62eb989804c11552e3d22e779303"
7178+
integrity sha512-GrdeshiRmS1YLMYgzF16olf2jJ/IzxXY9lhKOskuVziubpTYcYqyOwYeJKzQkwy7uN0fYSsbsC4RQaXf9LCrYA==
7179+
71387180
static-extend@^0.1.1:
71397181
version "0.1.2"
71407182
resolved "https://registry.yarnpkg.com/static-extend/-/static-extend-0.1.2.tgz#60809c39cbff55337226fd5e0b520f341f1fb5c6"

0 commit comments

Comments
 (0)