@@ -14,73 +14,29 @@ window.$RefreshSig$ = () => (type) => type;`
1414export const getPreambleCode = ( base : string ) : string =>
1515 preambleCode . replace ( '__BASE__' , base )
1616
17- export const avoidSourceMapOption = Symbol ( )
18-
19- export function addRefreshWrapper < M extends { mappings : string } > (
17+ export function addRefreshWrapper (
2018 code : string ,
21- map : M | string | typeof avoidSourceMapOption ,
2219 pluginName : string ,
2320 id : string ,
2421 reactRefreshHost = '' ,
25- ) : { code : string ; map : M | null | string } {
22+ ) : string | undefined {
2623 const hasRefresh = refreshContentRE . test ( code )
2724 const onlyReactComp = ! hasRefresh && reactCompRE . test ( code )
28- const normalizedMap = map === avoidSourceMapOption ? null : map
29-
30- if ( ! hasRefresh && ! onlyReactComp ) return { code, map : normalizedMap }
3125
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
3727
3828 let newCode = code
39- if ( hasRefresh ) {
40- const refreshHead = removeLineBreaksIfNeeded (
41- `let prevRefreshReg;
42- let prevRefreshSig;
29+ newCode += `
4330
31+ import * as RefreshRuntime from "${ reactRefreshHost } ${ runtimePublicPath } ";
32+ const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
4433if (import.meta.hot && !inWebWorker) {
4534 if (!window.$RefreshReg$) {
4635 throw new Error(
4736 "${ pluginName } can't detect preamble. Something is wrong."
4837 );
4938 }
5039
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) {
8440 RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
8541 RefreshRuntime.registerExportsForReactRefresh(${ JSON . stringify (
8642 id ,
@@ -95,13 +51,12 @@ if (import.meta.hot && !inWebWorker) {
9551 });
9652}
9753`
98- if ( newMap ) {
99- newMap . mappings = ';;;' + newMap . mappings
100- }
10154
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+ }
10460
105- function removeLineBreaksIfNeeded ( code : string , enabled : boolean ) : string {
106- return enabled ? code . replace ( / \n / g, '' ) : code
61+ return newCode
10762}
0 commit comments