1 |
|
2 |
|
3 |
|
4 |
|
5 | const originalFetch = global.fetch;
|
6 | delete global.fetch;
|
7 |
|
8 | const { getOptions } = require('loader-utils');
|
9 | const { validate: validateOptions } = require('schema-utils');
|
10 | const { SourceMapConsumer, SourceNode } = require('source-map');
|
11 | const {
|
12 | getIdentitySourceMap,
|
13 | getModuleSystem,
|
14 | getRefreshModuleRuntime,
|
15 | normalizeOptions,
|
16 | } = require('./utils');
|
17 | const schema = require('./options.json');
|
18 |
|
19 | const RefreshRuntimePath = require
|
20 | .resolve('react-refresh/runtime.js')
|
21 | .replace(/\\/g, '/')
|
22 | .replace(/'/g, "\\'");
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function ReactRefreshLoader(source, inputSourceMap, meta) {
|
35 | let options = getOptions(this);
|
36 | validateOptions(schema, options, {
|
37 | baseDataPath: 'options',
|
38 | name: 'React Refresh Loader',
|
39 | });
|
40 |
|
41 | options = normalizeOptions(options);
|
42 |
|
43 | const callback = this.async();
|
44 |
|
45 | const { ModuleFilenameHelpers, Template } = this._compiler.webpack || require('webpack');
|
46 |
|
47 | const RefreshSetupRuntimes = {
|
48 | cjs: Template.asString(
|
49 | `__webpack_require__.$Refresh$.runtime = require('${RefreshRuntimePath}');`
|
50 | ),
|
51 | esm: Template.asString([
|
52 | `import * as __react_refresh_runtime__ from '${RefreshRuntimePath}';`,
|
53 | `__webpack_require__.$Refresh$.runtime = __react_refresh_runtime__;`,
|
54 | ]),
|
55 | };
|
56 |
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | async function _loader(source, inputSourceMap) {
|
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 | const node = SourceNode.fromStringWithSourceMap(
|
79 | source,
|
80 | await new SourceMapConsumer(originalSourceMap)
|
81 | );
|
82 |
|
83 | node.prepend([RefreshSetupRuntime, '\n\n']);
|
84 | node.add(['\n\n', RefreshModuleRuntime]);
|
85 |
|
86 | const { code, map } = node.toStringWithSourceMap();
|
87 | return [code, map.toJSON()];
|
88 | } else {
|
89 | return [[RefreshSetupRuntime, source, RefreshModuleRuntime].join('\n\n'), inputSourceMap];
|
90 | }
|
91 | }
|
92 |
|
93 | _loader.call(this, source, inputSourceMap).then(
|
94 | ([code, map]) => {
|
95 | callback(null, code, map, meta);
|
96 | },
|
97 | (error) => {
|
98 | callback(error);
|
99 | }
|
100 | );
|
101 | }
|
102 |
|
103 | module.exports = ReactRefreshLoader;
|
104 |
|
105 |
|
106 | if (originalFetch) {
|
107 | global.fetch = originalFetch;
|
108 | }
|