@unpackjs/plugin-react 3.3.4 → 3.4.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 CHANGED
@@ -2,61 +2,18 @@ import __rslib_shim_module__ from '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 node_http from "node:http";
9
- var click_to_component_dirname = __rspack_dirname(__rspack_fileURLToPath(import.meta.url));
10
- let applyClickToComponentConfig = async ({ config, unpackConfig, cachePath })=>{
11
- let { port } = await new Promise((resolve)=>{
12
- let server = node_http.createServer((req, res)=>{
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 = {})=>{
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 = (options = {})=>{
53
10
  let { clickToComponent } = options;
54
11
  return {
55
- name: PLUGIN_NAME,
12
+ name: 'unpack:react',
56
13
  bundlerConfig: async (originalConfig, { unpackConfig, mergeConfig, cachePath })=>{
57
14
  let config = originalConfig;
58
15
  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)=>{
16
+ 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
17
  if (filename.startsWith('.')) return;
61
18
  let indexFilePath = ((dir)=>{
62
19
  for (let extension of [
@@ -69,28 +26,23 @@ import '${getPathInJs(node_path.resolve(Object.values(config.entry)[0]))}'
69
26
  return null;
70
27
  })(node_path.join(pagesRoot, filename));
71
28
  if (indexFilePath) {
72
- let filePath, pageConfig, pageConfig1 = (filePath = node_path.join(indexFilePath, '../config.json'), pageConfig = {}, node_fs.existsSync(filePath) && (pageConfig = JSON.parse(node_fs.readFileSync(filePath, 'utf-8'))), pageConfig), entryName = !0 === userOptions.lowerCase ? filename.toLowerCase() : filename;
73
- pageConfig1.filename && (entryName = pageConfig1.filename.slice(0, -5)), entry[entryName] = {
29
+ let entryName = !0 === userOptions.lowerCase ? filename.toLowerCase() : filename;
30
+ entry[entryName] = {
74
31
  import: [
75
32
  indexFilePath
76
33
  ]
77
- }, pageConfig1.template && (pageConfig1.template = node_path.join(indexFilePath, '../', pageConfig1.template)), html.push({
78
- template: unpackConfig.html?.template || void 0,
79
- templateContent: unpackConfig.html?.templateContent || (unpackConfig.html?.template ? void 0 : TEMPLATE_CONTENT),
80
- minify: !1,
34
+ }, html.push({
81
35
  filename: `${entryName}.html`,
82
36
  chunks: [
83
37
  entryName
84
38
  ],
85
- ...pageConfig1,
86
39
  templateParameters: {
87
40
  mountId: unpackConfig.html?.mountId || '',
88
41
  title: isFunction(unpackConfig.html?.title) ? unpackConfig.html?.title({
89
42
  entryName
90
43
  }) : unpackConfig.html?.title || '',
91
44
  headTag: unpackConfig.html?.headTag || '',
92
- ...unpackConfig.html?.templateParameters,
93
- ...pageConfig1.templateParameters
45
+ ...unpackConfig.html?.templateParameters
94
46
  }
95
47
  });
96
48
  }
@@ -116,16 +68,17 @@ ${renderer}
116
68
  }), node_fs.writeFileSync(filePath, tpl, 'utf-8'), entryConfig.import[0] = filePath;
117
69
  }), entry;
118
70
  })(entry1),
119
- plugins: html1.map((h)=>new rspack.HtmlRspackPlugin(h))
71
+ plugins: html1.map((h)=>new rspack.HtmlRspackPlugin({
72
+ ...h,
73
+ template,
74
+ templateContent,
75
+ minify: !1
76
+ }))
120
77
  });
121
78
  })({
122
79
  config,
123
80
  unpackConfig,
124
81
  cachePath
125
- })), isDev() && clickToComponent && (config = await applyClickToComponentConfig({
126
- config,
127
- unpackConfig,
128
- cachePath
129
82
  }));
130
83
  let jsExclude = [
131
84
  /node_modules[\\/](?!\.unpack)/
@@ -153,7 +106,7 @@ ${renderer}
153
106
  module: {
154
107
  rules: [
155
108
  {
156
- test: /\.(j|t)s$/,
109
+ test: JS_REGEX,
157
110
  exclude: jsExclude,
158
111
  use: [
159
112
  {
@@ -165,7 +118,7 @@ ${renderer}
165
118
  ]
166
119
  },
167
120
  {
168
- test: /\.(j|t)sx$/,
121
+ test: JSX_REGEX,
169
122
  exclude: jsExclude,
170
123
  use: [
171
124
  {
@@ -183,7 +136,7 @@ ${renderer}
183
136
  },
184
137
  {
185
138
  test: /\.svg$/i,
186
- issuer: /\.[jt]sx?$/,
139
+ issuer: SCRIPT_REGEX,
187
140
  resourceQuery: {
188
141
  not: [
189
142
  /url/
@@ -206,7 +159,24 @@ ${renderer}
206
159
  }
207
160
  }
208
161
  });
162
+ },
163
+ setupMiddlewares: (middlewares)=>isDevServer() && clickToComponent ? [
164
+ (req, res, next)=>{
165
+ if (!req.url) return void next();
166
+ let url = new URL(req.url, 'http://localhost');
167
+ if ('/__click_to_component__' !== url.pathname) return void next();
168
+ let file = url.searchParams.get('file'), line = Number(url.searchParams.get('line')), column = Number(url.searchParams.get('column'));
169
+ res.writeHead(200, {
170
+ 'Content-Type': 'text/plain'
171
+ }), res.end('ok'), launchEditor(`${decodeURIComponent(file)}:${line}:${column}`);
172
+ },
173
+ ...middlewares
174
+ ] : middlewares,
175
+ transformHtml: (html)=>{
176
+ if (!isDevServer() || !clickToComponent) return html;
177
+ let injectScriptPath = node_path.resolve(src_dirname, 'injectScript.js'), injectScript = node_fs.readFileSync(injectScriptPath, 'utf-8');
178
+ return injectToHead(html, `<script>${injectScript}</script>`);
209
179
  }
210
180
  };
211
181
  };
212
- export { PLUGIN_NAME, pluginReact };
182
+ export { pluginReact };
@@ -1,4 +1,4 @@
1
- !function() {
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 port = window.CLICK_TO_COMPONENT?.port, { fileName, lineNumber, columnNumber } = currentDebugSource, url = new URL('http://localhost:' + port + '/');
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), console.log('%c[Click-To-Component] Ready.', 'color: #888');
118
- }();
117
+ }), window.addEventListener('blur', stopInspect);
118
+ })();
@@ -1,5 +1,4 @@
1
1
  import { type UnpackPlugin } from '@unpackjs/core';
