@jsenv/plugin-preact 0.0.9 → 0.0.12
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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/plugin-preact",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -8,8 +8,7 @@
|
|
|
8
8
|
"directory": "packages/jsenv-plugin-preact"
|
|
9
9
|
},
|
|
10
10
|
"publishConfig": {
|
|
11
|
-
"access": "public"
|
|
12
|
-
"registry": "https://registry.npmjs.org"
|
|
11
|
+
"access": "public"
|
|
13
12
|
},
|
|
14
13
|
"type": "module",
|
|
15
14
|
"exports": {
|
|
@@ -18,16 +17,28 @@
|
|
|
18
17
|
},
|
|
19
18
|
"./*": "./*"
|
|
20
19
|
},
|
|
20
|
+
"files": [
|
|
21
|
+
"/src/",
|
|
22
|
+
"/main.js"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"test": "node ./script/test/test.mjs"
|
|
26
|
+
},
|
|
21
27
|
"dependencies": {
|
|
22
28
|
"@babel/plugin-transform-react-jsx": "7.17.3",
|
|
23
|
-
"@jsenv/utils": "1.
|
|
29
|
+
"@jsenv/utils": "1.6.2",
|
|
24
30
|
"@jsenv/url-meta": "6.2.0"
|
|
25
31
|
},
|
|
26
32
|
"devDependencies": {
|
|
27
33
|
"@babel/plugin-transform-react-jsx-development": "7.16.7",
|
|
34
|
+
"@jsenv/assert": "2.5.4",
|
|
35
|
+
"@jsenv/core": "../../",
|
|
36
|
+
"@jsenv/filesystem": "3.1.0",
|
|
28
37
|
"@prefresh/babel-plugin": "0.4.3",
|
|
29
38
|
"@prefresh/core": "1.3.4",
|
|
30
39
|
"@prefresh/utils": "1.1.3",
|
|
31
|
-
"babel-plugin-transform-hook-names": "1.0.2"
|
|
40
|
+
"babel-plugin-transform-hook-names": "1.0.2",
|
|
41
|
+
"playwright": "1.20.2",
|
|
42
|
+
"preact": "10.7.1"
|
|
32
43
|
}
|
|
33
|
-
}
|
|
44
|
+
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import "@prefresh/core"
|
|
4
4
|
import { flush } from "@prefresh/utils"
|
|
5
5
|
|
|
6
|
-
export const
|
|
6
|
+
export const installPreactRefresh = (url) => {
|
|
7
7
|
let prevRefreshReg = self.$RefreshReg$ || (() => {})
|
|
8
8
|
let prevRefreshSig = self.$RefreshSig$ || (() => (type) => type)
|
|
9
9
|
self.$RefreshReg$ = (type, id) => {
|
|
@@ -16,7 +16,12 @@ import {
|
|
|
16
16
|
} from "@jsenv/utils/html_ast/html_ast.js"
|
|
17
17
|
|
|
18
18
|
export const jsenvPluginPreact = ({
|
|
19
|
-
|
|
19
|
+
jsxPatterns = {
|
|
20
|
+
"./**/*.jsx": true,
|
|
21
|
+
"./**/*.tsx": true,
|
|
22
|
+
"./**/node_modules/": false,
|
|
23
|
+
},
|
|
24
|
+
refreshPatterns = {
|
|
20
25
|
"./**/*.jsx": true,
|
|
21
26
|
"./**/*.tsx": true,
|
|
22
27
|
"./**/node_modules/": false,
|
|
@@ -25,13 +30,15 @@ export const jsenvPluginPreact = ({
|
|
|
25
30
|
} = {}) => {
|
|
26
31
|
const structuredMetaMap = normalizeStructuredMetaMap(
|
|
27
32
|
{
|
|
28
|
-
|
|
33
|
+
jsx: jsxPatterns,
|
|
34
|
+
refresh: refreshPatterns,
|
|
35
|
+
hookNames: {
|
|
36
|
+
"./**/*": true,
|
|
37
|
+
"./**/node_modules": false,
|
|
38
|
+
},
|
|
29
39
|
},
|
|
30
40
|
"file://",
|
|
31
41
|
)
|
|
32
|
-
const shouldEnablePrefresh = (url) => {
|
|
33
|
-
return urlToMeta({ url, structuredMetaMap }).hot
|
|
34
|
-
}
|
|
35
42
|
|
|
36
43
|
return {
|
|
37
44
|
name: "jsenv:preact",
|
|
@@ -77,91 +84,94 @@ import ${preactDevtoolsReference.generatedSpecifier}
|
|
|
77
84
|
return { content: htmlModified }
|
|
78
85
|
},
|
|
79
86
|
js_module: async (urlInfo, { scenario, referenceUtils }) => {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const hookNamesEnabled = scenario === "dev"
|
|
87
|
+
const urlMeta = urlToMeta({ url: urlInfo.url, structuredMetaMap })
|
|
88
|
+
const jsxEnabled = urlMeta.jsx
|
|
89
|
+
const refreshEnabled = scenario === "dev" ? urlMeta.refresh : false
|
|
90
|
+
const hookNamesEnabled =
|
|
91
|
+
scenario === "dev" &&
|
|
92
|
+
urlMeta.hookNames &&
|
|
93
|
+
(urlInfo.content.includes("useState") ||
|
|
94
|
+
urlInfo.content.includes("useReducer") ||
|
|
95
|
+
urlInfo.content.includes("useRef") ||
|
|
96
|
+
urlInfo.content.includes("useMemo"))
|
|
97
|
+
|
|
92
98
|
const { code, map } = await applyBabelPlugins({
|
|
93
99
|
babelPlugins: [
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
...(jsxEnabled
|
|
101
|
+
? [
|
|
102
|
+
[
|
|
103
|
+
scenario === "dev"
|
|
104
|
+
? "@babel/plugin-transform-react-jsx-development"
|
|
105
|
+
: "@babel/plugin-transform-react-jsx",
|
|
106
|
+
{
|
|
107
|
+
runtime: "automatic",
|
|
108
|
+
importSource: "preact",
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
]
|
|
112
|
+
: []),
|
|
103
113
|
...(hookNamesEnabled ? ["babel-plugin-transform-hook-names"] : []),
|
|
104
|
-
...(
|
|
114
|
+
...(refreshEnabled ? ["@prefresh/babel-plugin"] : []),
|
|
105
115
|
],
|
|
106
116
|
urlInfo,
|
|
107
117
|
})
|
|
108
118
|
const magicSource = createMagicSource(code)
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
]
|
|
129
|
-
for (const importSpecifier of injectedSpecifiers) {
|
|
130
|
-
const index = code.indexOf(importSpecifier)
|
|
131
|
-
if (index > -1) {
|
|
132
|
-
magicSource.replace({
|
|
133
|
-
start: index,
|
|
134
|
-
end: index + importSpecifier.length,
|
|
135
|
-
replacement: referenceUtils.inject({
|
|
119
|
+
if (jsxEnabled) {
|
|
120
|
+
// "@babel/plugin-transform-react-jsx" is injecting some of these 3 imports into the code:
|
|
121
|
+
// 1. import { jsx } from "preact/jsx-runtime"
|
|
122
|
+
// 2. import { jsxDev } from "preact/jsx-dev-runtime"
|
|
123
|
+
// 3. import { createElement } from "preact"
|
|
124
|
+
// see https://github.com/babel/babel/blob/410c9acf1b9212cac69d50b5bb2015b9f372acc4/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts#L743-L755
|
|
125
|
+
// "@babel/plugin-transform-react-jsx" cannot be configured to inject what we want
|
|
126
|
+
// ("/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js" instead of "preact/jsx-runtime")
|
|
127
|
+
// but that's fine we can still replace these imports afterwards as done below.
|
|
128
|
+
const injectedSpecifiers = [
|
|
129
|
+
`"preact"`,
|
|
130
|
+
`"preact/jsx-dev-runtime"`,
|
|
131
|
+
`"preact/jsx-runtime"`,
|
|
132
|
+
]
|
|
133
|
+
for (const importSpecifier of injectedSpecifiers) {
|
|
134
|
+
let index = code.indexOf(importSpecifier)
|
|
135
|
+
while (index > -1) {
|
|
136
|
+
const specifier = importSpecifier.slice(1, -1)
|
|
137
|
+
const [injectedReference] = referenceUtils.inject({
|
|
136
138
|
type: "js_import_export",
|
|
137
139
|
expectedType: "js_module",
|
|
138
|
-
specifier
|
|
139
|
-
})
|
|
140
|
-
|
|
140
|
+
specifier,
|
|
141
|
+
})
|
|
142
|
+
magicSource.replace({
|
|
143
|
+
start: index,
|
|
144
|
+
end: index + importSpecifier.length,
|
|
145
|
+
replacement: injectedReference.generatedSpecifier,
|
|
146
|
+
})
|
|
147
|
+
index = code.indexOf(importSpecifier, index + 1)
|
|
148
|
+
}
|
|
141
149
|
}
|
|
142
150
|
}
|
|
143
|
-
if (
|
|
151
|
+
if (refreshEnabled) {
|
|
144
152
|
const hasReg = /\$RefreshReg\$\(/.test(code)
|
|
145
153
|
const hasSig = /\$RefreshSig\$\(/.test(code)
|
|
146
154
|
if (hasReg || hasSig) {
|
|
147
|
-
const
|
|
155
|
+
const [preactRefreshClientReference] = referenceUtils.inject({
|
|
148
156
|
type: "js_import_export",
|
|
149
157
|
expectedType: "js_module",
|
|
150
|
-
specifier: "@jsenv/plugin-preact/src/client/
|
|
158
|
+
specifier: "@jsenv/plugin-preact/src/client/preact_refresh.js",
|
|
151
159
|
})
|
|
152
|
-
magicSource.prepend(`import {
|
|
153
|
-
|
|
160
|
+
magicSource.prepend(`import { installPreactRefresh } from ${
|
|
161
|
+
preactRefreshClientReference.generatedSpecifier
|
|
154
162
|
}
|
|
155
|
-
|
|
156
|
-
|
|
163
|
+
const __preact_refresh__ = installPreactRefresh(${JSON.stringify(urlInfo.url)})
|
|
164
|
+
`)
|
|
157
165
|
if (hasReg) {
|
|
158
166
|
magicSource.append(`
|
|
159
|
-
|
|
160
|
-
|
|
167
|
+
__preact_refresh__.end()
|
|
168
|
+
import.meta.hot.accept(__preact_refresh__.acceptCallback)`)
|
|
161
169
|
}
|
|
162
170
|
}
|
|
163
171
|
}
|
|
164
|
-
const result = magicSource.toContentAndSourcemap(
|
|
172
|
+
const result = magicSource.toContentAndSourcemap({
|
|
173
|
+
source: "jsenv:plugin_preact",
|
|
174
|
+
})
|
|
165
175
|
return {
|
|
166
176
|
content: result.content,
|
|
167
177
|
sourcemap: await composeTwoSourcemaps(map, result.sourcemap),
|