Skip to content

Commit 84c7fb8

Browse files
authored
refactor: migrate package runtime to ESM (#91)
1 parent 7f8c2ac commit 84c7fb8

File tree

18 files changed

+307
-321
lines changed

18 files changed

+307
-321
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ jobs:
6060

6161
- name: Dry run release to npm
6262
if: inputs.dry_run
63-
run: node scripts/release.mjs --dry-run --tag ${{ inputs.tag }}
63+
run: node scripts/release.js --dry-run --tag ${{ inputs.tag }}
6464

6565
- name: Release to npm
6666
if: ${{ !inputs.dry_run }}
67-
run: node scripts/release.mjs --tag ${{ inputs.tag }}
67+
run: node scripts/release.js --tag ${{ inputs.tag }}

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ import { ReactRefreshRspackPlugin } from '@rspack/plugin-react-refresh';
4343
const isDev = process.env.NODE_ENV === 'development';
4444

4545
export default {
46-
experiments: {
47-
rspackFuture: {
48-
disableTransformByDefault: true,
49-
},
50-
},
51-
// ...
5246
mode: isDev ? 'development' : 'production',
5347
module: {
5448
rules: [
@@ -74,7 +68,7 @@ export default {
7468
},
7569
],
7670
},
77-
plugins: [isDev && new ReactRefreshRspackPlugin()].filter(Boolean),
71+
plugins: [isDev && new ReactRefreshRspackPlugin()],
7872
};
7973
```
8074

client/reactRefresh.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
const RefreshUtils = require('./refreshUtils');
2-
const RefreshRuntime = require('react-refresh/runtime');
1+
import {
2+
createSignatureFunctionForTransform,
3+
register,
4+
} from 'react-refresh/runtime';
5+
import { executeRuntime, getModuleExports } from './refreshUtils.js';
36

47
function refresh(moduleId, webpackHot) {
5-
const currentExports = RefreshUtils.getModuleExports(moduleId);
8+
const currentExports = getModuleExports(moduleId);
69
const fn = (exports) => {
710
var testMode;
811
if (typeof __react_refresh_test__ !== 'undefined') {
912
testMode = __react_refresh_test__;
1013
}
11-
RefreshUtils.executeRuntime(exports, moduleId, webpackHot, testMode);
14+
executeRuntime(exports, moduleId, webpackHot, testMode);
1215
};
1316
if (typeof Promise !== 'undefined' && currentExports instanceof Promise) {
1417
currentExports.then(fn);
@@ -17,9 +20,4 @@ function refresh(moduleId, webpackHot) {
1720
}
1821
}
1922

20-
module.exports = {
21-
refresh,
22-
register: RefreshRuntime.register,
23-
createSignatureFunctionForTransform:
24-
RefreshRuntime.createSignatureFunctionForTransform,
25-
};
23+
export { createSignatureFunctionForTransform, refresh, register };

client/reactRefreshEntry.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var RefreshRuntime = require('react-refresh/runtime');
1+
import { injectIntoGlobalHook } from 'react-refresh/runtime';
2+
23
var safeThis = (function () {
34
// copied from core-js-pure/features/global-this
45
'use strict';
@@ -37,7 +38,7 @@ if (process.env.NODE_ENV !== 'production') {
3738

3839
// Only inject the runtime if it hasn't been injected
3940
if (!safeThis[$RefreshInjected$]) {
40-
RefreshRuntime.injectIntoGlobalHook(safeThis);
41+
injectIntoGlobalHook(safeThis);
4142

4243
// Empty implementation to avoid "ReferenceError: variable is not defined" in module which didn't pass builtin:react-refresh-loader
4344
safeThis.$RefreshSig$ = () => (type) => type;

client/refreshUtils.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
/* global __webpack_require__ */
2-
var Refresh = require('react-refresh/runtime');
2+
import {
3+
getFamilyByType,
4+
isLikelyComponentType,
5+
performReactRefresh,
6+
register,
7+
} from 'react-refresh/runtime';
38

49
/**
510
* Extracts exports from a webpack module object.
@@ -45,7 +50,7 @@ function getModuleExports(moduleId) {
4550
*/
4651
function getReactRefreshBoundarySignature(moduleExports) {
4752
var signature = [];
48-
signature.push(Refresh.getFamilyByType(moduleExports));
53+
signature.push(getFamilyByType(moduleExports));
4954

5055
if (moduleExports == null || typeof moduleExports !== 'object') {
5156
// Exit if we can't iterate over exports.
@@ -58,7 +63,7 @@ function getReactRefreshBoundarySignature(moduleExports) {
5863
}
5964

6065
signature.push(key);
61-
signature.push(Refresh.getFamilyByType(moduleExports[key]));
66+
signature.push(getFamilyByType(moduleExports[key]));
6267
}
6368

6469
return signature;
@@ -84,7 +89,7 @@ function createDebounceUpdate() {
8489
if (typeof refreshTimeout === 'undefined') {
8590
refreshTimeout = setTimeout(function () {
8691
refreshTimeout = undefined;
87-
Refresh.performReactRefresh();
92+
performReactRefresh();
8893
if (callback) {
8994
callback();
9095
}
@@ -103,7 +108,7 @@ function createDebounceUpdate() {
103108
* @returns {boolean} Whether the exports are React component like.
104109
*/
105110
function isReactRefreshBoundary(moduleExports) {
106-
if (Refresh.isLikelyComponentType(moduleExports)) {
111+
if (isLikelyComponentType(moduleExports)) {
107112
return true;
108113
}
109114
if (
@@ -130,7 +135,7 @@ function isReactRefreshBoundary(moduleExports) {
130135
// without any side-effects attached.
131136
// Ref: https://github.com/webpack/webpack/blob/b93048643fe74de2a6931755911da1212df55897/lib/MainTemplate.js#L281
132137
var exportValue = moduleExports[key];
133-
if (!Refresh.isLikelyComponentType(exportValue)) {
138+
if (!isLikelyComponentType(exportValue)) {
134139
areAllExportsComponents = false;
135140
}
136141
}
@@ -147,9 +152,9 @@ function isReactRefreshBoundary(moduleExports) {
147152
* @returns {void}
148153
*/
149154
function registerExportsForReactRefresh(moduleExports, moduleId) {
150-
if (Refresh.isLikelyComponentType(moduleExports)) {
155+
if (isLikelyComponentType(moduleExports)) {
151156
// Register module.exports if it is likely a component
152-
Refresh.register(moduleExports, moduleId + ' %exports%');
157+
register(moduleExports, moduleId + ' %exports%');
153158
}
154159

155160
if (
@@ -168,9 +173,9 @@ function registerExportsForReactRefresh(moduleExports, moduleId) {
168173
}
169174

170175
var exportValue = moduleExports[key];
171-
if (Refresh.isLikelyComponentType(exportValue)) {
176+
if (isLikelyComponentType(exportValue)) {
172177
var typeID = moduleId + ' %exports% ' + key;
173-
Refresh.register(exportValue, typeID);
178+
register(exportValue, typeID);
174179
}
175180
}
176181
}
@@ -273,11 +278,11 @@ function isUnrecoverableRuntimeError(error) {
273278
return error.message.startsWith('RuntimeError: factory is undefined');
274279
}
275280

276-
module.exports = Object.freeze({
277-
enqueueUpdate: enqueueUpdate,
278-
executeRuntime: executeRuntime,
279-
getModuleExports: getModuleExports,
280-
isReactRefreshBoundary: isReactRefreshBoundary,
281-
shouldInvalidateReactRefreshBoundary: shouldInvalidateReactRefreshBoundary,
282-
registerExportsForReactRefresh: registerExportsForReactRefresh,
283-
});
281+
export {
282+
enqueueUpdate,
283+
executeRuntime,
284+
getModuleExports,
285+
isReactRefreshBoundary,
286+
registerExportsForReactRefresh,
287+
shouldInvalidateReactRefreshBoundary,
288+
};

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
"repository": "https://github.com/rstackjs/rspack-plugin-react-refresh",
55
"license": "MIT",
66
"description": "React refresh plugin for Rspack",
7-
"type": "commonjs",
7+
"type": "module",
88
"types": "./dist/index.d.ts",
99
"exports": {
1010
".": {
1111
"types": "./dist/index.d.ts",
12-
"default": "./dist/index.mjs"
12+
"default": "./dist/index.js"
1313
},
1414
"./react-refresh": "./client/reactRefresh.js",
1515
"./react-refresh-entry": "./client/reactRefreshEntry.js",
@@ -22,7 +22,7 @@
2222
"lint:write": "biome check . --write",
2323
"prepare": "simple-git-hooks && npm run build",
2424
"test": "rstest",
25-
"release": "node ./scripts/release.mjs",
25+
"release": "node ./scripts/release.js",
2626
"bump": "npx bumpp --no-push --no-tag --no-commit"
2727
},
2828
"files": ["client", "dist"],
File renamed without changes.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path';
2-
import * as url from 'url';
2+
import { fileURLToPath } from 'url';
33
import cac from 'cac';
44
import { $ } from 'execa';
55
import fs from 'fs-extra';
@@ -16,7 +16,7 @@ cli.option('--tag <tag>', 'The npm tag to publish under (default: canary)', {
1616
default: 'canary',
1717
});
1818

19-
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
19+
const __dirname = fileURLToPath(new URL('.', import.meta.url));
2020
const PKG_PATH = path.resolve(__dirname, '../package.json');
2121
const pkg = fs.readJsonSync(PKG_PATH);
2222
const publishVersion = pkg.version;

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { Compiler } from '@rspack/core';
2-
import { normalizeOptions } from './options';
3-
import type { NormalizedPluginOptions, PluginOptions } from './options';
2+
import { normalizeOptions } from './options.js';
3+
import type { NormalizedPluginOptions, PluginOptions } from './options.js';
44
import {
55
getRefreshRuntimeDirPath,
66
getRefreshRuntimePaths,
77
reactRefreshEntryPath,
88
reactRefreshPath,
99
refreshUtilsPath,
10-
} from './paths';
10+
} from './paths.js';
1111

1212
export type { PluginOptions };
1313

src/options.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export type PluginOptions = {
5656
* @default true
5757
*/
5858
injectLoader?: boolean;
59-
6059
/**
6160
* Whether to inject the client/reactRefreshEntry.js
6261
* @default true

0 commit comments

Comments
 (0)