-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
128 lines (111 loc) · 4.83 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
let fs = require('fs');
let runtime = fs.readFileSync(require.resolve('react-refresh/cjs/react-refresh-runtime.development.js'), 'utf8');
runtime = runtime.replace('process.env.NODE_ENV', JSON.stringify(process.env.NODE_ENV));
function ReactRefresh (opts = {}) {
return {
nollupBundleInit () {
return `
(function () {
let exports = {}; let module = { exports: exports };
${runtime};
window.$RefreshRuntime$ = exports;
})();
window.$RefreshRuntime$.injectIntoGlobalHook(window);
window.$RefreshReg$ = () => {};
window.$RefreshSig$ = () => type => type;
`
},
resolveId (id) {
if (id === 'react-refresh-runtime') {
return id;
}
},
load (id) {
if (id === 'react-refresh-runtime') {
return `
export function isReactRefreshBoundary(moduleExports) {
for (let key in moduleExports) {
let _c = moduleExports[key];
if ($RefreshRuntime$.isLikelyComponentType(_c)) {
$RefreshReg$(_c, _c.displayName || _c.name);
}
}
if ($RefreshRuntime$.isLikelyComponentType(moduleExports)) {
return true;
}
if (moduleExports == null || typeof moduleExports !== 'object') {
// Exit if we can't iterate over exports.
return false;
}
let hasExports = false;
let areAllExportsComponents = true;
for (const key in moduleExports) {
hasExports = true;
if (key === '__esModule') {
continue;
}
const desc = Object.getOwnPropertyDescriptor(moduleExports, key);
if (desc && desc.get) {
// Don't invoke getters as they may have side effects.
return false;
}
const exportValue = moduleExports[key];
if (!$RefreshRuntime$.isLikelyComponentType(exportValue)) {
areAllExportsComponents = false;
}
}
return hasExports && areAllExportsComponents;
};
export function __$RefreshCheck$__(m) {
if (isReactRefreshBoundary(m.exports)) {
m.hot.accept(() => require(m.id))
setTimeout(function () {
$RefreshRuntime$.performReactRefresh()
}, 0);
}
}
`;
}
},
nollupModuleWrap (code) {
return `
var prevRefreshReg = window.$RefreshReg$;
var prevRefreshSig = window.$RefreshSig$;
var RefreshRuntime = window.$RefreshRuntime$
if (RefreshRuntime) {
window.$RefreshReg$ = function (type, id) {
var fullId = module.id + ' ' + id;
RefreshRuntime.register(type, fullId);
}
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
}
try {
${code}
} finally {
window.$RefreshReg$ = prevRefreshReg;
window.$RefreshSig$ = prevRefreshSig;
}
`;
},
transform (code, id) {
if (id === 'react-refresh-runtime' || id.includes('node_modules') ) {
return;
}
if (
code.indexOf('React.createElement') === -1 && // React < 17
code.indexOf('react/jsx') === -1 // React 17+ (react/jsx-runtime & react/jsx-dev-runtime)
) {
return;
}
return {
code: [
code,
'import { __$RefreshCheck$__ } from "react-refresh-runtime"',
'__$RefreshCheck$__(module)'
].join(';'),
map: null
};
}
}
}
module.exports = ReactRefresh;