2
- export declare const PLUGIN_NAME = "unpack:react";
3
2
  export type PluginReactOptions = {
4
3
  clickToComponent?: boolean;
5
4
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAA+C,KAAK,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAI/F,eAAO,MAAM,WAAW,iBAAiB,CAAA;AAEzC,MAAM,MAAM,kBAAkB,GAAG;IAC/B,gBAAgB,CAAC,EAAE,OAAO,CAAA;CAC3B,CAAA;AACD,eAAO,MAAM,WAAW,GAAI,UAAS,kBAAuB,KAAG,YAuF9D,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAQL,KAAK,YAAY,EAClB,MAAM,gBAAgB,CAAA;AAGvB,MAAM,MAAM,kBAAkB,GAAG;IAC/B,gBAAgB,CAAC,EAAE,OAAO,CAAA;CAC3B,CAAA;AACD,eAAO,MAAM,WAAW,GAAI,UAAS,kBAAuB,KAAG,YAkH9D,CAAA"}
@@ -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;AAYvB,eAAO,MAAM,cAAc,GAAI,sCAI5B;IACD,MAAM,EAAE,oBAAoB,CAAA;IAC5B,YAAY,EAAE,YAAY,CAAA;IAC1B,SAAS,EAAE,MAAM,CAAA;CAClB,KAAG,oBAsGH,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.3.4",
3
+ "version": "3.4.0",
4
4
  "description": "React plugin for Unpack",
5
5
  "type": "module",
6
6
  "exports": {
@@ -24,11 +24,11 @@
24
24
  "dependencies": {
25
25
  "@rspack/plugin-react-refresh": "1.5.3",
26
26
  "@svgr/webpack": "8.1.0",
27
- "@swc/helpers": "0.5.17",
27
+ "@swc/helpers": "0.5.18",
28
28
  "react-refresh": "0.18.0"
29
29
  },
30
30
  "devDependencies": {
31
- "@unpackjs/core": "3.7.7"
31
+ "@unpackjs/core": "3.8.0"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "@unpackjs/core": "3.x"
@@ -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"}