-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: feature detect Webpack compiler version (#415)
- Loading branch information
Showing
22 changed files
with
478 additions
and
244 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
const { version } = require('webpack'); | ||
/** | ||
* Gets current bundle's global scope identifier for React Refresh. | ||
* @param {Record<string, string>} runtimeGlobals The Webpack runtime globals. | ||
* @returns {string} The React Refresh global scope within the Webpack bundle. | ||
*/ | ||
module.exports.getRefreshGlobalScope = (runtimeGlobals) => { | ||
return `${runtimeGlobals.require || '__webpack_require__'}.$Refresh$`; | ||
}; | ||
|
||
// Parse the major version of Webpack: x.y.z => x | ||
const webpackVersion = parseInt(version || '', 10); | ||
/** | ||
* Gets current Webpack version according to features on the compiler instance. | ||
* @param {import('webpack').Compiler} compiler The current Webpack compiler instance. | ||
* @returns {number} The current Webpack version. | ||
*/ | ||
module.exports.getWebpackVersion = (compiler) => { | ||
if (!compiler.hooks) { | ||
throw new Error(`[ReactRefreshPlugin] Webpack version is not supported!`); | ||
} | ||
|
||
let webpackGlobals = {}; | ||
if (webpackVersion === 5) { | ||
webpackGlobals = require('webpack/lib/RuntimeGlobals'); | ||
} | ||
|
||
module.exports.webpackVersion = webpackVersion; | ||
module.exports.webpackRequire = webpackGlobals.require || '__webpack_require__'; | ||
module.exports.refreshGlobal = `${module.exports.webpackRequire}.$Refresh$`; | ||
// Webpack v5+ implements compiler caching | ||
return 'cache' in compiler ? 5 : 4; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.