@@ -14,73 +14,29 @@ window.$RefreshSig$ = () => (type) => type;`
14
14
export const getPreambleCode = ( base : string ) : string =>
15
15
preambleCode . replace ( '__BASE__' , base )
16
16
17
- export const avoidSourceMapOption = Symbol ( )
18
-
19
- export function addRefreshWrapper < M extends { mappings : string } > (
17
+ export function addRefreshWrapper (
20
18
code : string ,
21
- map : M | string | typeof avoidSourceMapOption ,
22
19
pluginName : string ,
23
20
id : string ,
24
21
reactRefreshHost = '' ,
25
- ) : { code : string ; map : M | null | string } {
22
+ ) : string | undefined {
26
23
const hasRefresh = refreshContentRE . test ( code )
27
24
const onlyReactComp = ! hasRefresh && reactCompRE . test ( code )
28
- const normalizedMap = map === avoidSourceMapOption ? null : map
29
-
30
- if ( ! hasRefresh && ! onlyReactComp ) return { code, map : normalizedMap }
31
25
32
- const avoidSourceMap = map === avoidSourceMapOption
33
- const newMap =
34
- typeof normalizedMap === 'string'
35
- ? ( JSON . parse ( normalizedMap ) as M )
36
- : normalizedMap
26
+ if ( ! hasRefresh && ! onlyReactComp ) return undefined
37
27
38
28
let newCode = code
39
- if ( hasRefresh ) {
40
- const refreshHead = removeLineBreaksIfNeeded (
41
- `let prevRefreshReg;
42
- let prevRefreshSig;
29
+ newCode += `
43
30
31
+ import * as RefreshRuntime from "${ reactRefreshHost } ${ runtimePublicPath } ";
32
+ const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
44
33
if (import.meta.hot && !inWebWorker) {
45
34
if (!window.$RefreshReg$) {
46
35
throw new Error(
47
36
"${ pluginName } can't detect preamble. Something is wrong."
48
37
);
49
38
}
50
39
51
- prevRefreshReg = window.$RefreshReg$;
52
- prevRefreshSig = window.$RefreshSig$;
53
- window.$RefreshReg$ = RefreshRuntime.getRefreshReg(${ JSON . stringify ( id ) } );
54
- window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
55
- }
56
-
57
- ` ,
58
- avoidSourceMap ,
59
- )
60
-
61
- newCode = `${ refreshHead } ${ newCode }
62
-
63
- if (import.meta.hot && !inWebWorker) {
64
- window.$RefreshReg$ = prevRefreshReg;
65
- window.$RefreshSig$ = prevRefreshSig;
66
- }
67
- `
68
- if ( newMap ) {
69
- newMap . mappings = ';' . repeat ( 16 ) + newMap . mappings
70
- }
71
- }
72
-
73
- const sharedHead = removeLineBreaksIfNeeded (
74
- `import * as RefreshRuntime from "${ reactRefreshHost } ${ runtimePublicPath } ";
75
- const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
76
-
77
- ` ,
78
- avoidSourceMap ,
79
- )
80
-
81
- newCode = `${ sharedHead } ${ newCode }
82
-
83
- if (import.meta.hot && !inWebWorker) {
84
40
RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
85
41
RefreshRuntime.registerExportsForReactRefresh(${ JSON . stringify (
86
42
id ,
@@ -95,13 +51,12 @@ if (import.meta.hot && !inWebWorker) {
95
51
});
96
52
}
97
53
`
98
- if ( newMap ) {
99
- newMap . mappings = ';;;' + newMap . mappings
100
- }
101
54
102
- return { code : newCode , map : newMap }
103
- }
55
+ if ( hasRefresh ) {
56
+ newCode += `function $RefreshReg$(type, id) { return RefreshRuntime.register(type, ${ JSON . stringify ( id ) } + ' ' + id) }
57
+ function $RefreshSig$() { return RefreshRuntime.createSignatureFunctionForTransform(); }
58
+ `
59
+ }
104
60
105
- function removeLineBreaksIfNeeded ( code : string , enabled : boolean ) : string {
106
- return enabled ? code . replace ( / \n / g, '' ) : code
61
+ return newCode
107
62
}
0 commit comments