/
ReactRefreshRuntime.js
54 lines (49 loc) 路 1.28 KB
/
ReactRefreshRuntime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// @flow strict-local
import {Runtime} from '@parcel/plugin';
import {loadConfig} from '@parcel/utils';
const CODE = `
var Refresh = require('react-refresh/runtime');
Refresh.injectIntoGlobalHook(window);
window.$RefreshReg$ = function() {};
window.$RefreshSig$ = function() {
return function(type) {
return type;
};
};`;
export default (new Runtime({
async apply({bundle, options}) {
if (
bundle.type !== 'js' ||
!options.hmrOptions ||
!bundle.env.isBrowser() ||
bundle.env.isLibrary ||
bundle.env.isWorker() ||
bundle.env.isWorklet() ||
options.mode !== 'development' ||
bundle.env.sourceType !== 'module'
) {
return;
}
let entries = bundle.getEntryAssets();
for (let entry of entries) {
// TODO: do this in loadConfig - but it doesn't have access to the bundle...
let pkg = await loadConfig(
options.inputFS,
entry.filePath,
['package.json'],
options.projectRoot,
);
if (
pkg?.config?.dependencies?.react ||
pkg?.config?.devDependencies?.react ||
pkg?.config?.peerDependencies?.react
) {
return {
filePath: __filename,
code: CODE,
isEntry: true,
};
}
}
},
}): Runtime);