@jsenv/plugin-preact 1.2.25 → 1.2.27
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/package.json +4 -4
- package/src/client/preact_refresh.js +18 -18
- package/src/jsenv_plugin_preact.js +44 -46
- package/src/main.js +1 -1
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/plugin-preact",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.27",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
|
-
"url": "https://github.com/jsenv/
|
|
7
|
+
"url": "https://github.com/jsenv/core",
|
|
8
8
|
"directory": "packages/jsenv-plugin-preact"
|
|
9
9
|
},
|
|
10
10
|
"publishConfig": {
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"babel-plugin-transform-hook-names": "1.0.2",
|
|
28
|
-
"@babel/plugin-transform-react-jsx": "7.21.
|
|
28
|
+
"@babel/plugin-transform-react-jsx": "7.21.5",
|
|
29
29
|
"@babel/plugin-transform-react-jsx-development": "7.18.6",
|
|
30
|
-
"@jsenv/ast": "3.0.
|
|
30
|
+
"@jsenv/ast": "3.0.8",
|
|
31
31
|
"@jsenv/sourcemap": "1.0.10",
|
|
32
32
|
"@jsenv/url-meta": "8.1.0",
|
|
33
33
|
"@prefresh/babel-plugin": "0.4.4",
|
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import "@prefresh/core"
|
|
2
|
-
import { flush } from "@prefresh/utils"
|
|
1
|
+
import "@prefresh/core";
|
|
2
|
+
import { flush } from "@prefresh/utils";
|
|
3
3
|
|
|
4
4
|
export const installPreactRefresh = (url) => {
|
|
5
|
-
let prevRefreshReg = self.$RefreshReg$ || (() => {})
|
|
6
|
-
let prevRefreshSig = self.$RefreshSig$ || (() => (type) => type)
|
|
5
|
+
let prevRefreshReg = self.$RefreshReg$ || (() => {});
|
|
6
|
+
let prevRefreshSig = self.$RefreshSig$ || (() => (type) => type);
|
|
7
7
|
self.$RefreshReg$ = (type, id) => {
|
|
8
|
-
self.__PREFRESH__.register(type, `${url} ${id}`)
|
|
9
|
-
}
|
|
8
|
+
self.__PREFRESH__.register(type, `${url} ${id}`);
|
|
9
|
+
};
|
|
10
10
|
self.$RefreshSig$ = () => {
|
|
11
|
-
let status = "begin"
|
|
12
|
-
let savedType
|
|
11
|
+
let status = "begin";
|
|
12
|
+
let savedType;
|
|
13
13
|
return (type, key, forceReset, getCustomHooks) => {
|
|
14
|
-
if (!savedType) savedType = type
|
|
14
|
+
if (!savedType) savedType = type;
|
|
15
15
|
status = self.__PREFRESH__.sign(
|
|
16
16
|
type || savedType,
|
|
17
17
|
key,
|
|
18
18
|
forceReset,
|
|
19
19
|
getCustomHooks,
|
|
20
20
|
status,
|
|
21
|
-
)
|
|
22
|
-
return type
|
|
23
|
-
}
|
|
24
|
-
}
|
|
21
|
+
);
|
|
22
|
+
return type;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
25
|
return {
|
|
26
26
|
end: () => {
|
|
27
|
-
self.$RefreshReg$ = prevRefreshReg
|
|
28
|
-
self.$RefreshSig$ = prevRefreshSig
|
|
27
|
+
self.$RefreshReg$ = prevRefreshReg;
|
|
28
|
+
self.$RefreshSig$ = prevRefreshSig;
|
|
29
29
|
},
|
|
30
30
|
acceptCallback: () => {
|
|
31
|
-
flush()
|
|
31
|
+
flush();
|
|
32
32
|
},
|
|
33
|
-
}
|
|
34
|
-
}
|
|
33
|
+
};
|
|
34
|
+
};
|
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
* - https://github.com/preactjs/prefresh/blob/main/packages/vite/src/index.js
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { URL_META } from "@jsenv/url-meta"
|
|
7
|
-
import { createMagicSource, composeTwoSourcemaps } from "@jsenv/sourcemap"
|
|
6
|
+
import { URL_META } from "@jsenv/url-meta";
|
|
7
|
+
import { createMagicSource, composeTwoSourcemaps } from "@jsenv/sourcemap";
|
|
8
8
|
import {
|
|
9
9
|
applyBabelPlugins,
|
|
10
10
|
parseHtmlString,
|
|
11
11
|
stringifyHtmlAst,
|
|
12
|
-
|
|
12
|
+
injectHtmlNodeAsEarlyAsPossible,
|
|
13
13
|
createHtmlNode,
|
|
14
|
-
} from "@jsenv/ast"
|
|
14
|
+
} from "@jsenv/ast";
|
|
15
15
|
|
|
16
16
|
export const jsenvPluginPreact = ({
|
|
17
17
|
jsxTranspilation = true,
|
|
@@ -23,22 +23,22 @@ export const jsenvPluginPreact = ({
|
|
|
23
23
|
jsxTranspilation = {
|
|
24
24
|
"./**/*.jsx": true,
|
|
25
25
|
"./**/*.tsx": true,
|
|
26
|
-
}
|
|
26
|
+
};
|
|
27
27
|
} else if (jsxTranspilation === false) {
|
|
28
|
-
jsxTranspilation = {}
|
|
28
|
+
jsxTranspilation = {};
|
|
29
29
|
}
|
|
30
30
|
if (refreshInstrumentation === true) {
|
|
31
31
|
refreshInstrumentation = {
|
|
32
32
|
"./**/*.jsx": true,
|
|
33
33
|
"./**/*.tsx": true,
|
|
34
|
-
}
|
|
34
|
+
};
|
|
35
35
|
} else if (refreshInstrumentation === false) {
|
|
36
|
-
refreshInstrumentation = {}
|
|
36
|
+
refreshInstrumentation = {};
|
|
37
37
|
}
|
|
38
38
|
if (hookNamesInstrumentation === true) {
|
|
39
|
-
hookNamesInstrumentation = { "./**": true }
|
|
39
|
+
hookNamesInstrumentation = { "./**": true };
|
|
40
40
|
} else {
|
|
41
|
-
hookNamesInstrumentation = {}
|
|
41
|
+
hookNamesInstrumentation = {};
|
|
42
42
|
}
|
|
43
43
|
const associations = URL_META.resolveAssociations(
|
|
44
44
|
{
|
|
@@ -47,7 +47,7 @@ export const jsenvPluginPreact = ({
|
|
|
47
47
|
hookNamesInstrumentation,
|
|
48
48
|
},
|
|
49
49
|
"file://",
|
|
50
|
-
)
|
|
50
|
+
);
|
|
51
51
|
|
|
52
52
|
return {
|
|
53
53
|
name: "jsenv:preact",
|
|
@@ -58,57 +58,55 @@ export const jsenvPluginPreact = ({
|
|
|
58
58
|
reference.specifier === "react" ||
|
|
59
59
|
reference.specifier === "react-dom"
|
|
60
60
|
) {
|
|
61
|
-
reference.specifier = "preact/compat"
|
|
62
|
-
return context.resolveReference(reference)[0].url
|
|
61
|
+
reference.specifier = "preact/compat";
|
|
62
|
+
return context.resolveReference(reference)[0].url;
|
|
63
63
|
}
|
|
64
|
-
return null
|
|
64
|
+
return null;
|
|
65
65
|
},
|
|
66
66
|
},
|
|
67
67
|
transformUrlContent: {
|
|
68
68
|
html: (urlInfo, context) => {
|
|
69
69
|
if (!preactDevtools) {
|
|
70
|
-
return null
|
|
70
|
+
return null;
|
|
71
71
|
}
|
|
72
72
|
if (context.build && preactDevtools !== "dev_and_build") {
|
|
73
|
-
return null
|
|
73
|
+
return null;
|
|
74
74
|
}
|
|
75
|
-
const htmlAst = parseHtmlString(urlInfo.content)
|
|
75
|
+
const htmlAst = parseHtmlString(urlInfo.content);
|
|
76
76
|
const [preactDevtoolsReference] = context.referenceUtils.inject({
|
|
77
77
|
type: "js_import",
|
|
78
78
|
expectedType: "js_module",
|
|
79
79
|
specifier: context.dev ? "preact/debug" : "preact/devtools",
|
|
80
|
-
})
|
|
81
|
-
|
|
80
|
+
});
|
|
81
|
+
injectHtmlNodeAsEarlyAsPossible(
|
|
82
82
|
htmlAst,
|
|
83
83
|
createHtmlNode({
|
|
84
84
|
tagName: "script",
|
|
85
85
|
type: "module",
|
|
86
|
-
textContent: `
|
|
87
|
-
import ${preactDevtoolsReference.generatedSpecifier}
|
|
88
|
-
`,
|
|
86
|
+
textContent: `import ${preactDevtoolsReference.generatedSpecifier}`,
|
|
89
87
|
}),
|
|
90
88
|
"jsenv:preact",
|
|
91
|
-
)
|
|
92
|
-
const htmlModified = stringifyHtmlAst(htmlAst)
|
|
93
|
-
return { content: htmlModified }
|
|
89
|
+
);
|
|
90
|
+
const htmlModified = stringifyHtmlAst(htmlAst);
|
|
91
|
+
return { content: htmlModified };
|
|
94
92
|
},
|
|
95
93
|
js_module: async (urlInfo, context) => {
|
|
96
94
|
const urlMeta = URL_META.applyAssociations({
|
|
97
95
|
url: urlInfo.url,
|
|
98
96
|
associations,
|
|
99
|
-
})
|
|
100
|
-
const jsxEnabled = urlMeta.jsxTranspilation
|
|
97
|
+
});
|
|
98
|
+
const jsxEnabled = urlMeta.jsxTranspilation;
|
|
101
99
|
const refreshEnabled = context.dev
|
|
102
100
|
? urlMeta.refreshInstrumentation &&
|
|
103
101
|
!urlInfo.content.includes("import.meta.hot.decline()")
|
|
104
|
-
: false
|
|
102
|
+
: false;
|
|
105
103
|
const hookNamesEnabled =
|
|
106
104
|
context.dev &&
|
|
107
105
|
urlMeta.hookNamesInstrumentation &&
|
|
108
106
|
(urlInfo.content.includes("useState") ||
|
|
109
107
|
urlInfo.content.includes("useReducer") ||
|
|
110
108
|
urlInfo.content.includes("useRef") ||
|
|
111
|
-
urlInfo.content.includes("useMemo"))
|
|
109
|
+
urlInfo.content.includes("useMemo"));
|
|
112
110
|
const { code, map } = await applyBabelPlugins({
|
|
113
111
|
babelPlugins: [
|
|
114
112
|
...(jsxEnabled
|
|
@@ -128,8 +126,8 @@ import ${preactDevtoolsReference.generatedSpecifier}
|
|
|
128
126
|
...(refreshEnabled ? ["@prefresh/babel-plugin"] : []),
|
|
129
127
|
],
|
|
130
128
|
urlInfo,
|
|
131
|
-
})
|
|
132
|
-
const magicSource = createMagicSource(code)
|
|
129
|
+
});
|
|
130
|
+
const magicSource = createMagicSource(code);
|
|
133
131
|
if (jsxEnabled) {
|
|
134
132
|
// "@babel/plugin-transform-react-jsx" is injecting some of these 3 imports into the code:
|
|
135
133
|
// 1. import { jsx } from "preact/jsx-runtime"
|
|
@@ -143,50 +141,50 @@ import ${preactDevtoolsReference.generatedSpecifier}
|
|
|
143
141
|
`"preact"`,
|
|
144
142
|
`"preact/jsx-dev-runtime"`,
|
|
145
143
|
`"preact/jsx-runtime"`,
|
|
146
|
-
]
|
|
144
|
+
];
|
|
147
145
|
for (const importSpecifier of injectedSpecifiers) {
|
|
148
|
-
let index = code.indexOf(importSpecifier)
|
|
146
|
+
let index = code.indexOf(importSpecifier);
|
|
149
147
|
while (index > -1) {
|
|
150
|
-
const specifier = importSpecifier.slice(1, -1)
|
|
148
|
+
const specifier = importSpecifier.slice(1, -1);
|
|
151
149
|
const [injectedReference] = context.referenceUtils.inject({
|
|
152
150
|
type: "js_import",
|
|
153
151
|
expectedType: "js_module",
|
|
154
152
|
specifier,
|
|
155
|
-
})
|
|
153
|
+
});
|
|
156
154
|
magicSource.replace({
|
|
157
155
|
start: index,
|
|
158
156
|
end: index + importSpecifier.length,
|
|
159
157
|
replacement: injectedReference.generatedSpecifier,
|
|
160
|
-
})
|
|
161
|
-
index = code.indexOf(importSpecifier, index + 1)
|
|
158
|
+
});
|
|
159
|
+
index = code.indexOf(importSpecifier, index + 1);
|
|
162
160
|
}
|
|
163
161
|
}
|
|
164
162
|
}
|
|
165
163
|
if (refreshEnabled) {
|
|
166
|
-
const hasReg = /\$RefreshReg\$\(/.test(code)
|
|
167
|
-
const hasSig = /\$RefreshSig\$\(/.test(code)
|
|
164
|
+
const hasReg = /\$RefreshReg\$\(/.test(code);
|
|
165
|
+
const hasSig = /\$RefreshSig\$\(/.test(code);
|
|
168
166
|
if (hasReg || hasSig) {
|
|
169
167
|
const [preactRefreshClientReference] =
|
|
170
168
|
context.referenceUtils.inject({
|
|
171
169
|
type: "js_import",
|
|
172
170
|
expectedType: "js_module",
|
|
173
171
|
specifier: "@jsenv/plugin-preact/src/client/preact_refresh.js",
|
|
174
|
-
})
|
|
172
|
+
});
|
|
175
173
|
magicSource.prepend(`import { installPreactRefresh } from ${
|
|
176
174
|
preactRefreshClientReference.generatedSpecifier
|
|
177
175
|
}
|
|
178
176
|
const __preact_refresh__ = installPreactRefresh(${JSON.stringify(urlInfo.url)})
|
|
179
|
-
`)
|
|
177
|
+
`);
|
|
180
178
|
if (hasReg) {
|
|
181
179
|
magicSource.append(`
|
|
182
180
|
__preact_refresh__.end()
|
|
183
|
-
import.meta.hot.accept(__preact_refresh__.acceptCallback)`)
|
|
181
|
+
import.meta.hot.accept(__preact_refresh__.acceptCallback)`);
|
|
184
182
|
}
|
|
185
183
|
}
|
|
186
184
|
}
|
|
187
185
|
const result = magicSource.toContentAndSourcemap({
|
|
188
186
|
source: "jsenv_preact",
|
|
189
|
-
})
|
|
187
|
+
});
|
|
190
188
|
return {
|
|
191
189
|
content: result.content,
|
|
192
190
|
sourcemap: await composeTwoSourcemaps(map, result.sourcemap),
|
|
@@ -194,8 +192,8 @@ import.meta.hot.accept(__preact_refresh__.acceptCallback)`)
|
|
|
194
192
|
// I don't know exactly what is resulting in bad sourcemaps
|
|
195
193
|
// but I suspect hooknames or prefresh to be responsible
|
|
196
194
|
sourcemapIsWrong: jsxEnabled && refreshEnabled,
|
|
197
|
-
}
|
|
195
|
+
};
|
|
198
196
|
},
|
|
199
197
|
},
|
|
200
|
-
}
|
|
201
|
-
}
|
|
198
|
+
};
|
|
199
|
+
};
|
package/src/main.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { jsenvPluginPreact } from "./jsenv_plugin_preact.js"
|
|
1
|
+
export { jsenvPluginPreact } from "./jsenv_plugin_preact.js";
|