MCPcopy Index your code
hub / github.com/pmmmwh/react-refresh-webpack-plugin / ReactRefreshLoader

Function ReactRefreshLoader

loader/index.js:33–100  ·  view source on GitHub ↗

* A simple Webpack loader to inject react-refresh HMR code into modules. * * [Reference for Loader API](https://webpack.js.org/api/loaders/) * @this {import('webpack').LoaderContext } * @param {string} source The original module source code. * @param

(source, inputSourceMap, meta)

Source from the content-addressed store, hash-verified

31 * @returns {void}
32 */
33function ReactRefreshLoader(source, inputSourceMap, meta) {
34 let options = this.getOptions();
35 validateOptions(schema, options, {
36 baseDataPath: 'options',
37 name: 'React Refresh Loader',
38 });
39
40 options = normalizeOptions(options);
41
42 const callback = this.async();
43
44 const { ModuleFilenameHelpers, Template } = this._compiler.webpack || require('webpack');
45
46 const RefreshSetupRuntimes = {
47 cjs: Template.asString(
48 `__webpack_require__.$Refresh$.runtime = require('${RefreshRuntimePath}');`
49 ),
50 esm: Template.asString([
51 `import * as __react_refresh_runtime__ from '${RefreshRuntimePath}';`,
52 `__webpack_require__.$Refresh$.runtime = __react_refresh_runtime__;`,
53 ]),
54 };
55
56 /**
57 * @this {import('webpack').LoaderContext<import('./types').ReactRefreshLoaderOptions>}
58 * @param {string} source
59 * @param {import('source-map').RawSourceMap} [inputSourceMap]
60 * @returns {Promise<[string, import('source-map').RawSourceMap]>}
61 */
62 async function _loader(source, inputSourceMap) {
63 /** @type {'esm' | 'cjs'} */
64 const moduleSystem = await getModuleSystem.call(this, ModuleFilenameHelpers, options);
65
66 const RefreshSetupRuntime = RefreshSetupRuntimes[moduleSystem];
67 const RefreshModuleRuntime = getRefreshModuleRuntime(Template, {
68 const: options.const,
69 moduleSystem,
70 });
71
72 if (this.sourceMap) {
73 let originalSourceMap = inputSourceMap;
74 if (!originalSourceMap) {
75 originalSourceMap = getIdentitySourceMap(source, this.resourcePath);
76 }
77
78 return SourceMapConsumer.with(originalSourceMap, undefined, (consumer) => {
79 const node = SourceNode.fromStringWithSourceMap(source, consumer);
80
81 node.prepend([RefreshSetupRuntime, '\n\n']);
82 node.add(['\n\n', RefreshModuleRuntime]);
83
84 const { code, map } = node.toStringWithSourceMap();
85 return [code, map.toJSON()];
86 });
87 } else {
88 return [[RefreshSetupRuntime, source, RefreshModuleRuntime].join('\n\n'), inputSourceMap];
89 }
90 }

Callers

nothing calls this directly

Calls 1

normalizeOptionsFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…