source: frontend/node_modules/@pmmmwh/react-refresh-webpack-plugin/lib/utils/makeRefreshRuntimeModule.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[9af201e]1/**
2 * Makes a runtime module to intercept module execution for React Refresh.
3 * This module creates an isolated `__webpack_require__` function for each module,
4 * and injects a `$Refresh$` object into it for use by React Refresh.
5 * @param {import('webpack')} webpack The Webpack exports.
6 * @returns {new () => import('webpack').RuntimeModule} The runtime module class.
7 */
8function makeRefreshRuntimeModule(webpack) {
9 return class ReactRefreshRuntimeModule extends webpack.RuntimeModule {
10 constructor() {
11 // Second argument is the `stage` for this runtime module -
12 // we'll use the same stage as Webpack's HMR runtime module for safety.
13 super('react refresh', webpack.RuntimeModule.STAGE_BASIC);
14 }
15
16 /**
17 * @returns {string} runtime code
18 */
19 generate() {
20 if (!this.compilation) throw new Error('Webpack compilation missing!');
21
22 const { runtimeTemplate } = this.compilation;
23 const declareVar = runtimeTemplate.supportsConst() ? 'const' : 'var';
24 return webpack.Template.asString([
25 `${declareVar} setup = ${runtimeTemplate.basicFunction('moduleId', [
26 `${declareVar} refresh = {`,
27 webpack.Template.indent([
28 `moduleId: moduleId,`,
29 `register: ${runtimeTemplate.basicFunction('type, id', [
30 `${declareVar} typeId = moduleId + " " + id;`,
31 `refresh.runtime.register(type, typeId);`,
32 ])},`,
33 `signature: ${runtimeTemplate.returningFunction(
34 'refresh.runtime.createSignatureFunctionForTransform()'
35 )},`,
36 `runtime: {`,
37 webpack.Template.indent([
38 `createSignatureFunctionForTransform: ${runtimeTemplate.returningFunction(
39 runtimeTemplate.returningFunction('type', 'type')
40 )},`,
41 `register: ${runtimeTemplate.emptyFunction()}`,
42 ]),
43 `},`,
44 ]),
45 `};`,
46 `return refresh;`,
47 ])}`,
48 '',
49 `${webpack.RuntimeGlobals.interceptModuleExecution}.push(${runtimeTemplate.basicFunction(
50 'options',
51 [
52 `${declareVar} originalFactory = options.factory;`,
53 // Using a function declaration -
54 // ensures `this` would propagate for modules relying on it
55 `options.factory = function(moduleObject, moduleExports, webpackRequire) {`,
56 webpack.Template.indent([
57 // Our require function delegates to the original require function
58 `${declareVar} hotRequire = ${runtimeTemplate.returningFunction(
59 'webpackRequire(request)',
60 'request'
61 )};`,
62 // The propery descriptor factory below ensures all properties but `$Refresh$`
63 // are proxied through to the original require function
64 `${declareVar} createPropertyDescriptor = ${runtimeTemplate.basicFunction('name', [
65 `return {`,
66 webpack.Template.indent([
67 `configurable: true,`,
68 `enumerable: true,`,
69 `get: ${runtimeTemplate.returningFunction('webpackRequire[name]')},`,
70 `set: ${runtimeTemplate.basicFunction('value', [
71 'webpackRequire[name] = value;',
72 ])},`,
73 ]),
74 `};`,
75 ])};`,
76 `for (${declareVar} name in webpackRequire) {`,
77 webpack.Template.indent([
78 `if (Object.prototype.hasOwnProperty.call(webpackRequire, name) && name !== "$Refresh$") {`,
79 webpack.Template.indent([
80 `Object.defineProperty(hotRequire, name, createPropertyDescriptor(name));`,
81 ]),
82 `}`,
83 ]),
84 `}`,
85 `hotRequire.$Refresh$ = setup(options.id);`,
86 `originalFactory.call(this, moduleObject, moduleExports, hotRequire);`,
87 ]),
88 '};',
89 ]
90 )});`,
91 ]);
92 }
93 };
94}
95
96module.exports = makeRefreshRuntimeModule;
Note: See TracBrowser for help on using the repository browser.