@pmmmwh/react-refresh-webpack-plugin
Advanced tools
Comparing version 0.5.10 to 0.5.11
@@ -11,5 +11,8 @@ function runWithRetry(callback, maxRetries) { | ||
} catch (err) { | ||
setTimeout(function () { | ||
executeWithRetryAndTimeout(currentCount + 1); | ||
}, Math.pow(10, currentCount)); | ||
setTimeout( | ||
function () { | ||
executeWithRetryAndTimeout(currentCount + 1); | ||
}, | ||
Math.pow(10, currentCount) | ||
); | ||
} | ||
@@ -16,0 +19,0 @@ } |
@@ -66,2 +66,17 @@ /* global __webpack_require__ */ | ||
/** | ||
* Creates a data object to be retained across refreshes. | ||
* This object should not transtively reference previous exports, | ||
* which can form infinite chain of objects across refreshes, which can pressure RAM. | ||
* | ||
* @param {*} moduleExports A Webpack module exports object. | ||
* @returns {*} A React refresh boundary signature array. | ||
*/ | ||
function getWebpackHotData(moduleExports) { | ||
return { | ||
signature: getReactRefreshBoundarySignature(moduleExports), | ||
isReactRefreshBoundary: isReactRefreshBoundary(moduleExports), | ||
}; | ||
} | ||
/** | ||
* Creates a helper that performs a delayed React refresh. | ||
@@ -171,10 +186,7 @@ * @returns {function(function(): void): void} A debounced React refresh function. | ||
* This implementation is based on the one in [Metro](https://github.com/facebook/metro/blob/907d6af22ac6ebe58572be418e9253a90665ecbd/packages/metro/src/lib/polyfills/require.js#L776-L792). | ||
* @param {*} prevExports The current Webpack module exports object. | ||
* @param {*} nextExports The next Webpack module exports object. | ||
* @param {*} prevSignature The signature of the current Webpack module exports object. | ||
* @param {*} nextSignature The signature of the next Webpack module exports object. | ||
* @returns {boolean} Whether the React refresh boundary should be invalidated. | ||
*/ | ||
function shouldInvalidateReactRefreshBoundary(prevExports, nextExports) { | ||
var prevSignature = getReactRefreshBoundarySignature(prevExports); | ||
var nextSignature = getReactRefreshBoundarySignature(nextExports); | ||
function shouldInvalidateReactRefreshBoundary(prevSignature, nextSignature) { | ||
if (prevSignature.length !== nextSignature.length) { | ||
@@ -199,5 +211,5 @@ return true; | ||
var isHotUpdate = !!webpackHot.data; | ||
var prevExports; | ||
var prevData; | ||
if (isHotUpdate) { | ||
prevExports = webpackHot.data.prevExports; | ||
prevData = webpackHot.data.prevData; | ||
} | ||
@@ -215,3 +227,3 @@ | ||
// We have to mutate the data object to get data registered and cached | ||
data.prevExports = moduleExports; | ||
data.prevData = getWebpackHotData(moduleExports); | ||
} | ||
@@ -242,4 +254,8 @@ ); | ||
if ( | ||
isReactRefreshBoundary(prevExports) && | ||
shouldInvalidateReactRefreshBoundary(prevExports, moduleExports) | ||
prevData && | ||
prevData.isReactRefreshBoundary && | ||
shouldInvalidateReactRefreshBoundary( | ||
prevData.signature, | ||
getReactRefreshBoundarySignature(moduleExports) | ||
) | ||
) { | ||
@@ -262,3 +278,3 @@ webpackHot.invalidate(); | ||
} else { | ||
if (isHotUpdate && typeof prevExports !== 'undefined') { | ||
if (isHotUpdate && typeof prevData !== 'undefined') { | ||
webpackHot.invalidate(); | ||
@@ -275,4 +291,3 @@ } | ||
isReactRefreshBoundary: isReactRefreshBoundary, | ||
shouldInvalidateReactRefreshBoundary: shouldInvalidateReactRefreshBoundary, | ||
registerExportsForReactRefresh: registerExportsForReactRefresh, | ||
}); |
@@ -28,25 +28,29 @@ const path = require('path'); | ||
// Include and exclude user-specified files | ||
if (!match(moduleData.matchResource || moduleData.resource)) return moduleData; | ||
// Include and exclude dynamically generated modules from other loaders | ||
if (moduleData.matchResource && !match(moduleData.request)) return moduleData; | ||
// Exclude files referenced as assets | ||
if (moduleData.type.includes('asset')) return moduleData; | ||
// Check to prevent double injection | ||
if (moduleData.loaders.find(({ loader }) => loader === resolvedLoader)) return moduleData; | ||
// Skip react-refresh and the plugin's runtime utils to prevent self-referencing - | ||
// this is useful when using the plugin as a direct dependency, | ||
// or when node_modules are specified to be processed. | ||
if ( | ||
// Include and exclude user-specified files | ||
match(moduleData.matchResource || moduleData.resource) && | ||
// Exclude files referenced as assets | ||
!moduleData.type.includes('asset') && | ||
// Skip react-refresh and the plugin's runtime utils to prevent self-referencing - | ||
// this is useful when using the plugin as a direct dependency, | ||
// or when node_modules are specified to be processed. | ||
!moduleData.resource.includes(reactRefreshPath) && | ||
!moduleData.resource.includes(refreshUtilsPath) && | ||
// Check to prevent double injection | ||
!moduleData.loaders.find(({ loader }) => loader === resolvedLoader) | ||
moduleData.resource.includes(reactRefreshPath) || | ||
moduleData.resource.includes(refreshUtilsPath) | ||
) { | ||
// As we inject runtime code for each module, | ||
// it is important to run the injected loader after everything. | ||
// This way we can ensure that all code-processing have been done, | ||
// and we won't risk breaking tools like Flow or ESLint. | ||
moduleData.loaders.unshift({ | ||
loader: resolvedLoader, | ||
options, | ||
}); | ||
return moduleData; | ||
} | ||
// As we inject runtime code for each module, | ||
// it is important to run the injected loader after everything. | ||
// This way we can ensure that all code-processing have been done, | ||
// and we won't risk breaking tools like Flow or ESLint. | ||
moduleData.loaders.unshift({ | ||
loader: resolvedLoader, | ||
options, | ||
}); | ||
return moduleData; | ||
@@ -53,0 +57,0 @@ } |
@@ -58,3 +58,3 @@ // This is a patch for mozilla/source-map#349 - | ||
/** | ||
* @this {import('webpack').loader.LoaderContext} | ||
* @this {import('webpack').LoaderContext<import('./types').ReactRefreshLoaderOptions>} | ||
* @param {string} source | ||
@@ -65,3 +65,8 @@ * @param {import('source-map').RawSourceMap} [inputSourceMap] | ||
async function _loader(source, inputSourceMap) { | ||
const moduleSystem = await getModuleSystem.call(this, ModuleFilenameHelpers, options); | ||
/** @type {'esm' | 'cjs'} */ | ||
let moduleSystem = 'cjs'; | ||
// Only try to resolve the module system if the environment is known to support ES syntax | ||
if (this.environment != null) { | ||
moduleSystem = await getModuleSystem.call(this, ModuleFilenameHelpers, options); | ||
} | ||
@@ -68,0 +73,0 @@ const RefreshSetupRuntime = RefreshSetupRuntimes[moduleSystem]; |
@@ -6,4 +6,4 @@ const { promises: fsPromises } = require('fs'); | ||
/** @type {string | undefined} */ | ||
let packageJsonType; | ||
/** @type {Map<string, string | undefined>} */ | ||
let packageJsonTypeMap = new Map(); | ||
@@ -49,2 +49,3 @@ /** | ||
// We will cache the results in a global variable so it will only be parsed once. | ||
let packageJsonType = packageJsonTypeMap.get(this.rootContext); | ||
if (!packageJsonType) { | ||
@@ -66,2 +67,3 @@ try { | ||
({ type: packageJsonType } = JSON.parse(rawPackageJson)); | ||
packageJsonTypeMap.set(this.rootContext, packageJsonType); | ||
} catch (e) { | ||
@@ -68,0 +70,0 @@ // Failed to parse `package.json`, do nothing. |
{ | ||
"name": "@pmmmwh/react-refresh-webpack-plugin", | ||
"version": "0.5.10", | ||
"version": "0.5.11", | ||
"description": "An **EXPERIMENTAL** Webpack plugin to enable \"Fast Refresh\" (also previously known as _Hot Reloading_) for React components.", | ||
@@ -77,3 +77,3 @@ "keywords": [ | ||
"@types/module-alias": "^2.0.0", | ||
"@types/node": "^18.7.21", | ||
"@types/node": "^20.5.0", | ||
"@types/semver": "^7.3.9", | ||
@@ -86,4 +86,4 @@ "@types/webpack": "^5.28.0", | ||
"eslint": "^8.6.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"eslint-config-prettier": "^9.0.0", | ||
"eslint-plugin-prettier": "^5.0.0", | ||
"fs-extra": "^10.0.0", | ||
@@ -98,10 +98,10 @@ "get-port": "^5.1.1", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^2.3.0", | ||
"prettier": "^3.0.1", | ||
"puppeteer": "^13.4.0", | ||
"react-refresh": "^0.14.0", | ||
"semver": "^7.3.5", | ||
"semver": "^7.5.2", | ||
"sourcemap-validator": "^2.1.0", | ||
"type-fest": "^3.0.0", | ||
"typescript": "~4.8.4", | ||
"webpack": "^5.42.0", | ||
"type-fest": "^4.0.0", | ||
"typescript": "~5.1.6", | ||
"webpack": "^5.76.0", | ||
"webpack-cli": "^4.7.2", | ||
@@ -121,3 +121,3 @@ "webpack-cli.legacy": "npm:webpack-cli@3.x", | ||
"sockjs-client": "^1.4.0", | ||
"type-fest": ">=0.17.0 <4.0.0", | ||
"type-fest": ">=0.17.0 <5.0.0", | ||
"webpack": ">=4.43.0 <6.0.0", | ||
@@ -149,3 +149,3 @@ "webpack-dev-server": "3.x || 4.x", | ||
"resolutions": { | ||
"type-fest": "3.2.0" | ||
"type-fest": "^4.0.0" | ||
}, | ||
@@ -152,0 +152,0 @@ "engines": { |
130571
3145