| 1 | /**
|
|---|
| 2 | * @typedef ModuleRuntimeOptions {Object}
|
|---|
| 3 | * @property {boolean} const Use ES6 `const` and `let` in generated runtime code.
|
|---|
| 4 | * @property {'cjs' | 'esm'} moduleSystem The module system to be used.
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * Generates code appended to each JS-like module for react-refresh capabilities.
|
|---|
| 9 | *
|
|---|
| 10 | * `__react_refresh_utils__` will be replaced with actual utils during source parsing by `webpack.ProvidePlugin`.
|
|---|
| 11 | *
|
|---|
| 12 | * [Reference for Runtime Injection](https://github.com/webpack/webpack/blob/b07d3b67d2252f08e4bb65d354a11c9b69f8b434/lib/HotModuleReplacementPlugin.js#L419)
|
|---|
| 13 | * [Reference for HMR Error Recovery](https://github.com/webpack/webpack/issues/418#issuecomment-490296365)
|
|---|
| 14 | *
|
|---|
| 15 | * @param {import('webpack').Template} Webpack's templating helpers.
|
|---|
| 16 | * @param {ModuleRuntimeOptions} options The refresh module runtime options.
|
|---|
| 17 | * @returns {string} The refresh module runtime template.
|
|---|
| 18 | */
|
|---|
| 19 | function getRefreshModuleRuntime(Template, options) {
|
|---|
| 20 | const constDeclaration = options.const ? 'const' : 'var';
|
|---|
| 21 | const letDeclaration = options.const ? 'let' : 'var';
|
|---|
| 22 | const webpackHot = options.moduleSystem === 'esm' ? 'import.meta.webpackHot' : 'module.hot';
|
|---|
| 23 | return Template.asString([
|
|---|
| 24 | `${constDeclaration} $ReactRefreshModuleId$ = __webpack_require__.$Refresh$.moduleId;`,
|
|---|
| 25 | `${constDeclaration} $ReactRefreshCurrentExports$ = __react_refresh_utils__.getModuleExports(`,
|
|---|
| 26 | Template.indent('$ReactRefreshModuleId$'),
|
|---|
| 27 | ');',
|
|---|
| 28 | '',
|
|---|
| 29 | 'function $ReactRefreshModuleRuntime$(exports) {',
|
|---|
| 30 | Template.indent([
|
|---|
| 31 | `if (${webpackHot}) {`,
|
|---|
| 32 | Template.indent([
|
|---|
| 33 | `${letDeclaration} errorOverlay;`,
|
|---|
| 34 | "if (typeof __react_refresh_error_overlay__ !== 'undefined') {",
|
|---|
| 35 | Template.indent('errorOverlay = __react_refresh_error_overlay__;'),
|
|---|
| 36 | '}',
|
|---|
| 37 | `${letDeclaration} testMode;`,
|
|---|
| 38 | "if (typeof __react_refresh_test__ !== 'undefined') {",
|
|---|
| 39 | Template.indent('testMode = __react_refresh_test__;'),
|
|---|
| 40 | '}',
|
|---|
| 41 | 'return __react_refresh_utils__.executeRuntime(',
|
|---|
| 42 | Template.indent([
|
|---|
| 43 | 'exports,',
|
|---|
| 44 | '$ReactRefreshModuleId$,',
|
|---|
| 45 | `${webpackHot},`,
|
|---|
| 46 | 'errorOverlay,',
|
|---|
| 47 | 'testMode',
|
|---|
| 48 | ]),
|
|---|
| 49 | ');',
|
|---|
| 50 | ]),
|
|---|
| 51 | '}',
|
|---|
| 52 | ]),
|
|---|
| 53 | '}',
|
|---|
| 54 | '',
|
|---|
| 55 | "if (typeof Promise !== 'undefined' && $ReactRefreshCurrentExports$ instanceof Promise) {",
|
|---|
| 56 | Template.indent('$ReactRefreshCurrentExports$.then($ReactRefreshModuleRuntime$);'),
|
|---|
| 57 | '} else {',
|
|---|
| 58 | Template.indent('$ReactRefreshModuleRuntime$($ReactRefreshCurrentExports$);'),
|
|---|
| 59 | '}',
|
|---|
| 60 | ]);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | module.exports = getRefreshModuleRuntime;
|
|---|