Skip to content

Commit 4dc633d

Browse files
authored
fix: get module system scoped to context (#768)
1 parent 0ea5af1 commit 4dc633d

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

loader/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,18 @@ function ReactRefreshLoader(source, inputSourceMap, meta) {
5555
};
5656

5757
/**
58-
* @this {import('webpack').loader.LoaderContext}
58+
* @this {import('webpack').LoaderContext<import('./types').ReactRefreshLoaderOptions>}
5959
* @param {string} source
6060
* @param {import('source-map').RawSourceMap} [inputSourceMap]
6161
* @returns {Promise<[string, import('source-map').RawSourceMap]>}
6262
*/
6363
async function _loader(source, inputSourceMap) {
64-
const moduleSystem = await getModuleSystem.call(this, ModuleFilenameHelpers, options);
64+
/** @type {'esm' | 'cjs'} */
65+
let moduleSystem = 'cjs';
66+
// Only try to resolve the module system if the environment is known to support ES syntax
67+
if (this.environment != null) {
68+
moduleSystem = await getModuleSystem.call(this, ModuleFilenameHelpers, options);
69+
}
6570

6671
const RefreshSetupRuntime = RefreshSetupRuntimes[moduleSystem];
6772
const RefreshModuleRuntime = getRefreshModuleRuntime(Template, {

loader/utils/getModuleSystem.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const path = require('path');
33
const commonPathPrefix = require('common-path-prefix');
44
const findUp = require('find-up');
55

6-
/** @type {string | undefined} */
7-
let packageJsonType;
6+
/** @type {Map<string, string | undefined>} */
7+
let packageJsonTypeMap = new Map();
88

99
/**
1010
* Infers the current active module system from loader context and options.
@@ -45,6 +45,7 @@ async function getModuleSystem(ModuleFilenameHelpers, options) {
4545

4646
// Load users' `package.json` -
4747
// We will cache the results in a global variable so it will only be parsed once.
48+
let packageJsonType = packageJsonTypeMap.get(this.rootContext);
4849
if (!packageJsonType) {
4950
try {
5051
const commonPath = commonPathPrefix([this.rootContext, this.resourcePath], '/');
@@ -61,6 +62,7 @@ async function getModuleSystem(ModuleFilenameHelpers, options) {
6162
const buffer = await fsPromises.readFile(packageJsonPath, { encoding: 'utf-8' });
6263
const rawPackageJson = buffer.toString('utf-8');
6364
({ type: packageJsonType } = JSON.parse(rawPackageJson));
65+
packageJsonTypeMap.set(this.rootContext, packageJsonType);
6466
} catch (e) {
6567
// Failed to parse `package.json`, do nothing.
6668
}

test/loader/loader.test.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ describe('loader', () => {
337337
const { execution, parsed } = compilation.module;
338338

339339
expect(parsed).toMatchInlineSnapshot(`
340-
"__webpack_require__.$Refresh$.runtime = require('react-refresh');
340+
"import * as __react_refresh_runtime__ from 'react-refresh';
341+
__webpack_require__.$Refresh$.runtime = __react_refresh_runtime__;
341342
342343
export default 'Test';
343344
@@ -348,7 +349,7 @@ describe('loader', () => {
348349
);
349350
350351
function $ReactRefreshModuleRuntime$(exports) {
351-
if (module.hot) {
352+
if (import.meta.webpackHot) {
352353
var errorOverlay;
353354
if (typeof __react_refresh_error_overlay__ !== 'undefined') {
354355
errorOverlay = __react_refresh_error_overlay__;
@@ -360,7 +361,7 @@ describe('loader', () => {
360361
return __react_refresh_utils__.executeRuntime(
361362
exports,
362363
$ReactRefreshModuleId$,
363-
module.hot,
364+
import.meta.webpackHot,
364365
errorOverlay,
365366
testMode
366367
);
@@ -381,13 +382,15 @@ describe('loader', () => {
381382
/*!******************!*\\\\
382383
!*** ./index.js ***!
383384
\\\\******************/
384-
/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => {
385+
/***/ ((__webpack_module__, __webpack_exports__, __webpack_require__) => {
385386
386387
__webpack_require__.r(__webpack_exports__);
387388
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
388389
/* harmony export */ \\"default\\": () => (__WEBPACK_DEFAULT_EXPORT__)
389390
/* harmony export */ });
390-
__webpack_require__.$Refresh$.runtime = require('react-refresh');
391+
/* harmony import */ var react_refresh__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react-refresh */ \\"../../../../node_modules/react-refresh/runtime.js\\");
392+
393+
__webpack_require__.$Refresh$.runtime = react_refresh__WEBPACK_IMPORTED_MODULE_0__;
391394
392395
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = ('Test');
393396
@@ -398,7 +401,7 @@ describe('loader', () => {
398401
);
399402
400403
function $ReactRefreshModuleRuntime$(exports) {
401-
if (module.hot) {
404+
if (true) {
402405
var errorOverlay;
403406
if (typeof __react_refresh_error_overlay__ !== 'undefined') {
404407
errorOverlay = __react_refresh_error_overlay__;
@@ -410,7 +413,7 @@ describe('loader', () => {
410413
return __react_refresh_utils__.executeRuntime(
411414
exports,
412415
$ReactRefreshModuleId$,
413-
module.hot,
416+
__webpack_module__.hot,
414417
errorOverlay,
415418
testMode
416419
);
@@ -428,7 +431,8 @@ describe('loader', () => {
428431
},
429432
/******/ __webpack_require__ => { // webpackRuntimeModules
430433
/******/ var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
431-
/******/ var __webpack_exports__ = (__webpack_exec__(\\"./index.js\\"));
434+
/******/ __webpack_require__.O(0, [\\"defaultVendors\\"], () => (__webpack_exec__(\\"./index.js\\")));
435+
/******/ var __webpack_exports__ = __webpack_require__.O();
432436
/******/ }
433437
]);"
434438
`);

0 commit comments

Comments
 (0)