@peachy/plugin-react 0.0.8 → 0.0.10

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Plugin } from "rolldown";
2
2
 
3
3
  //#region src/index.d.ts
4
- declare function react(port: number): Plugin[];
4
+ declare function reactHMRPlugin(port: number): Plugin[];
5
5
  //#endregion
6
- export { react };
6
+ export { reactHMRPlugin };
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(id) {
32
- if (id.startsWith(PREFIX)) return id;
33
- return null;
31
+ resolveId: {
32
+ filter: { id: prefixRegex(PREFIX) },
33
+ handler(id) {
34
+ return id;
35
+ }
34
36
  },
35
- load(id) {
36
- if (id.startsWith(PREFIX)) {
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(id) {
52
- if (id === "\0virtual:hmr-runtime") return runtime;
53
+ resolveId: {
54
+ filter: { id: exactRegex("\0virtual:hmr-runtime") },
55
+ handler() {
56
+ return runtime;
57
+ }
53
58
  },
54
- async transform(code, id) {
55
- if (id === runtime) return { code: code.replace("$port$", port.toString()) };
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 template = readFileSync(resolve(import.meta.dirname, "./templates/hmr-wrapper.ts"), "utf-8");
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(code, id) {
67
- if (!id.startsWith(process.cwd()) || !id.match(/\.[tj]sx?$/) || id.includes("node_modules") || id.startsWith("gi://*")) return null;
68
- return {
69
- code: template.replace("$sourceCode$", code).replaceAll("$hmrId$", JSON.stringify(id)),
70
- map: null
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,
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 react(port) {
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 { react };
124
+ export { reactHMRPlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peachy/plugin-react",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "HMR for react",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",
@@ -8,14 +8,14 @@
8
8
  "author": "Angelo Verlain <hey@vixalien.com>",
9
9
  "license": "MIT",
10
10
  "devDependencies": {
11
- "rolldown": "1.0.0-beta.60",
12
- "tsdown": "0.20.0-beta.4",
11
+ "@types/node": "^25.2.2",
12
+ "rolldown": "1.0.0-rc.3",
13
+ "tsdown": "0.20.3",
13
14
  "typescript": "^5.9.3"
14
15
  },
15
16
  "dependencies": {
16
- "@babel/plugin-syntax-jsx": "^7.28.6",
17
- "@babel/plugin-syntax-typescript": "^7.28.6",
18
- "@rollup/plugin-babel": "^6.1.0",
17
+ "@ampproject/remapping": "^2.3.0",
18
+ "magic-string": "^0.30.21",
19
19
  "react-refresh": "^0.18.0"
20
20
  },
21
21
  "peerDependencies": {