2021-09-21 19:17:16 +02:00
|
|
|
import type { LoaderDefinition } from 'webpack'
|
2020-04-18 19:55:10 +02:00
|
|
|
import RefreshModuleRuntime from './internal/ReactRefreshModule.runtime'
|
|
|
|
|
|
|
|
let refreshModuleRuntime = RefreshModuleRuntime.toString()
|
2022-02-15 17:24:11 +01:00
|
|
|
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'
|
2020-04-18 19:55:10 +02:00
|
|
|
)
|
|
|
|
|
2021-09-21 19:17:16 +02:00
|
|
|
const ReactRefreshLoader: LoaderDefinition = function ReactRefreshLoader(
|
2020-04-18 19:55:10 +02:00
|
|
|
source,
|
|
|
|
inputSourceMap
|
|
|
|
) {
|
2022-02-15 17:24:11 +01:00
|
|
|
this.callback(
|
|
|
|
null,
|
|
|
|
`${source}\n\n;${
|
|
|
|
// Account for commonjs not supporting `import.meta
|
|
|
|
this.resourcePath.endsWith('.cjs')
|
|
|
|
? commonJsrefreshModuleRuntime
|
|
|
|
: refreshModuleRuntime
|
|
|
|
}`,
|
|
|
|
inputSourceMap
|
|
|
|
)
|
2020-04-18 19:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ReactRefreshLoader
|