File size: 1,002 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { LoaderDefinition } from 'webpack'
import RefreshModuleRuntime from './internal/ReactRefreshModule.runtime'

let refreshModuleRuntime = RefreshModuleRuntime.toString()
refreshModuleRuntime = refreshModuleRuntime
  .slice(
    refreshModuleRuntime.indexOf('{') + 1,
    refreshModuleRuntime.lastIndexOf('}')
  )
  // Given that the import above executes the module we need to make sure it does not crash on `import.meta` not being allowed.
  .replace('global.importMeta', 'import.meta')

let commonJsrefreshModuleRuntime = refreshModuleRuntime.replace(
  'import.meta.webpackHot',
  'module.hot'
)

const ReactRefreshLoader: LoaderDefinition = function ReactRefreshLoader(
  source,
  inputSourceMap
) {
  this.callback(
    null,
    `${source}\n\n;${
      // Account for commonjs not supporting `import.meta
      this.resourcePath.endsWith('.cjs')
        ? commonJsrefreshModuleRuntime
        : refreshModuleRuntime
    }`,
    inputSourceMap
  )
}

export default ReactRefreshLoader