@unpackjs/plugin-react 3.3.4 → 4.0.0
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.js +39 -72
- package/dist/injectScript.js +5 -5
- package/dist-types/index.d.ts +1 -5
- package/dist-types/index.d.ts.map +1 -1
- package/dist-types/mpa.d.ts.map +1 -1
- package/package.json +5 -5
- package/dist-types/click-to-component/index.d.ts +0 -7
- package/dist-types/click-to-component/index.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,62 +1,17 @@
|
|
|
1
|
-
import __rslib_shim_module__ from
|
|
1
|
+
import __rslib_shim_module__ from "node:module";
|
|
2
2
|
let require = __rslib_shim_module__.createRequire(import.meta.url);
|
|
3
3
|
import { fileURLToPath as __rspack_fileURLToPath } from "node:url";
|
|
4
4
|
import node_path, { dirname as __rspack_dirname } from "node:path";
|
|
5
|
-
import plugin_react_refresh from "@rspack/plugin-react-refresh";
|
|
6
|
-
import { TEMPLATE_CONTENT, addRestartCleaner, esVersionToBrowserslist, getPathInJs, getPort, isDev, isDevServer, isFunction, isPlainObject, launchEditor, mergeConfig as core_mergeConfig, removeDir, rspack } from "@unpackjs/core";
|
|
7
5
|
import node_fs from "node:fs";
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
let params = new URLSearchParams(req.url.slice(1)), file = decodeURIComponent(params.get('file')), line = Number(params.get('line')), column = Number(params.get('column'));
|
|
14
|
-
res.writeHead(200, {
|
|
15
|
-
'Access-Control-Allow-Origin': '*',
|
|
16
|
-
'Access-Control-Allow-Methods': '*',
|
|
17
|
-
'Access-Control-Allow-Headers': '*',
|
|
18
|
-
'Access-Control-Allow-Private-Network': 'true'
|
|
19
|
-
}), res.end('ok'), launchEditor(`${file}:${line}:${column}`);
|
|
20
|
-
});
|
|
21
|
-
getPort().then((port)=>{
|
|
22
|
-
server.listen(port, ()=>{
|
|
23
|
-
resolve({
|
|
24
|
-
port
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
}), addRestartCleaner(()=>new Promise((resolve)=>server.close(()=>resolve())));
|
|
28
|
-
}), injectScript = node_fs.readFileSync(node_path.resolve(click_to_component_dirname, './injectScript.js'), 'utf-8'), scriptTpl = `
|
|
29
|
-
if (!document.querySelector('#__click-to-component__')) {
|
|
30
|
-
const script = document.createElement('script')
|
|
31
|
-
window['CLICK_TO_COMPONENT'] = { port: ${port} }
|
|
32
|
-
script.setAttribute('type', 'text/javascript')
|
|
33
|
-
script.textContent = \`${injectScript}\`
|
|
34
|
-
document.body.appendChild(script)
|
|
35
|
-
}`;
|
|
36
|
-
if (unpackConfig.mpa) Object.values(config.entry).forEach((entryConfig)=>{
|
|
37
|
-
let filePath = entryConfig.import[0], content = node_fs.readFileSync(filePath, 'utf-8');
|
|
38
|
-
node_fs.writeFileSync(filePath, `${scriptTpl}\n${content}`, 'utf-8');
|
|
39
|
-
});
|
|
40
|
-
else {
|
|
41
|
-
let realEntryPath = node_path.resolve(config.context, cachePath, 'entries', `index.${port}.jsx`), tpl = `
|
|
42
|
-
${scriptTpl}
|
|
43
|
-
import '${getPathInJs(node_path.resolve(Object.values(config.entry)[0]))}'
|
|
44
|
-
`, dir = node_path.dirname(realEntryPath);
|
|
45
|
-
node_fs.existsSync(dir) || node_fs.mkdirSync(dir, {
|
|
46
|
-
recursive: !0
|
|
47
|
-
}), node_fs.writeFileSync(realEntryPath, tpl, 'utf-8'), config.entry = {
|
|
48
|
-
main: realEntryPath
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
return config;
|
|
52
|
-
}, PLUGIN_NAME = 'unpack:react', pluginReact = (options = {})=>{
|
|
53
|
-
let { clickToComponent } = options;
|
|
54
|
-
return {
|
|
55
|
-
name: PLUGIN_NAME,
|
|
6
|
+
import plugin_react_refresh from "@rspack/plugin-react-refresh";
|
|
7
|
+
import { JSX_REGEX, JS_REGEX, SCRIPT_REGEX, esVersionToBrowserslist, getHtmlTemplateOrContent, getPathInJs, injectToHead, isDevServer, isFunction, isPlainObject, launchEditor, mergeConfig as core_mergeConfig, removeDir, rspack } from "@unpackjs/core";
|
|
8
|
+
var src_dirname = __rspack_dirname(__rspack_fileURLToPath(import.meta.url));
|
|
9
|
+
let pluginReact = ()=>({
|
|
10
|
+
name: 'unpack:react',
|
|
56
11
|
bundlerConfig: async (originalConfig, { unpackConfig, mergeConfig, cachePath })=>{
|
|
57
12
|
let config = originalConfig;
|
|
58
13
|
unpackConfig.mpa && (config = (({ config, unpackConfig, cachePath })=>{
|
|
59
|
-
let entry, html, pagesRoot, tempDirectory = node_path.join(cachePath, 'mpa'), userOptions = isPlainObject(unpackConfig.mpa) ? unpackConfig.mpa : {}, { entry: entry1, html: html1 } = (entry = {}, html = [], pagesRoot = node_path.join(unpackConfig.root, 'src', 'pages'), node_fs.readdirSync(pagesRoot).forEach((filename)=>{
|
|
14
|
+
let entry, html, pagesRoot, tempDirectory = node_path.join(cachePath, 'mpa'), userOptions = isPlainObject(unpackConfig.mpa) ? unpackConfig.mpa : {}, { template, templateContent } = getHtmlTemplateOrContent(unpackConfig.html?.template, unpackConfig.root), { entry: entry1, html: html1 } = (entry = {}, html = [], pagesRoot = node_path.join(unpackConfig.root, 'src', 'pages'), node_fs.readdirSync(pagesRoot).forEach((filename)=>{
|
|
60
15
|
if (filename.startsWith('.')) return;
|
|
61
16
|
let indexFilePath = ((dir)=>{
|
|
62
17
|
for (let extension of [
|
|
@@ -69,28 +24,23 @@ import '${getPathInJs(node_path.resolve(Object.values(config.entry)[0]))}'
|
|
|
69
24
|
return null;
|
|
70
25
|
})(node_path.join(pagesRoot, filename));
|
|
71
26
|
if (indexFilePath) {
|
|
72
|
-
let
|
|
73
|
-
|
|
27
|
+
let entryName = !0 === userOptions.lowerCase ? filename.toLowerCase() : filename;
|
|
28
|
+
entry[entryName] = {
|
|
74
29
|
import: [
|
|
75
30
|
indexFilePath
|
|
76
31
|
]
|
|
77
|
-
},
|
|
78
|
-
template: unpackConfig.html?.template || void 0,
|
|
79
|
-
templateContent: unpackConfig.html?.templateContent || (unpackConfig.html?.template ? void 0 : TEMPLATE_CONTENT),
|
|
80
|
-
minify: !1,
|
|
32
|
+
}, html.push({
|
|
81
33
|
filename: `${entryName}.html`,
|
|
82
34
|
chunks: [
|
|
83
35
|
entryName
|
|
84
36
|
],
|
|
85
|
-
...pageConfig1,
|
|
86
37
|
templateParameters: {
|
|
87
38
|
mountId: unpackConfig.html?.mountId || '',
|
|
88
39
|
title: isFunction(unpackConfig.html?.title) ? unpackConfig.html?.title({
|
|
89
40
|
entryName
|
|
90
41
|
}) : unpackConfig.html?.title || '',
|
|
91
42
|
headTag: unpackConfig.html?.headTag || '',
|
|
92
|
-
...unpackConfig.html?.templateParameters
|
|
93
|
-
...pageConfig1.templateParameters
|
|
43
|
+
...unpackConfig.html?.templateParameters
|
|
94
44
|
}
|
|
95
45
|
});
|
|
96
46
|
}
|
|
@@ -116,16 +66,17 @@ ${renderer}
|
|
|
116
66
|
}), node_fs.writeFileSync(filePath, tpl, 'utf-8'), entryConfig.import[0] = filePath;
|
|
117
67
|
}), entry;
|
|
118
68
|
})(entry1),
|
|
119
|
-
plugins: html1.map((h)=>new rspack.HtmlRspackPlugin(
|
|
69
|
+
plugins: html1.map((h)=>new rspack.HtmlRspackPlugin({
|
|
70
|
+
...h,
|
|
71
|
+
template,
|
|
72
|
+
templateContent,
|
|
73
|
+
minify: !1
|
|
74
|
+
}))
|
|
120
75
|
});
|
|
121
76
|
})({
|
|
122
77
|
config,
|
|
123
78
|
unpackConfig,
|
|
124
79
|
cachePath
|
|
125
|
-
})), isDev() && clickToComponent && (config = await applyClickToComponentConfig({
|
|
126
|
-
config,
|
|
127
|
-
unpackConfig,
|
|
128
|
-
cachePath
|
|
129
80
|
}));
|
|
130
81
|
let jsExclude = [
|
|
131
82
|
/node_modules[\\/](?!\.unpack)/
|
|
@@ -153,7 +104,7 @@ ${renderer}
|
|
|
153
104
|
module: {
|
|
154
105
|
rules: [
|
|
155
106
|
{
|
|
156
|
-
test:
|
|
107
|
+
test: JS_REGEX,
|
|
157
108
|
exclude: jsExclude,
|
|
158
109
|
use: [
|
|
159
110
|
{
|
|
@@ -165,7 +116,7 @@ ${renderer}
|
|
|
165
116
|
]
|
|
166
117
|
},
|
|
167
118
|
{
|
|
168
|
-
test:
|
|
119
|
+
test: JSX_REGEX,
|
|
169
120
|
exclude: jsExclude,
|
|
170
121
|
use: [
|
|
171
122
|
{
|
|
@@ -183,7 +134,7 @@ ${renderer}
|
|
|
183
134
|
},
|
|
184
135
|
{
|
|
185
136
|
test: /\.svg$/i,
|
|
186
|
-
issuer:
|
|
137
|
+
issuer: SCRIPT_REGEX,
|
|
187
138
|
resourceQuery: {
|
|
188
139
|
not: [
|
|
189
140
|
/url/
|
|
@@ -206,7 +157,23 @@ ${renderer}
|
|
|
206
157
|
}
|
|
207
158
|
}
|
|
208
159
|
});
|
|
160
|
+
},
|
|
161
|
+
setupMiddlewares: (middlewares)=>isDevServer() ? [
|
|
162
|
+
(req, res, next)=>{
|
|
163
|
+
if (!req.url) return void next();
|
|
164
|
+
let url = new URL(req.url, 'http://localhost');
|
|
165
|
+
if ('/__click_to_component__' !== url.pathname) return void next();
|
|
166
|
+
let file = url.searchParams.get('file'), line = Number(url.searchParams.get('line')), column = Number(url.searchParams.get('column'));
|
|
167
|
+
res.writeHead(200, {
|
|
168
|
+
'Content-Type': 'text/plain'
|
|
169
|
+
}), res.end('ok'), launchEditor(`${decodeURIComponent(file)}:${line}:${column}`);
|
|
170
|
+
},
|
|
171
|
+
...middlewares
|
|
172
|
+
] : middlewares,
|
|
173
|
+
transformHtml: (html)=>{
|
|
174
|
+
if (!isDevServer()) return html;
|
|
175
|
+
let injectScriptPath = node_path.resolve(src_dirname, 'injectScript.js'), injectScript = node_fs.readFileSync(injectScriptPath, 'utf-8');
|
|
176
|
+
return injectToHead(html, `<script>${injectScript}</script>`);
|
|
209
177
|
}
|
|
210
|
-
};
|
|
211
|
-
};
|
|
212
|
-
export { PLUGIN_NAME, pluginReact };
|
|
178
|
+
});
|
|
179
|
+
export { pluginReact };
|
package/dist/injectScript.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
(()=>{
|
|
2
2
|
let cover = null, nameTag = null, currentDebugSource = null, cachedFiberKey = null, isEnabled = !1, debounceTimer = null;
|
|
3
3
|
function applyStyles(target, styles) {
|
|
4
4
|
for(let key in styles)target.style[key] = styles[key];
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
let handleClick = (e)=>{
|
|
10
10
|
0 === e.button && (e.preventDefault(), e.stopPropagation(), function() {
|
|
11
11
|
if (!currentDebugSource) return;
|
|
12
|
-
let
|
|
12
|
+
let { fileName, lineNumber, columnNumber } = currentDebugSource, url = new URL('/__click_to_component__', window.location.origin);
|
|
13
13
|
url.searchParams.set('file', fileName), url.searchParams.set('line', lineNumber), url.searchParams.set('column', columnNumber), fetch(url.toString()).catch(()=>{
|
|
14
14
|
new Image().src = url.toString();
|
|
15
15
|
});
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
if (!target || 'object' != typeof target) return null;
|
|
25
25
|
if (cachedFiberKey && target[cachedFiberKey]) return target[cachedFiberKey];
|
|
26
26
|
try {
|
|
27
|
-
cachedFiberKey = Object.keys(target).find((key)=>key.startsWith('__reactFiber
|
|
27
|
+
cachedFiberKey = Object.keys(target).find((key)=>key.startsWith('__reactFiber'));
|
|
28
28
|
} catch {
|
|
29
29
|
return null;
|
|
30
30
|
}
|
|
@@ -114,5 +114,5 @@
|
|
|
114
114
|
})));
|
|
115
115
|
}), document.addEventListener('keyup', (e)=>{
|
|
116
116
|
('Alt' === e.key || 'Option' === e.key) && stopInspect();
|
|
117
|
-
}), window.addEventListener('blur', stopInspect)
|
|
118
|
-
}();
|
|
117
|
+
}), window.addEventListener('blur', stopInspect);
|
|
118
|
+
})();
|
package/dist-types/index.d.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
1
|
import { type UnpackPlugin } from '@unpackjs/core';
|
|
2
|
-
export declare const
|
|
3
|
-
export type PluginReactOptions = {
|
|
4
|
-
clickToComponent?: boolean;
|
|
5
|
-
};
|
|
6
|
-
export declare const pluginReact: (options?: PluginReactOptions) => UnpackPlugin;
|
|
2
|
+
export declare const pluginReact: () => UnpackPlugin;
|
|
7
3
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAQL,KAAK,YAAY,EAClB,MAAM,gBAAgB,CAAA;AAGvB,eAAO,MAAM,WAAW,QAAO,YAiH9B,CAAA"}
|
package/dist-types/mpa.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mpa.d.ts","sourceRoot":"","sources":["../src/mpa.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,oBAAoB,EAQzB,KAAK,YAAY,EAClB,MAAM,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"mpa.d.ts","sourceRoot":"","sources":["../src/mpa.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,oBAAoB,EAQzB,KAAK,YAAY,EAClB,MAAM,gBAAgB,CAAA;AASvB,eAAO,MAAM,cAAc,GAAI,sCAI5B;IACD,MAAM,EAAE,oBAAoB,CAAA;IAC5B,YAAY,EAAE,YAAY,CAAA;IAC1B,SAAS,EAAE,MAAM,CAAA;CAClB,KAAG,oBA4FH,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unpackjs/plugin-react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "React plugin for Unpack",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -22,16 +22,16 @@
|
|
|
22
22
|
"access": "public"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@rspack/plugin-react-refresh": "1.
|
|
25
|
+
"@rspack/plugin-react-refresh": "1.6.0",
|
|
26
26
|
"@svgr/webpack": "8.1.0",
|
|
27
|
-
"@swc/helpers": "0.5.
|
|
27
|
+
"@swc/helpers": "0.5.18",
|
|
28
28
|
"react-refresh": "0.18.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@unpackjs/core": "
|
|
31
|
+
"@unpackjs/core": "4.0.0"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@unpackjs/core": "
|
|
34
|
+
"@unpackjs/core": "4.x"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"dev": "rslib build --watch",
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { type BundlerConfiguration, type UnpackConfig } from '@unpackjs/core';
|
|
2
|
-
export declare const applyClickToComponentConfig: ({ config, unpackConfig, cachePath, }: {
|
|
3
|
-
config: BundlerConfiguration;
|
|
4
|
-
unpackConfig: UnpackConfig;
|
|
5
|
-
cachePath: string;
|
|
6
|
-
}) => Promise<BundlerConfiguration>;
|
|
7
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/click-to-component/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,KAAK,oBAAoB,EAIzB,KAAK,YAAY,EAClB,MAAM,gBAAgB,CAAA;AA4BvB,eAAO,MAAM,2BAA2B,GAAU,sCAI/C;IACD,MAAM,EAAE,oBAAoB,CAAA;IAC5B,YAAY,EAAE,YAAY,CAAA;IAC1B,SAAS,EAAE,MAAM,CAAA;CAClB,KAAG,OAAO,CAAC,oBAAoB,CA+B/B,CAAA"}
|