@unpackjs/plugin-react 2.1.0 → 2.2.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.cjs +182 -81
- package/dist/index.js +155 -41
- package/dist/injectScript.js +82 -0
- package/{dist → dist-types}/index.d.ts +0 -1
- package/dist-types/index.d.ts.map +1 -0
- package/{dist → dist-types}/mpa.d.ts.map +1 -1
- package/package.json +8 -11
- package/dist/click-to-component/index.cjs +0 -110
- package/dist/click-to-component/index.js +0 -68
- package/dist/click-to-component/injectScript.js +0 -116
- package/dist/index.d.ts.map +0 -1
- package/dist/mpa.cjs +0 -152
- package/dist/mpa.js +0 -109
- package/dist/react-compiler-loader/index.cjs +0 -95
- package/dist/react-compiler-loader/index.d.ts +0 -5
- package/dist/react-compiler-loader/index.d.ts.map +0 -1
- package/dist/react-compiler-loader/index.js +0 -51
- package/dist/splitChunks.cjs +0 -59
- package/dist/splitChunks.js +0 -27
- /package/{dist → dist-types}/click-to-component/index.d.ts +0 -0
- /package/{dist → dist-types}/click-to-component/index.d.ts.map +0 -0
- /package/{dist → dist-types}/mpa.d.ts +0 -0
- /package/{dist → dist-types}/splitChunks.d.ts +0 -0
- /package/{dist → dist-types}/splitChunks.d.ts.map +0 -0
package/dist/index.cjs
CHANGED
|
@@ -1,92 +1,201 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
let __rslib_import_meta_url__ = 'undefined' == typeof document ? new (require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
|
|
2
3
|
var __webpack_require__ = {};
|
|
3
|
-
(
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
(()=>{
|
|
21
|
-
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
22
|
-
})();
|
|
23
|
-
(()=>{
|
|
24
|
-
__webpack_require__.r = (exports1)=>{
|
|
25
|
-
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
26
|
-
value: 'Module'
|
|
27
|
-
});
|
|
28
|
-
Object.defineProperty(exports1, '__esModule', {
|
|
29
|
-
value: true
|
|
30
|
-
});
|
|
31
|
-
};
|
|
32
|
-
})();
|
|
4
|
+
__webpack_require__.n = (module)=>{
|
|
5
|
+
var getter = module && module.__esModule ? ()=>module.default : ()=>module;
|
|
6
|
+
return __webpack_require__.d(getter, {
|
|
7
|
+
a: getter
|
|
8
|
+
}), getter;
|
|
9
|
+
}, __webpack_require__.d = (exports1, definition)=>{
|
|
10
|
+
for(var key in definition)__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key) && Object.defineProperty(exports1, key, {
|
|
11
|
+
enumerable: !0,
|
|
12
|
+
get: definition[key]
|
|
13
|
+
});
|
|
14
|
+
}, __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop), __webpack_require__.r = (exports1)=>{
|
|
15
|
+
'undefined' != typeof Symbol && Symbol.toStringTag && Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
16
|
+
value: 'Module'
|
|
17
|
+
}), Object.defineProperty(exports1, '__esModule', {
|
|
18
|
+
value: !0
|
|
19
|
+
});
|
|
20
|
+
};
|
|
33
21
|
var __webpack_exports__ = {};
|
|
34
|
-
__webpack_require__.r(__webpack_exports__)
|
|
35
|
-
__webpack_require__.d(__webpack_exports__, {
|
|
22
|
+
__webpack_require__.r(__webpack_exports__), __webpack_require__.d(__webpack_exports__, {
|
|
36
23
|
pluginReact: ()=>pluginReact,
|
|
37
24
|
PLUGIN_NAME: ()=>PLUGIN_NAME
|
|
38
25
|
});
|
|
39
|
-
|
|
40
|
-
var external_node_path_default =
|
|
41
|
-
|
|
42
|
-
var react_refresh_webpack_plugin_default =
|
|
43
|
-
|
|
44
|
-
var plugin_react_refresh_default =
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
26
|
+
let external_node_path_namespaceObject = require("node:path");
|
|
27
|
+
var external_node_path_default = __webpack_require__.n(external_node_path_namespaceObject);
|
|
28
|
+
let react_refresh_webpack_plugin_namespaceObject = require("@pmmmwh/react-refresh-webpack-plugin");
|
|
29
|
+
var react_refresh_webpack_plugin_default = __webpack_require__.n(react_refresh_webpack_plugin_namespaceObject);
|
|
30
|
+
let plugin_react_refresh_namespaceObject = require("@rspack/plugin-react-refresh");
|
|
31
|
+
var plugin_react_refresh_default = __webpack_require__.n(plugin_react_refresh_namespaceObject);
|
|
32
|
+
let core_namespaceObject = require("@unpackjs/core"), external_node_fs_namespaceObject = require("node:fs");
|
|
33
|
+
var external_node_fs_default = __webpack_require__.n(external_node_fs_namespaceObject);
|
|
34
|
+
let external_node_http_namespaceObject = require("node:http");
|
|
35
|
+
var external_node_http_default = __webpack_require__.n(external_node_http_namespaceObject);
|
|
36
|
+
let external_launch_editor_namespaceObject = require("launch-editor");
|
|
37
|
+
var external_launch_editor_default = __webpack_require__.n(external_launch_editor_namespaceObject);
|
|
38
|
+
let createServer = ()=>new Promise((resolve)=>{
|
|
39
|
+
let server = external_node_http_default().createServer((req, res)=>{
|
|
40
|
+
let params = new URLSearchParams(req.url.slice(1)), file = decodeURIComponent(params.get('file')), line = Number(params.get('line')), column = Number(params.get('column'));
|
|
41
|
+
res.writeHead(200, {
|
|
42
|
+
'Access-Control-Allow-Origin': '*',
|
|
43
|
+
'Access-Control-Allow-Methods': '*',
|
|
44
|
+
'Access-Control-Allow-Headers': '*',
|
|
45
|
+
'Access-Control-Allow-Private-Network': 'true'
|
|
46
|
+
}), res.end('ok'), external_launch_editor_default()(`${file}:${line}:${column}`);
|
|
47
|
+
});
|
|
48
|
+
(0, core_namespaceObject.getPort)().then((port)=>{
|
|
49
|
+
server.listen(port, ()=>{
|
|
50
|
+
resolve({
|
|
51
|
+
port
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
}), core_namespaceObject.restartCleaners.push(()=>new Promise((resolve)=>server.close(()=>resolve())));
|
|
55
|
+
}), applyClickToComponentConfig = async ({ config, unpackConfig })=>{
|
|
56
|
+
let { port } = await createServer(), injectScript = external_node_fs_default().readFileSync(external_node_path_default().resolve(__dirname, './injectScript.js'), 'utf-8'), scriptTpl = `
|
|
57
|
+
if (!document.querySelector('#__click-to-component__')) {
|
|
58
|
+
const script = document.createElement('script')
|
|
59
|
+
window['CLICK_TO_COMPONENT'] = { port: ${port} }
|
|
60
|
+
script.setAttribute('type', 'text/javascript')
|
|
61
|
+
script.textContent = \`${injectScript}\`
|
|
62
|
+
document.body.appendChild(script)
|
|
63
|
+
}`;
|
|
64
|
+
if (unpackConfig.mpa) Object.values(config.entry).forEach((entryConfig)=>{
|
|
65
|
+
let filePath = entryConfig.import[0], content = external_node_fs_default().readFileSync(filePath, 'utf-8');
|
|
66
|
+
external_node_fs_default().writeFileSync(filePath, `${scriptTpl}\n${content}`, 'utf-8');
|
|
67
|
+
});
|
|
68
|
+
else {
|
|
69
|
+
let realEntryPath = external_node_path_default().resolve(config.context, core_namespaceObject.TEMP_DIR, 'entries', `index.${port}.jsx`), tpl = `
|
|
70
|
+
${scriptTpl}
|
|
71
|
+
import '${(0, core_namespaceObject.getPathInJs)(external_node_path_default().resolve(Object.values(config.entry)[0]))}'
|
|
72
|
+
`, dir = external_node_path_default().dirname(realEntryPath);
|
|
73
|
+
external_node_fs_default().existsSync(dir) || external_node_fs_default().mkdirSync(dir, {
|
|
74
|
+
recursive: !0
|
|
75
|
+
}), external_node_fs_default().writeFileSync(realEntryPath, tpl, 'utf-8'), config.entry = {
|
|
76
|
+
main: realEntryPath
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return config;
|
|
80
|
+
}, applyMpaConfig = ({ config, unpackConfig, mergeConfig, HtmlPlugin })=>{
|
|
81
|
+
if (!unpackConfig.mpa) return config;
|
|
82
|
+
let isRspack = 'rspack' === unpackConfig.bundler, tempDirectory = external_node_path_default().join(core_namespaceObject.TEMP_DIR, 'mpa'), userOptions = (0, core_namespaceObject.isPlainObject)(unpackConfig.mpa) ? unpackConfig.mpa : {}, { entry, html } = (()=>{
|
|
83
|
+
let start = performance.now(), entry = {}, html = [], pagesRoot = external_node_path_default().join(unpackConfig.root, 'src', 'pages');
|
|
84
|
+
return external_node_fs_default().readdirSync(pagesRoot).forEach((filename)=>{
|
|
85
|
+
if (filename.startsWith('.')) return;
|
|
86
|
+
let indexFilePath = ((dir)=>{
|
|
87
|
+
for (let extension of [
|
|
88
|
+
'.tsx',
|
|
89
|
+
'.jsx'
|
|
90
|
+
]){
|
|
91
|
+
let indexFilePath = external_node_path_default().join(dir, `index${extension}`);
|
|
92
|
+
if (external_node_fs_default().existsSync(indexFilePath)) return indexFilePath;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
})(external_node_path_default().join(pagesRoot, filename));
|
|
96
|
+
if (indexFilePath) {
|
|
97
|
+
var _unpackConfig_html, _unpackConfig_html1, _unpackConfig_html2, _unpackConfig_html3, _unpackConfig_html4, _unpackConfig_html5, _unpackConfig_html6, _unpackConfig_html7, _unpackConfig_html8;
|
|
98
|
+
let pageConfig = ((indexPath)=>{
|
|
99
|
+
let filePath = external_node_path_default().join(indexPath, '../config.json'), pageConfig = {};
|
|
100
|
+
return external_node_fs_default().existsSync(filePath) && (pageConfig = JSON.parse(external_node_fs_default().readFileSync(filePath, 'utf-8'))), pageConfig;
|
|
101
|
+
})(indexFilePath), entryName = !0 === userOptions.lowerCase ? filename.toLowerCase() : filename;
|
|
102
|
+
pageConfig.filename && (entryName = pageConfig.filename.slice(0, -5)), entry[entryName] = {
|
|
103
|
+
import: [
|
|
104
|
+
indexFilePath
|
|
105
|
+
]
|
|
106
|
+
}, pageConfig.template && (pageConfig.template = external_node_path_default().join(indexFilePath, '../', pageConfig.template)), html.push({
|
|
107
|
+
template: (null == (_unpackConfig_html = unpackConfig.html) ? void 0 : _unpackConfig_html.template) || (isRspack ? void 0 : ''),
|
|
108
|
+
templateContent: (null == (_unpackConfig_html1 = unpackConfig.html) ? void 0 : _unpackConfig_html1.templateContent) || ((null == (_unpackConfig_html2 = unpackConfig.html) ? void 0 : _unpackConfig_html2.template) ? !!isRspack && void 0 : core_namespaceObject.TEMPLATE_CONTENT),
|
|
109
|
+
minify: !1,
|
|
110
|
+
filename: `${entryName}.html`,
|
|
111
|
+
chunks: [
|
|
112
|
+
entryName
|
|
113
|
+
],
|
|
114
|
+
...pageConfig,
|
|
115
|
+
templateParameters: {
|
|
116
|
+
mountId: null == (_unpackConfig_html3 = unpackConfig.html) ? void 0 : _unpackConfig_html3.mountId,
|
|
117
|
+
title: (0, core_namespaceObject.isFunction)(null == (_unpackConfig_html4 = unpackConfig.html) ? void 0 : _unpackConfig_html4.title) ? null == (_unpackConfig_html5 = unpackConfig.html) ? void 0 : _unpackConfig_html5.title({
|
|
118
|
+
entryName
|
|
119
|
+
}) : (null == (_unpackConfig_html6 = unpackConfig.html) ? void 0 : _unpackConfig_html6.title) || '',
|
|
120
|
+
headTag: (null == (_unpackConfig_html7 = unpackConfig.html) ? void 0 : _unpackConfig_html7.headTag) || '',
|
|
121
|
+
...null == (_unpackConfig_html8 = unpackConfig.html) ? void 0 : _unpackConfig_html8.templateParameters,
|
|
122
|
+
...pageConfig.templateParameters
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}), (0, core_namespaceObject.isDevServer)() || core_namespaceObject.logger.info(`collect entries in ${(performance.now() - start).toFixed(2)}ms ${core_namespaceObject.colors.dim('[MPA]')}`), {
|
|
127
|
+
entry,
|
|
128
|
+
html
|
|
129
|
+
};
|
|
130
|
+
})();
|
|
131
|
+
return config = mergeConfig(config, {
|
|
132
|
+
entry: ((entry)=>{
|
|
133
|
+
var _userOptions_globalImport, _unpackConfig_html;
|
|
134
|
+
let root = unpackConfig.root;
|
|
135
|
+
(0, core_namespaceObject.removeDir)(external_node_path_default().join(root, tempDirectory));
|
|
136
|
+
let globalImport = (null == (_userOptions_globalImport = userOptions.globalImport) ? void 0 : _userOptions_globalImport.reduce((acc, curr)=>`${acc}\nimport '${(0, core_namespaceObject.getPathInJs)(external_node_path_default().resolve(root, curr))}'`, '')) || '', { layout } = userOptions, layoutImport = layout ? `import Layout from '${(0, core_namespaceObject.getPathInJs)(external_node_path_default().resolve(root, layout))}'` : '', layoutJSX = layout ? '<Layout><App /></Layout>' : '<App />', rootElement = `document.getElementById('${null == (_unpackConfig_html = unpackConfig.html) ? void 0 : _unpackConfig_html.mountId}')`, renderer = `ReactDOM.createRoot(${rootElement}).render(${layoutJSX})`;
|
|
137
|
+
return Object.entries(entry).forEach(([entryName, entryConfig])=>{
|
|
138
|
+
let filePath = external_node_path_default().join(root, tempDirectory, `${entryName}.jsx`), tpl = `
|
|
139
|
+
import React from 'react'
|
|
140
|
+
import ReactDOM from 'react-dom/client'
|
|
141
|
+
import App from '${(0, core_namespaceObject.getPathInJs)(entryConfig.import[0])}'${layoutImport && `\n${layoutImport}`}
|
|
142
|
+
${globalImport}
|
|
143
|
+
${renderer}
|
|
144
|
+
`, dir = external_node_path_default().dirname(filePath);
|
|
145
|
+
external_node_fs_default().existsSync(dir) || external_node_fs_default().mkdirSync(dir, {
|
|
146
|
+
recursive: !0
|
|
147
|
+
}), external_node_fs_default().writeFileSync(filePath, tpl, 'utf-8'), entryConfig.import[0] = filePath;
|
|
148
|
+
}), entry;
|
|
149
|
+
})(entry),
|
|
150
|
+
plugins: html.map((h)=>new HtmlPlugin(h))
|
|
151
|
+
});
|
|
152
|
+
}, applySplitChunksConfig = ({ config, unpackConfig })=>{
|
|
153
|
+
if ('split-by-experience' !== unpackConfig.performance.chunkSplit.strategy) return config;
|
|
154
|
+
let currentConfig = config.optimization.splitChunks, extraGroups = {};
|
|
155
|
+
return extraGroups.react = {
|
|
156
|
+
name: 'lib-react',
|
|
157
|
+
test: (0, core_namespaceObject.isDevServer)() ? /node_modules[\\/](?:react|react-dom|scheduler|react-refresh|@rspack[\\/]plugin-react-refresh)[\\/]/ : /node_modules[\\/](?:react|react-dom|scheduler)[\\/]/,
|
|
158
|
+
priority: 0
|
|
159
|
+
}, extraGroups.router = {
|
|
160
|
+
name: 'lib-router',
|
|
161
|
+
test: /node_modules[\\/](?:react-router|react-router-dom|history|@remix-run[\\/]router)[\\/]/,
|
|
162
|
+
priority: 0
|
|
163
|
+
}, config.optimization.splitChunks = {
|
|
164
|
+
...currentConfig,
|
|
165
|
+
cacheGroups: {
|
|
166
|
+
...extraGroups,
|
|
167
|
+
...currentConfig.cacheGroups
|
|
168
|
+
}
|
|
169
|
+
}, config;
|
|
170
|
+
}, PLUGIN_NAME = 'unpack:react', pluginReact = (options = {})=>{
|
|
171
|
+
let { clickToComponent } = options;
|
|
52
172
|
return {
|
|
53
173
|
name: PLUGIN_NAME,
|
|
54
174
|
bundlerConfig: async (originalConfig, { unpackConfig, mergeConfig })=>{
|
|
55
|
-
let config = originalConfig;
|
|
56
|
-
|
|
57
|
-
const HtmlPlugin = isRspack ? core_namespaceObject.rspack.HtmlRspackPlugin : core_namespaceObject.HtmlWebpackPlugin;
|
|
58
|
-
config = (0, external_mpa_cjs_namespaceObject.applyMpaConfig)({
|
|
175
|
+
let config = originalConfig, isRspack = 'rspack' === unpackConfig.bundler;
|
|
176
|
+
config = applyMpaConfig({
|
|
59
177
|
config,
|
|
60
178
|
unpackConfig,
|
|
61
179
|
mergeConfig,
|
|
62
|
-
HtmlPlugin
|
|
63
|
-
})
|
|
64
|
-
if ((0, core_namespaceObject.isDev)() && clickToComponent) config = await (0, index_cjs_namespaceObject.applyClickToComponentConfig)({
|
|
180
|
+
HtmlPlugin: isRspack ? core_namespaceObject.rspack.HtmlRspackPlugin : core_namespaceObject.HtmlWebpackPlugin
|
|
181
|
+
}), (0, core_namespaceObject.isDev)() && clickToComponent && (config = await applyClickToComponentConfig({
|
|
65
182
|
config,
|
|
66
183
|
unpackConfig
|
|
67
|
-
})
|
|
68
|
-
config = (0, external_splitChunks_cjs_namespaceObject.applySplitChunksConfig)({
|
|
184
|
+
})), config = applySplitChunksConfig({
|
|
69
185
|
config,
|
|
70
186
|
unpackConfig
|
|
71
187
|
});
|
|
72
|
-
|
|
73
|
-
core_namespaceObject.logger.error('dependency "react-compiler-runtime" not found, did you install it?');
|
|
74
|
-
process.exit(1);
|
|
75
|
-
}
|
|
76
|
-
const jsExclude = [
|
|
188
|
+
let jsExclude = [
|
|
77
189
|
/node_modules[\\/](?!\.unpack)/
|
|
78
|
-
]
|
|
79
|
-
const swcLoader = isRspack ? 'builtin:swc-loader' : require.resolve('swc-loader');
|
|
80
|
-
const ReactRefreshPlugin = isRspack ? plugin_react_refresh_default() : react_refresh_webpack_plugin_default();
|
|
81
|
-
const getSwcOptions = ({ tsx })=>({
|
|
190
|
+
], swcLoader = isRspack ? 'builtin:swc-loader' : require.resolve('swc-loader'), ReactRefreshPlugin = isRspack ? plugin_react_refresh_default() : react_refresh_webpack_plugin_default(), getSwcOptions = ({ tsx })=>({
|
|
82
191
|
jsc: {
|
|
83
192
|
parser: {
|
|
84
193
|
tsx,
|
|
85
194
|
syntax: "typescript",
|
|
86
|
-
dynamicImport:
|
|
87
|
-
decorators:
|
|
195
|
+
dynamicImport: !0,
|
|
196
|
+
decorators: !0
|
|
88
197
|
},
|
|
89
|
-
externalHelpers:
|
|
198
|
+
externalHelpers: !0,
|
|
90
199
|
transform: {
|
|
91
200
|
react: {
|
|
92
201
|
runtime: 'automatic',
|
|
@@ -108,7 +217,7 @@ const pluginReact = (options = {})=>{
|
|
|
108
217
|
{
|
|
109
218
|
loader: swcLoader,
|
|
110
219
|
options: getSwcOptions({
|
|
111
|
-
tsx:
|
|
220
|
+
tsx: !1
|
|
112
221
|
})
|
|
113
222
|
}
|
|
114
223
|
]
|
|
@@ -120,16 +229,10 @@ const pluginReact = (options = {})=>{
|
|
|
120
229
|
{
|
|
121
230
|
loader: swcLoader,
|
|
122
231
|
options: getSwcOptions({
|
|
123
|
-
tsx:
|
|
232
|
+
tsx: !0
|
|
124
233
|
})
|
|
125
|
-
},
|
|
126
|
-
reactCompiler && {
|
|
127
|
-
loader: external_node_path_default().resolve(__dirname, './react-compiler-loader/index.cjs'),
|
|
128
|
-
options: {
|
|
129
|
-
target: (0, core_namespaceObject.getUserDepVersion)(unpackConfig.root, 'react').split('.')[0]
|
|
130
|
-
}
|
|
131
234
|
}
|
|
132
|
-
]
|
|
235
|
+
]
|
|
133
236
|
},
|
|
134
237
|
{
|
|
135
238
|
test: /\.svg$/i,
|
|
@@ -152,7 +255,7 @@ const pluginReact = (options = {})=>{
|
|
|
152
255
|
},
|
|
153
256
|
plugins: [
|
|
154
257
|
(0, core_namespaceObject.isDevServer)() && new ReactRefreshPlugin({
|
|
155
|
-
overlay:
|
|
258
|
+
overlay: !1
|
|
156
259
|
})
|
|
157
260
|
].filter(Boolean),
|
|
158
261
|
resolve: {
|
|
@@ -164,12 +267,10 @@ const pluginReact = (options = {})=>{
|
|
|
164
267
|
}
|
|
165
268
|
};
|
|
166
269
|
};
|
|
167
|
-
exports.PLUGIN_NAME = __webpack_exports__.PLUGIN_NAME
|
|
168
|
-
exports.pluginReact = __webpack_exports__.pluginReact;
|
|
169
|
-
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
270
|
+
for(var __webpack_i__ in exports.PLUGIN_NAME = __webpack_exports__.PLUGIN_NAME, exports.pluginReact = __webpack_exports__.pluginReact, __webpack_exports__)-1 === [
|
|
170
271
|
"PLUGIN_NAME",
|
|
171
272
|
"pluginReact"
|
|
172
|
-
].indexOf(__webpack_i__)
|
|
273
|
+
].indexOf(__webpack_i__) && (exports[__webpack_i__] = __webpack_exports__[__webpack_i__]);
|
|
173
274
|
Object.defineProperty(exports, '__esModule', {
|
|
174
|
-
value:
|
|
275
|
+
value: !0
|
|
175
276
|
});
|
package/dist/index.js
CHANGED
|
@@ -1,56 +1,176 @@
|
|
|
1
1
|
import __rslib_shim_module__ from 'module';
|
|
2
|
-
|
|
2
|
+
let require = __rslib_shim_module__.createRequire(import.meta.url);
|
|
3
3
|
import { fileURLToPath as __webpack_fileURLToPath__ } from "node:url";
|
|
4
4
|
import { dirname as __webpack_dirname__ } from "node:path";
|
|
5
5
|
import node_path from "node:path";
|
|
6
6
|
import react_refresh_webpack_plugin from "@pmmmwh/react-refresh-webpack-plugin";
|
|
7
7
|
import plugin_react_refresh from "@rspack/plugin-react-refresh";
|
|
8
|
-
import { DEFAULT_BROWSERSLIST, HtmlWebpackPlugin,
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
import { DEFAULT_BROWSERSLIST, HtmlWebpackPlugin, TEMPLATE_CONTENT, TEMP_DIR, colors, getPathInJs, getPort, isDev, isDevServer, isFunction, isPlainObject, logger, removeDir, restartCleaners, rspack } from "@unpackjs/core";
|
|
9
|
+
import node_fs from "node:fs";
|
|
10
|
+
import node_http from "node:http";
|
|
11
|
+
import launch_editor from "launch-editor";
|
|
12
|
+
var click_to_component_dirname = __webpack_dirname__(__webpack_fileURLToPath__(import.meta.url));
|
|
13
|
+
let applyClickToComponentConfig = async ({ config, unpackConfig })=>{
|
|
14
|
+
let { port } = await new Promise((resolve)=>{
|
|
15
|
+
let server = node_http.createServer((req, res)=>{
|
|
16
|
+
let params = new URLSearchParams(req.url.slice(1)), file = decodeURIComponent(params.get('file')), line = Number(params.get('line')), column = Number(params.get('column'));
|
|
17
|
+
res.writeHead(200, {
|
|
18
|
+
'Access-Control-Allow-Origin': '*',
|
|
19
|
+
'Access-Control-Allow-Methods': '*',
|
|
20
|
+
'Access-Control-Allow-Headers': '*',
|
|
21
|
+
'Access-Control-Allow-Private-Network': 'true'
|
|
22
|
+
}), res.end('ok'), launch_editor(`${file}:${line}:${column}`);
|
|
23
|
+
});
|
|
24
|
+
getPort().then((port)=>{
|
|
25
|
+
server.listen(port, ()=>{
|
|
26
|
+
resolve({
|
|
27
|
+
port
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}), restartCleaners.push(()=>new Promise((resolve)=>server.close(()=>resolve())));
|
|
31
|
+
}), injectScript = node_fs.readFileSync(node_path.resolve(click_to_component_dirname, './injectScript.js'), 'utf-8'), scriptTpl = `
|
|
32
|
+
if (!document.querySelector('#__click-to-component__')) {
|
|
33
|
+
const script = document.createElement('script')
|
|
34
|
+
window['CLICK_TO_COMPONENT'] = { port: ${port} }
|
|
35
|
+
script.setAttribute('type', 'text/javascript')
|
|
36
|
+
script.textContent = \`${injectScript}\`
|
|
37
|
+
document.body.appendChild(script)
|
|
38
|
+
}`;
|
|
39
|
+
if (unpackConfig.mpa) Object.values(config.entry).forEach((entryConfig)=>{
|
|
40
|
+
let filePath = entryConfig.import[0], content = node_fs.readFileSync(filePath, 'utf-8');
|
|
41
|
+
node_fs.writeFileSync(filePath, `${scriptTpl}\n${content}`, 'utf-8');
|
|
42
|
+
});
|
|
43
|
+
else {
|
|
44
|
+
let realEntryPath = node_path.resolve(config.context, TEMP_DIR, 'entries', `index.${port}.jsx`), tpl = `
|
|
45
|
+
${scriptTpl}
|
|
46
|
+
import '${getPathInJs(node_path.resolve(Object.values(config.entry)[0]))}'
|
|
47
|
+
`, dir = node_path.dirname(realEntryPath);
|
|
48
|
+
node_fs.existsSync(dir) || node_fs.mkdirSync(dir, {
|
|
49
|
+
recursive: !0
|
|
50
|
+
}), node_fs.writeFileSync(realEntryPath, tpl, 'utf-8'), config.entry = {
|
|
51
|
+
main: realEntryPath
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return config;
|
|
55
|
+
}, PLUGIN_NAME = 'unpack:react', pluginReact = (options = {})=>{
|
|
56
|
+
let { clickToComponent } = options;
|
|
16
57
|
return {
|
|
17
58
|
name: PLUGIN_NAME,
|
|
18
59
|
bundlerConfig: async (originalConfig, { unpackConfig, mergeConfig })=>{
|
|
19
|
-
let config = originalConfig;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
60
|
+
let config = originalConfig, isRspack = 'rspack' === unpackConfig.bundler;
|
|
61
|
+
config = (({ config, unpackConfig, mergeConfig, HtmlPlugin })=>{
|
|
62
|
+
if (!unpackConfig.mpa) return config;
|
|
63
|
+
let isRspack = 'rspack' === unpackConfig.bundler, tempDirectory = node_path.join(TEMP_DIR, 'mpa'), userOptions = isPlainObject(unpackConfig.mpa) ? unpackConfig.mpa : {}, { entry, html } = (()=>{
|
|
64
|
+
let start = performance.now(), entry = {}, html = [], pagesRoot = node_path.join(unpackConfig.root, 'src', 'pages');
|
|
65
|
+
return node_fs.readdirSync(pagesRoot).forEach((filename)=>{
|
|
66
|
+
if (filename.startsWith('.')) return;
|
|
67
|
+
let indexFilePath = ((dir)=>{
|
|
68
|
+
for (let extension of [
|
|
69
|
+
'.tsx',
|
|
70
|
+
'.jsx'
|
|
71
|
+
]){
|
|
72
|
+
let indexFilePath = node_path.join(dir, `index${extension}`);
|
|
73
|
+
if (node_fs.existsSync(indexFilePath)) return indexFilePath;
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
})(node_path.join(pagesRoot, filename));
|
|
77
|
+
if (indexFilePath) {
|
|
78
|
+
var _unpackConfig_html, _unpackConfig_html1, _unpackConfig_html2, _unpackConfig_html3, _unpackConfig_html4, _unpackConfig_html5, _unpackConfig_html6, _unpackConfig_html7, _unpackConfig_html8;
|
|
79
|
+
let pageConfig = ((indexPath)=>{
|
|
80
|
+
let filePath = node_path.join(indexPath, '../config.json'), pageConfig = {};
|
|
81
|
+
return node_fs.existsSync(filePath) && (pageConfig = JSON.parse(node_fs.readFileSync(filePath, 'utf-8'))), pageConfig;
|
|
82
|
+
})(indexFilePath), entryName = !0 === userOptions.lowerCase ? filename.toLowerCase() : filename;
|
|
83
|
+
pageConfig.filename && (entryName = pageConfig.filename.slice(0, -5)), entry[entryName] = {
|
|
84
|
+
import: [
|
|
85
|
+
indexFilePath
|
|
86
|
+
]
|
|
87
|
+
}, pageConfig.template && (pageConfig.template = node_path.join(indexFilePath, '../', pageConfig.template)), html.push({
|
|
88
|
+
template: (null == (_unpackConfig_html = unpackConfig.html) ? void 0 : _unpackConfig_html.template) || (isRspack ? void 0 : ''),
|
|
89
|
+
templateContent: (null == (_unpackConfig_html1 = unpackConfig.html) ? void 0 : _unpackConfig_html1.templateContent) || ((null == (_unpackConfig_html2 = unpackConfig.html) ? void 0 : _unpackConfig_html2.template) ? !!isRspack && void 0 : TEMPLATE_CONTENT),
|
|
90
|
+
minify: !1,
|
|
91
|
+
filename: `${entryName}.html`,
|
|
92
|
+
chunks: [
|
|
93
|
+
entryName
|
|
94
|
+
],
|
|
95
|
+
...pageConfig,
|
|
96
|
+
templateParameters: {
|
|
97
|
+
mountId: null == (_unpackConfig_html3 = unpackConfig.html) ? void 0 : _unpackConfig_html3.mountId,
|
|
98
|
+
title: isFunction(null == (_unpackConfig_html4 = unpackConfig.html) ? void 0 : _unpackConfig_html4.title) ? null == (_unpackConfig_html5 = unpackConfig.html) ? void 0 : _unpackConfig_html5.title({
|
|
99
|
+
entryName
|
|
100
|
+
}) : (null == (_unpackConfig_html6 = unpackConfig.html) ? void 0 : _unpackConfig_html6.title) || '',
|
|
101
|
+
headTag: (null == (_unpackConfig_html7 = unpackConfig.html) ? void 0 : _unpackConfig_html7.headTag) || '',
|
|
102
|
+
...null == (_unpackConfig_html8 = unpackConfig.html) ? void 0 : _unpackConfig_html8.templateParameters,
|
|
103
|
+
...pageConfig.templateParameters
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}), isDevServer() || logger.info(`collect entries in ${(performance.now() - start).toFixed(2)}ms ${colors.dim('[MPA]')}`), {
|
|
108
|
+
entry,
|
|
109
|
+
html
|
|
110
|
+
};
|
|
111
|
+
})();
|
|
112
|
+
return config = mergeConfig(config, {
|
|
113
|
+
entry: ((entry)=>{
|
|
114
|
+
var _userOptions_globalImport, _unpackConfig_html;
|
|
115
|
+
let root = unpackConfig.root;
|
|
116
|
+
removeDir(node_path.join(root, tempDirectory));
|
|
117
|
+
let globalImport = (null == (_userOptions_globalImport = userOptions.globalImport) ? void 0 : _userOptions_globalImport.reduce((acc, curr)=>`${acc}\nimport '${getPathInJs(node_path.resolve(root, curr))}'`, '')) || '', { layout } = userOptions, layoutImport = layout ? `import Layout from '${getPathInJs(node_path.resolve(root, layout))}'` : '', layoutJSX = layout ? '<Layout><App /></Layout>' : '<App />', rootElement = `document.getElementById('${null == (_unpackConfig_html = unpackConfig.html) ? void 0 : _unpackConfig_html.mountId}')`, renderer = `ReactDOM.createRoot(${rootElement}).render(${layoutJSX})`;
|
|
118
|
+
return Object.entries(entry).forEach(([entryName, entryConfig])=>{
|
|
119
|
+
let filePath = node_path.join(root, tempDirectory, `${entryName}.jsx`), tpl = `
|
|
120
|
+
import React from 'react'
|
|
121
|
+
import ReactDOM from 'react-dom/client'
|
|
122
|
+
import App from '${getPathInJs(entryConfig.import[0])}'${layoutImport && `\n${layoutImport}`}
|
|
123
|
+
${globalImport}
|
|
124
|
+
${renderer}
|
|
125
|
+
`, dir = node_path.dirname(filePath);
|
|
126
|
+
node_fs.existsSync(dir) || node_fs.mkdirSync(dir, {
|
|
127
|
+
recursive: !0
|
|
128
|
+
}), node_fs.writeFileSync(filePath, tpl, 'utf-8'), entryConfig.import[0] = filePath;
|
|
129
|
+
}), entry;
|
|
130
|
+
})(entry),
|
|
131
|
+
plugins: html.map((h)=>new HtmlPlugin(h))
|
|
132
|
+
});
|
|
133
|
+
})({
|
|
23
134
|
config,
|
|
24
135
|
unpackConfig,
|
|
25
136
|
mergeConfig,
|
|
26
|
-
HtmlPlugin
|
|
27
|
-
})
|
|
28
|
-
if (isDev() && clickToComponent) config = await applyClickToComponentConfig({
|
|
137
|
+
HtmlPlugin: isRspack ? rspack.HtmlRspackPlugin : HtmlWebpackPlugin
|
|
138
|
+
}), isDev() && clickToComponent && (config = await applyClickToComponentConfig({
|
|
29
139
|
config,
|
|
30
140
|
unpackConfig
|
|
31
|
-
})
|
|
32
|
-
|
|
141
|
+
})), config = (({ config, unpackConfig })=>{
|
|
142
|
+
if ('split-by-experience' !== unpackConfig.performance.chunkSplit.strategy) return config;
|
|
143
|
+
let currentConfig = config.optimization.splitChunks, extraGroups = {};
|
|
144
|
+
return extraGroups.react = {
|
|
145
|
+
name: 'lib-react',
|
|
146
|
+
test: isDevServer() ? /node_modules[\\/](?:react|react-dom|scheduler|react-refresh|@rspack[\\/]plugin-react-refresh)[\\/]/ : /node_modules[\\/](?:react|react-dom|scheduler)[\\/]/,
|
|
147
|
+
priority: 0
|
|
148
|
+
}, extraGroups.router = {
|
|
149
|
+
name: 'lib-router',
|
|
150
|
+
test: /node_modules[\\/](?:react-router|react-router-dom|history|@remix-run[\\/]router)[\\/]/,
|
|
151
|
+
priority: 0
|
|
152
|
+
}, config.optimization.splitChunks = {
|
|
153
|
+
...currentConfig,
|
|
154
|
+
cacheGroups: {
|
|
155
|
+
...extraGroups,
|
|
156
|
+
...currentConfig.cacheGroups
|
|
157
|
+
}
|
|
158
|
+
}, config;
|
|
159
|
+
})({
|
|
33
160
|
config,
|
|
34
161
|
unpackConfig
|
|
35
162
|
});
|
|
36
|
-
|
|
37
|
-
logger.error('dependency "react-compiler-runtime" not found, did you install it?');
|
|
38
|
-
process.exit(1);
|
|
39
|
-
}
|
|
40
|
-
const jsExclude = [
|
|
163
|
+
let jsExclude = [
|
|
41
164
|
/node_modules[\\/](?!\.unpack)/
|
|
42
|
-
]
|
|
43
|
-
const swcLoader = isRspack ? 'builtin:swc-loader' : require.resolve('swc-loader');
|
|
44
|
-
const ReactRefreshPlugin = isRspack ? plugin_react_refresh : react_refresh_webpack_plugin;
|
|
45
|
-
const getSwcOptions = ({ tsx })=>({
|
|
165
|
+
], swcLoader = isRspack ? 'builtin:swc-loader' : require.resolve('swc-loader'), getSwcOptions = ({ tsx })=>({
|
|
46
166
|
jsc: {
|
|
47
167
|
parser: {
|
|
48
168
|
tsx,
|
|
49
169
|
syntax: "typescript",
|
|
50
|
-
dynamicImport:
|
|
51
|
-
decorators:
|
|
170
|
+
dynamicImport: !0,
|
|
171
|
+
decorators: !0
|
|
52
172
|
},
|
|
53
|
-
externalHelpers:
|
|
173
|
+
externalHelpers: !0,
|
|
54
174
|
transform: {
|
|
55
175
|
react: {
|
|
56
176
|
runtime: 'automatic',
|
|
@@ -72,7 +192,7 @@ const pluginReact = (options = {})=>{
|
|
|
72
192
|
{
|
|
73
193
|
loader: swcLoader,
|
|
74
194
|
options: getSwcOptions({
|
|
75
|
-
tsx:
|
|
195
|
+
tsx: !1
|
|
76
196
|
})
|
|
77
197
|
}
|
|
78
198
|
]
|
|
@@ -84,16 +204,10 @@ const pluginReact = (options = {})=>{
|
|
|
84
204
|
{
|
|
85
205
|
loader: swcLoader,
|
|
86
206
|
options: getSwcOptions({
|
|
87
|
-
tsx:
|
|
207
|
+
tsx: !0
|
|
88
208
|
})
|
|
89
|
-
},
|
|
90
|
-
reactCompiler && {
|
|
91
|
-
loader: node_path.resolve(src_dirname, './react-compiler-loader/index.cjs'),
|
|
92
|
-
options: {
|
|
93
|
-
target: getUserDepVersion(unpackConfig.root, 'react').split('.')[0]
|
|
94
|
-
}
|
|
95
209
|
}
|
|
96
|
-
]
|
|
210
|
+
]
|
|
97
211
|
},
|
|
98
212
|
{
|
|
99
213
|
test: /\.svg$/i,
|
|
@@ -115,8 +229,8 @@ const pluginReact = (options = {})=>{
|
|
|
115
229
|
]
|
|
116
230
|
},
|
|
117
231
|
plugins: [
|
|
118
|
-
isDevServer() && new
|
|
119
|
-
overlay:
|
|
232
|
+
isDevServer() && new (isRspack ? plugin_react_refresh : react_refresh_webpack_plugin)({
|
|
233
|
+
overlay: !1
|
|
120
234
|
})
|
|
121
235
|
].filter(Boolean),
|
|
122
236
|
resolve: {
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
let COVER_ID = '__click-to-component__', cover = null, currentDebugSource = null, enabled = !1;
|
|
2
|
+
function debounce(fn, delay = 200) {
|
|
3
|
+
let timer = null;
|
|
4
|
+
return (...args)=>{
|
|
5
|
+
clearTimeout(timer), timer = setTimeout(()=>{
|
|
6
|
+
fn(...args);
|
|
7
|
+
}, delay);
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
function applyStyles(target, styles) {
|
|
11
|
+
Object.entries(styles).forEach(([key, value])=>{
|
|
12
|
+
target.style[key] = value;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
function createCover() {
|
|
16
|
+
(cover = document.createElement('div')).id = '__click-to-component__', applyStyles(cover, {
|
|
17
|
+
position: 'fixed',
|
|
18
|
+
zIndex: '999999',
|
|
19
|
+
background: 'rgba(111, 168, 220, 0.5)',
|
|
20
|
+
display: 'none',
|
|
21
|
+
pointerEvents: 'none'
|
|
22
|
+
}), document.body.appendChild(cover);
|
|
23
|
+
}
|
|
24
|
+
function updateCover(target) {
|
|
25
|
+
if ('function' != typeof target.getBoundingClientRect) {
|
|
26
|
+
currentDebugSource = null;
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
let { top, left, width, height } = target.getBoundingClientRect();
|
|
30
|
+
applyStyles(cover, {
|
|
31
|
+
top: top + 'px',
|
|
32
|
+
left: left + 'px',
|
|
33
|
+
width: width + 'px',
|
|
34
|
+
height: height + 'px',
|
|
35
|
+
display: 'block'
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function hideCover() {
|
|
39
|
+
cover.style.display = 'none';
|
|
40
|
+
}
|
|
41
|
+
function getFiberWithDebugSourceByTarget(target) {
|
|
42
|
+
let fiberKey = Object.keys(target).find((key)=>key.includes('__reactFiber')), doNext = (fiber, depth = 0)=>{
|
|
43
|
+
if (fiber && !(depth > 10)) {
|
|
44
|
+
if (fiber._debugSource) return fiber;
|
|
45
|
+
if (fiber._debugOwner) return doNext(fiber._debugOwner, depth + 1);
|
|
46
|
+
console.warn('[WARN] click-to-component cannot work with the minified version of React.');
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
return doNext(target[fiberKey]);
|
|
50
|
+
}
|
|
51
|
+
function getClosestNodeFromFiber(fiber) {
|
|
52
|
+
return fiber.stateNode ? fiber.stateNode : getClosestNodeFromFiber(fiber.return);
|
|
53
|
+
}
|
|
54
|
+
function openEditor() {
|
|
55
|
+
if (!currentDebugSource) return;
|
|
56
|
+
let port = window.CLICK_TO_COMPONENT.port, { fileName, lineNumber, columnNumber } = currentDebugSource, url = [
|
|
57
|
+
'http://localhost:',
|
|
58
|
+
port,
|
|
59
|
+
'/?file=',
|
|
60
|
+
fileName,
|
|
61
|
+
'&line=',
|
|
62
|
+
lineNumber,
|
|
63
|
+
'&column=',
|
|
64
|
+
columnNumber
|
|
65
|
+
].join(''), xhr = new XMLHttpRequest();
|
|
66
|
+
xhr.open('GET', url, !0), xhr.send(), xhr.addEventListener('error', ()=>{
|
|
67
|
+
document.createElement('img').src = url;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
document.addEventListener('mouseover', debounce((e)=>{
|
|
71
|
+
if (!enabled) return;
|
|
72
|
+
let fiber = getFiberWithDebugSourceByTarget(e.target);
|
|
73
|
+
if (!fiber) return;
|
|
74
|
+
let closestNode = getClosestNodeFromFiber(fiber);
|
|
75
|
+
closestNode && (currentDebugSource = fiber._debugSource, updateCover(closestNode));
|
|
76
|
+
}, 30)), document.addEventListener('click', (e)=>{
|
|
77
|
+
enabled && (e.stopPropagation(), e.preventDefault(), openEditor(), hideCover(), enabled = !1);
|
|
78
|
+
}, !0), document.addEventListener('keydown', (e)=>{
|
|
79
|
+
'Alt' === e.key && (enabled = !0);
|
|
80
|
+
}), document.addEventListener('keyup', (e)=>{
|
|
81
|
+
'Alt' === e.key && (enabled = !1, hideCover());
|
|
82
|
+
}), createCover();
|