@peachy/plugin-react 0.0.9 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +60 -41
- package/dist/templates/hmr-runtime.ts +3 -8
- package/dist/templates/hmr-wrapper.ts +3 -5
- package/package.json +16 -15
package/dist/index.d.mts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { babel } from "@rollup/plugin-babel";
|
|
2
|
-
import ReactFreshBabelPlugin from "react-refresh/babel";
|
|
3
|
-
import BabelJSX from "@babel/plugin-syntax-jsx";
|
|
4
|
-
import BabelTS from "@babel/plugin-syntax-typescript";
|
|
5
1
|
import { resolve } from "path";
|
|
2
|
+
import { exactRegex, prefixRegex } from "rolldown/filter";
|
|
6
3
|
import { readFileSync } from "fs";
|
|
4
|
+
import remapping from "@ampproject/remapping";
|
|
5
|
+
import MagicString from "magic-string";
|
|
6
|
+
import { transform } from "rolldown/experimental";
|
|
7
7
|
|
|
8
8
|
//#region src/entrypoint.ts
|
|
9
9
|
function addHmrEntrypoint(input) {
|
|
@@ -28,16 +28,18 @@ function createHmrEntrypointPlugin() {
|
|
|
28
28
|
input: addHmrEntrypoint(opts.input)
|
|
29
29
|
};
|
|
30
30
|
},
|
|
31
|
-
resolveId
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
resolveId: {
|
|
32
|
+
filter: { id: prefixRegex(PREFIX) },
|
|
33
|
+
handler(id) {
|
|
34
|
+
return id;
|
|
35
|
+
}
|
|
34
36
|
},
|
|
35
|
-
load
|
|
36
|
-
|
|
37
|
+
load: {
|
|
38
|
+
filter: { id: prefixRegex(PREFIX) },
|
|
39
|
+
handler(id) {
|
|
37
40
|
const actualEntry = id.slice(21);
|
|
38
41
|
return `import '${resolvedPath}';\nimport '${resolve(process.cwd(), actualEntry)}';`;
|
|
39
42
|
}
|
|
40
|
-
return null;
|
|
41
43
|
}
|
|
42
44
|
};
|
|
43
45
|
}
|
|
@@ -48,11 +50,17 @@ function hmrRuntimePlugin(port) {
|
|
|
48
50
|
const runtime = resolve(import.meta.dirname, "./templates/hmr-runtime.ts");
|
|
49
51
|
return {
|
|
50
52
|
name: "hmr-runtime-plugin",
|
|
51
|
-
resolveId
|
|
52
|
-
|
|
53
|
+
resolveId: {
|
|
54
|
+
filter: { id: exactRegex("\0virtual:hmr-runtime") },
|
|
55
|
+
handler() {
|
|
56
|
+
return runtime;
|
|
57
|
+
}
|
|
53
58
|
},
|
|
54
|
-
|
|
55
|
-
|
|
59
|
+
transform: {
|
|
60
|
+
filter: { id: exactRegex(runtime) },
|
|
61
|
+
handler(code) {
|
|
62
|
+
return { code: code.replace("$port$", port.toString()) };
|
|
63
|
+
}
|
|
56
64
|
}
|
|
57
65
|
};
|
|
58
66
|
}
|
|
@@ -60,41 +68,52 @@ function hmrRuntimePlugin(port) {
|
|
|
60
68
|
//#endregion
|
|
61
69
|
//#region src/wrapper.ts
|
|
62
70
|
function reactRefreshWrapperPlugin() {
|
|
63
|
-
const
|
|
71
|
+
const [prefix, suffix] = readFileSync(resolve(import.meta.dirname, "./templates/hmr-wrapper.ts"), "utf-8").split("$sourceCode$");
|
|
64
72
|
return {
|
|
65
73
|
name: "react-refresh-wrapper",
|
|
66
|
-
transform
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
74
|
+
transform: {
|
|
75
|
+
filter: {
|
|
76
|
+
id: {
|
|
77
|
+
include: [prefixRegex(process.cwd())],
|
|
78
|
+
exclude: [/node_modules/, /^gi:/]
|
|
79
|
+
},
|
|
80
|
+
moduleType: ["jsx", "tsx"]
|
|
81
|
+
},
|
|
82
|
+
async handler(code, id) {
|
|
83
|
+
const { code: transformed, map, errors } = await transform(id, code, {
|
|
84
|
+
jsx: {
|
|
85
|
+
refresh: true,
|
|
86
|
+
importSource: "@peachy/react"
|
|
87
|
+
},
|
|
88
|
+
sourcemap: true
|
|
89
|
+
});
|
|
90
|
+
for (const error of errors) this.error({
|
|
91
|
+
frame: error.codeframe ?? void 0,
|
|
92
|
+
message: error.message,
|
|
93
|
+
cause: error.helpMessage,
|
|
94
|
+
stack: ""
|
|
95
|
+
});
|
|
96
|
+
const hmrId = JSON.stringify(id);
|
|
97
|
+
const s = new MagicString(transformed);
|
|
98
|
+
s.prepend(prefix.replaceAll("$hmrId$", hmrId));
|
|
99
|
+
s.append(suffix.replaceAll("$hmrId$", hmrId));
|
|
100
|
+
const combinedMap = remapping([s.generateMap({
|
|
101
|
+
source: id,
|
|
102
|
+
hires: true
|
|
103
|
+
}), map], () => null);
|
|
104
|
+
return {
|
|
105
|
+
code: s.toString(),
|
|
106
|
+
map: combinedMap.toString()
|
|
107
|
+
};
|
|
108
|
+
}
|
|
72
109
|
}
|
|
73
110
|
};
|
|
74
111
|
}
|
|
75
112
|
|
|
76
113
|
//#endregion
|
|
77
114
|
//#region src/index.ts
|
|
78
|
-
function
|
|
115
|
+
function reactHMRPlugin(port) {
|
|
79
116
|
return [
|
|
80
|
-
babel({
|
|
81
|
-
plugins: [
|
|
82
|
-
BabelJSX,
|
|
83
|
-
[BabelTS, { isTSX: true }],
|
|
84
|
-
ReactFreshBabelPlugin
|
|
85
|
-
],
|
|
86
|
-
babelHelpers: "bundled",
|
|
87
|
-
ignore: [/node_modules/],
|
|
88
|
-
extensions: [
|
|
89
|
-
".js",
|
|
90
|
-
".jsx",
|
|
91
|
-
".es6",
|
|
92
|
-
".es",
|
|
93
|
-
".mjs",
|
|
94
|
-
".ts",
|
|
95
|
-
".tsx"
|
|
96
|
-
]
|
|
97
|
-
}),
|
|
98
117
|
createHmrEntrypointPlugin(),
|
|
99
118
|
reactRefreshWrapperPlugin(),
|
|
100
119
|
hmrRuntimePlugin(port)
|
|
@@ -102,4 +121,4 @@ function react(port) {
|
|
|
102
121
|
}
|
|
103
122
|
|
|
104
123
|
//#endregion
|
|
105
|
-
export {
|
|
124
|
+
export { reactHMRPlugin };
|
|
@@ -43,9 +43,7 @@ if (!globalThis.__hmr__) {
|
|
|
43
43
|
|
|
44
44
|
connection.connect("message", async (_, _type, event) => {
|
|
45
45
|
const decoder = new TextDecoder();
|
|
46
|
-
const payload = JSON.parse(
|
|
47
|
-
decoder.decode(event.toArray()),
|
|
48
|
-
) as HMRMessage;
|
|
46
|
+
const payload = JSON.parse(decoder.decode(event.toArray())) as HMRMessage;
|
|
49
47
|
|
|
50
48
|
switch (payload?.type) {
|
|
51
49
|
case "reload":
|
|
@@ -60,12 +58,9 @@ if (!globalThis.__hmr__) {
|
|
|
60
58
|
for (const update of payload.updates) {
|
|
61
59
|
if (globalThis.__hmr__.contexts[update.id]) {
|
|
62
60
|
try {
|
|
63
|
-
const module = await import(
|
|
64
|
-
"file://" + update.url + "?t=" + Date.now()
|
|
65
|
-
);
|
|
61
|
+
const module = await import("file://" + update.url + "?t=" + Date.now());
|
|
66
62
|
|
|
67
|
-
const accepted =
|
|
68
|
-
globalThis.__hmr__.contexts[update.id].emit(module);
|
|
63
|
+
const accepted = globalThis.__hmr__.contexts[update.id].emit(module);
|
|
69
64
|
|
|
70
65
|
if (accepted) {
|
|
71
66
|
console.info("[HMR] Update accepted by", update.id);
|
|
@@ -7,9 +7,7 @@ if (import.meta) {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
var NO_FAST_REFRESH =
|
|
10
|
-
!globalThis.$RefreshReg$ ||
|
|
11
|
-
!globalThis.$RefreshSig$ ||
|
|
12
|
-
!globalThis.$RefreshRuntime$;
|
|
10
|
+
!globalThis.$RefreshReg$ || !globalThis.$RefreshSig$ || !globalThis.$RefreshRuntime$;
|
|
13
11
|
if (NO_FAST_REFRESH) {
|
|
14
12
|
console.warn(
|
|
15
13
|
"[HMR]: React Refresh is not enabled. Please ensure that you have installed the necessary dependencies and enabled the plugin in your webpack configuration.",
|
|
@@ -21,10 +19,10 @@ if (NO_FAST_REFRESH) {
|
|
|
21
19
|
const fullId = $hmrId$ + "#" + id;
|
|
22
20
|
globalThis.$RefreshRuntime$.register(type, fullId);
|
|
23
21
|
};
|
|
24
|
-
globalThis.$RefreshSig$ =
|
|
25
|
-
globalThis.$RefreshRuntime$.createSignatureFunctionForTransform;
|
|
22
|
+
globalThis.$RefreshSig$ = globalThis.$RefreshRuntime$.createSignatureFunctionForTransform;
|
|
26
23
|
}
|
|
27
24
|
|
|
25
|
+
/* oxlint-disable no-unused-expressions */
|
|
28
26
|
$sourceCode$;
|
|
29
27
|
|
|
30
28
|
if (!NO_FAST_REFRESH) {
|
package/package.json
CHANGED
|
@@ -1,29 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peachy/plugin-react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"description": "HMR for react",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "./dist/index.mjs",
|
|
7
5
|
"keywords": [],
|
|
8
|
-
"author": "Angelo Verlain <hey@vixalien.com>",
|
|
9
6
|
"license": "MIT",
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
"author": "Angelo Verlain <hey@vixalien.com>",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.mjs",
|
|
15
13
|
"dependencies": {
|
|
16
|
-
"@
|
|
17
|
-
"
|
|
18
|
-
"@rollup/plugin-babel": "^6.1.0",
|
|
14
|
+
"@ampproject/remapping": "^2.3.0",
|
|
15
|
+
"magic-string": "^0.30.21",
|
|
19
16
|
"react-refresh": "^0.18.0"
|
|
20
17
|
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^25.2.2",
|
|
20
|
+
"rolldown": "1.0.0-rc.3",
|
|
21
|
+
"tsdown": "0.20.3",
|
|
22
|
+
"typescript": "^5.9.3",
|
|
23
|
+
"@peachy/internal-utilities": "0.0.0"
|
|
24
|
+
},
|
|
21
25
|
"peerDependencies": {
|
|
22
26
|
"rolldown": "1.0.0-beta.58"
|
|
23
27
|
},
|
|
24
|
-
"files": [
|
|
25
|
-
"dist"
|
|
26
|
-
],
|
|
27
28
|
"scripts": {
|
|
28
29
|
"build": "tsdown src/index.ts --dts && mkdir -p dist/templates && cp src/templates/* dist/templates"
|
|
29
30
|
},
|