@unpackjs/plugin-react 3.2.4 → 3.2.5

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 CHANGED
@@ -62,7 +62,7 @@ if (!document.querySelector('#__click-to-component__')) {
62
62
  external_node_fs_default().writeFileSync(filePath, `${scriptTpl}\n${content}`, 'utf-8');
63
63
  });
64
64
  else {
65
- let realEntryPath = external_node_path_default().resolve(config.context, core_namespaceObject.TEMP_DIR, 'entries', `index.${port}.jsx`), tpl = `
65
+ let realEntryPath = external_node_path_default().resolve(config.context, unpackConfig._context.cachePath, 'entries', `index.${port}.jsx`), tpl = `
66
66
  ${scriptTpl}
67
67
  import '${(0, core_namespaceObject.getPathInJs)(external_node_path_default().resolve(Object.values(config.entry)[0]))}'
68
68
  `, dir = external_node_path_default().dirname(realEntryPath);
@@ -74,7 +74,7 @@ import '${(0, core_namespaceObject.getPathInJs)(external_node_path_default().res
74
74
  }
75
75
  return config;
76
76
  }, applyMpaConfig = ({ config, unpackConfig })=>{
77
- let start, entry, html, pagesRoot, tempDirectory = external_node_path_default().join(core_namespaceObject.TEMP_DIR, 'mpa'), userOptions = (0, core_namespaceObject.isPlainObject)(unpackConfig.mpa) ? unpackConfig.mpa : {}, { entry: entry1, html: html1 } = (start = performance.now(), entry = {}, html = [], pagesRoot = external_node_path_default().join(unpackConfig.root, 'src', 'pages'), external_node_fs_default().readdirSync(pagesRoot).forEach((filename)=>{
77
+ let start, entry, html, pagesRoot, tempDirectory = external_node_path_default().join(unpackConfig._context.cachePath, 'mpa'), userOptions = (0, core_namespaceObject.isPlainObject)(unpackConfig.mpa) ? unpackConfig.mpa : {}, { entry: entry1, html: html1 } = (start = performance.now(), entry = {}, html = [], pagesRoot = external_node_path_default().join(unpackConfig.root, 'src', 'pages'), external_node_fs_default().readdirSync(pagesRoot).forEach((filename)=>{
78
78
  if (filename.startsWith('.')) return;
79
79
  let indexFilePath = ((dir)=>{
80
80
  for (let extension of [
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ 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 plugin_react_refresh from "@rspack/plugin-react-refresh";
5
- import { TEMPLATE_CONTENT, TEMP_DIR, addRestartCleaner, colors, esVersionToBrowserslist, getPathInJs, getPort, isDev, isDevServer, isFunction, isPlainObject, launchEditor, logger, mergeConfig as core_mergeConfig, removeDir, rspack } from "@unpackjs/core";
5
+ import { TEMPLATE_CONTENT, addRestartCleaner, colors, esVersionToBrowserslist, getPathInJs, getPort, isDev, isDevServer, isFunction, isPlainObject, launchEditor, logger, mergeConfig as core_mergeConfig, removeDir, rspack } from "@unpackjs/core";
6
6
  import node_fs from "node:fs";
7
7
  import node_http from "node:http";
8
8
  var click_to_component_dirname = __webpack_dirname__(__webpack_fileURLToPath__(import.meta.url));
@@ -37,7 +37,7 @@ if (!document.querySelector('#__click-to-component__')) {
37
37
  node_fs.writeFileSync(filePath, `${scriptTpl}\n${content}`, 'utf-8');
38
38
  });
39
39
  else {
40
- let realEntryPath = node_path.resolve(config.context, TEMP_DIR, 'entries', `index.${port}.jsx`), tpl = `
40
+ let realEntryPath = node_path.resolve(config.context, unpackConfig._context.cachePath, 'entries', `index.${port}.jsx`), tpl = `
41
41
  ${scriptTpl}
42
42
  import '${getPathInJs(node_path.resolve(Object.values(config.entry)[0]))}'
43
43
  `, dir = node_path.dirname(realEntryPath);
@@ -55,7 +55,7 @@ import '${getPathInJs(node_path.resolve(Object.values(config.entry)[0]))}'
55
55
  bundlerConfig: async (originalConfig, { unpackConfig, mergeConfig })=>{
56
56
  let config = originalConfig;
57
57
  unpackConfig.mpa && (config = (({ config, unpackConfig })=>{
58
- let start, entry, html, pagesRoot, tempDirectory = node_path.join(TEMP_DIR, 'mpa'), userOptions = isPlainObject(unpackConfig.mpa) ? unpackConfig.mpa : {}, { entry: entry1, html: html1 } = (start = performance.now(), entry = {}, html = [], pagesRoot = node_path.join(unpackConfig.root, 'src', 'pages'), node_fs.readdirSync(pagesRoot).forEach((filename)=>{
58
+ let start, entry, html, pagesRoot, tempDirectory = node_path.join(unpackConfig._context.cachePath, 'mpa'), userOptions = isPlainObject(unpackConfig.mpa) ? unpackConfig.mpa : {}, { entry: entry1, html: html1 } = (start = performance.now(), entry = {}, html = [], pagesRoot = node_path.join(unpackConfig.root, 'src', 'pages'), node_fs.readdirSync(pagesRoot).forEach((filename)=>{
59
59
  if (filename.startsWith('.')) return;
60
60
  let indexFilePath = ((dir)=>{
61
61
  for (let extension of [
@@ -1,75 +1,113 @@
1
- const COVER_ID = '__click-to-component__';
2
- let cover = null, currentDebugSource = null, enabled = !1;
3
- function debounce(fn, delay = 200) {
4
- let timer = null;
5
- return (...args)=>{
6
- clearTimeout(timer), timer = setTimeout(()=>{
7
- fn(...args);
8
- }, delay);
9
- };
10
- }
11
- function applyStyles(target, styles) {
12
- Object.entries(styles).forEach(([key, value])=>{
13
- target.style[key] = value;
14
- });
15
- }
16
- function createCover() {
17
- (cover = document.createElement('div')).id = '__click-to-component__', applyStyles(cover, {
18
- position: 'fixed',
19
- zIndex: '999999',
20
- background: 'rgba(111, 168, 220, 0.5)',
21
- display: 'none',
22
- pointerEvents: 'none'
23
- }), document.body.appendChild(cover);
24
- }
25
- function updateCover(target) {
26
- if ('function' != typeof target.getBoundingClientRect) {
27
- currentDebugSource = null;
28
- return;
1
+ !function() {
2
+ let cover = null, nameTag = null, currentDebugSource = null, cachedFiberKey = null, isEnabled = !1, debounceTimer = null;
3
+ function applyStyles(target, styles) {
4
+ for(let key in styles)target.style[key] = styles[key];
5
+ }
6
+ function hideCover() {
7
+ cover && (cover.style.display = 'none');
29
8
  }
30
- let { top, left, width, height } = target.getBoundingClientRect();
31
- applyStyles(cover, {
32
- top: top + 'px',
33
- left: left + 'px',
34
- width: width + 'px',
35
- height: height + 'px',
36
- display: 'block'
37
- });
38
- }
39
- function hideCover() {
40
- cover.style.display = 'none';
41
- }
42
- function getFiberWithDebugSourceByTarget(target) {
43
- let fiberKey = Object.keys(target).find((key)=>key.includes('__reactFiber')), doNext = (fiber, depth = 0)=>{
44
- if (fiber && !(depth > 10)) {
45
- if (fiber._debugSource) return fiber;
46
- if (fiber._debugOwner) return doNext(fiber._debugOwner, depth + 1);
47
- console.warn('[WARN] click-to-component cannot work with the minified version of React.');
48
- }
9
+ let handleClick = (e)=>{
10
+ 0 === e.button && (e.preventDefault(), e.stopPropagation(), function() {
11
+ if (!currentDebugSource) return;
12
+ let port = window.CLICK_TO_COMPONENT?.port, { fileName, lineNumber, columnNumber } = currentDebugSource, url = new URL('http://localhost:' + port + '/');
13
+ url.searchParams.set('file', fileName), url.searchParams.set('line', lineNumber), url.searchParams.set('column', columnNumber), fetch(url.toString()).catch(()=>{
14
+ new Image().src = url.toString();
15
+ });
16
+ }(), stopInspect());
17
+ }, debouncedMouseOver = (e)=>{
18
+ debounceTimer && clearTimeout(debounceTimer), debounceTimer = setTimeout(()=>{
19
+ ((e)=>{
20
+ if (!isEnabled) return;
21
+ let target = e.composedPath && e.composedPath().length > 0 ? e.composedPath()[0] : e.target;
22
+ if (3 === target.nodeType && (target = target.parentElement), !target || 1 !== target.nodeType) return;
23
+ let originalFiber = function(target) {
24
+ if (!target || 'object' != typeof target) return null;
25
+ if (cachedFiberKey && target[cachedFiberKey]) return target[cachedFiberKey];
26
+ try {
27
+ cachedFiberKey = Object.keys(target).find((key)=>key.startsWith('__reactFiber$'));
28
+ } catch {
29
+ return null;
30
+ }
31
+ return target[cachedFiberKey];
32
+ }(target);
33
+ if (!originalFiber) return;
34
+ let debugFiber = function(fiber) {
35
+ let curr = fiber;
36
+ for(; curr;){
37
+ if (curr._debugSource) return curr;
38
+ curr = curr.return || curr._debugOwner;
39
+ }
40
+ return null;
41
+ }(originalFiber);
42
+ if (!debugFiber) return;
43
+ let highlightNode = function findDOMOfFiber(fiber) {
44
+ return fiber ? fiber.stateNode instanceof Element ? fiber.stateNode : fiber.child ? findDOMOfFiber(fiber.child) : null : null;
45
+ }(debugFiber), displayText = function(source) {
46
+ if (!source || !source.fileName) return 'Unknown Source';
47
+ let { fileName, lineNumber, columnNumber } = source, parts = fileName.split(/[\\/]/);
48
+ return (parts.length > 3 ? parts.slice(-3).join('/') : parts.join('/')) + ':' + lineNumber + ':' + columnNumber;
49
+ }(currentDebugSource = debugFiber._debugSource);
50
+ !function(rect, text) {
51
+ if (!rect) return hideCover();
52
+ let el = (cover || ((cover = document.createElement('div')).id = '__click-to-component-cover__', applyStyles(cover, {
53
+ position: 'fixed',
54
+ zIndex: 2147483647,
55
+ background: 'rgba(111, 168, 220, 0.3)',
56
+ boxSizing: 'border-box',
57
+ display: 'none',
58
+ pointerEvents: 'none',
59
+ transition: 'all 0.2s cubic-bezier(0.25, 1, 0.5, 1)',
60
+ boxShadow: 'inset 0 0 0 1px #4a90e2, 0 0 4px rgba(74, 144, 226, 0.5)'
61
+ }), (nameTag = document.createElement('span')).id = '__click-to-component-name__', applyStyles(nameTag, {
62
+ position: 'absolute',
63
+ bottom: '0',
64
+ right: '0',
65
+ background: '#2d3748',
66
+ color: '#fff',
67
+ fontSize: '12px',
68
+ fontFamily: 'Consolas, Monaco, "Andale Mono", monospace',
69
+ fontWeight: 'bold',
70
+ padding: '4px 8px',
71
+ pointerEvents: 'none',
72
+ whiteSpace: 'nowrap',
73
+ zIndex: 2147483648,
74
+ boxShadow: '0 4px 6px rgba(0, 0, 0, 0.3)'
75
+ }), cover.appendChild(nameTag), document.documentElement.appendChild(cover)), cover);
76
+ applyStyles(el, {
77
+ top: rect.top + 'px',
78
+ left: rect.left + 'px',
79
+ width: rect.width + 'px',
80
+ height: rect.height + 'px',
81
+ display: 'block'
82
+ }), nameTag.innerText = text, nameTag.style.left = 'auto', nameTag.style.right = 'auto', nameTag.style.top = 'auto', nameTag.style.bottom = 'auto';
83
+ let tagWidth = nameTag.offsetWidth, viewportHeight = window.innerHeight;
84
+ rect.right - tagWidth < 0 ? nameTag.style.left = '0' : nameTag.style.right = '0';
85
+ rect.height < 50 || rect.width < tagWidth + 20 ? rect.bottom + 28 <= viewportHeight ? nameTag.style.bottom = "-28px" : nameTag.style.top = "-28px" : nameTag.style.bottom = '0';
86
+ }(highlightNode.getBoundingClientRect(), displayText);
87
+ })(e);
88
+ }, 30);
49
89
  };
50
- return doNext(target[fiberKey]);
51
- }
52
- function getClosestNodeFromFiber(fiber) {
53
- return fiber.stateNode ? fiber.stateNode : getClosestNodeFromFiber(fiber.return);
54
- }
55
- function openEditor() {
56
- let xhr;
57
- if (!currentDebugSource) return;
58
- let port = window.CLICK_TO_COMPONENT.port, { fileName, lineNumber, columnNumber } = currentDebugSource, url = "http://localhost:" + port + "/?file=" + fileName + "&line=" + lineNumber + "&column=" + columnNumber;
59
- (xhr = new XMLHttpRequest()).open('GET', url, !0), xhr.send(), xhr.addEventListener('error', ()=>{
60
- document.createElement('img').src = url;
61
- });
62
- }
63
- document.addEventListener('mouseover', debounce((e)=>{
64
- if (!enabled) return;
65
- let fiber = getFiberWithDebugSourceByTarget(e.target);
66
- if (!fiber) return;
67
- let closestNode = getClosestNodeFromFiber(fiber);
68
- closestNode && (currentDebugSource = fiber._debugSource, updateCover(closestNode));
69
- }, 30)), document.addEventListener('click', (e)=>{
70
- enabled && (e.stopPropagation(), e.preventDefault(), openEditor(), hideCover(), enabled = !1);
71
- }, !0), document.addEventListener('keydown', (e)=>{
72
- 'Alt' === e.key && (enabled = !0);
73
- }), document.addEventListener('keyup', (e)=>{
74
- 'Alt' === e.key && (enabled = !1, hideCover());
75
- }), createCover();
90
+ function stopInspect() {
91
+ isEnabled && (isEnabled = !1, document.body.style.cursor = '', debounceTimer && clearTimeout(debounceTimer), hideCover(), currentDebugSource = null, document.removeEventListener('mouseover', debouncedMouseOver, {
92
+ capture: !0
93
+ }), document.removeEventListener('click', handleClick, {
94
+ capture: !0
95
+ }), window.removeEventListener('scroll', hideCover, {
96
+ capture: !0
97
+ }), window.removeEventListener('resize', hideCover));
98
+ }
99
+ document.addEventListener('keydown', (e)=>{
100
+ ('Alt' === e.key || 'Option' === e.key) && (isEnabled || (isEnabled = !0, document.body.style.cursor = 'crosshair', document.addEventListener('mouseover', debouncedMouseOver, {
101
+ capture: !0
102
+ }), document.addEventListener('click', handleClick, {
103
+ capture: !0
104
+ }), window.addEventListener('scroll', hideCover, {
105
+ capture: !0,
106
+ passive: !0
107
+ }), window.addEventListener('resize', hideCover, {
108
+ passive: !0
109
+ })));
110
+ }), document.addEventListener('keyup', (e)=>{
111
+ ('Alt' === e.key || 'Option' === e.key) && stopInspect();
112
+ }), window.addEventListener('blur', stopInspect), console.log('%c[Click-To-Component] Ready.', 'color: #888');
113
+ }();
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/click-to-component/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,KAAK,oBAAoB,EAKzB,KAAK,YAAY,EAClB,MAAM,gBAAgB,CAAA;AA4BvB,eAAO,MAAM,2BAA2B,GAAU,2BAG/C;IACD,MAAM,EAAE,oBAAoB,CAAA;IAC5B,YAAY,EAAE,YAAY,CAAA;CAC3B,KAAG,OAAO,CAAC,oBAAoB,CA+B/B,CAAA"}
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,2BAG/C;IACD,MAAM,EAAE,oBAAoB,CAAA;IAC5B,YAAY,EAAE,YAAY,CAAA;CAC3B,KAAG,OAAO,CAAC,oBAAoB,CAoC/B,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"mpa.d.ts","sourceRoot":"","sources":["../src/mpa.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,oBAAoB,EAYzB,KAAK,YAAY,EAClB,MAAM,gBAAgB,CAAA;AAYvB,eAAO,MAAM,cAAc,GAAI,2BAG5B;IACD,MAAM,EAAE,oBAAoB,CAAA;IAC5B,YAAY,EAAE,YAAY,CAAA;CAC3B,KAAG,oBA2GH,CAAA"}
1
+ {"version":3,"file":"mpa.d.ts","sourceRoot":"","sources":["../src/mpa.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,oBAAoB,EAWzB,KAAK,YAAY,EAClB,MAAM,gBAAgB,CAAA;AAYvB,eAAO,MAAM,cAAc,GAAI,2BAG5B;IACD,MAAM,EAAE,oBAAoB,CAAA;IAC5B,YAAY,EAAE,YAAY,CAAA;CAC3B,KAAG,oBA2GH,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unpackjs/plugin-react",
3
- "version": "3.2.4",
3
+ "version": "3.2.5",
4
4
  "description": "React plugin for Unpack",
5
5
  "type": "module",
6
6
  "exports": {
@@ -30,7 +30,7 @@
30
30
  "react-refresh": "0.18.0"
31
31
  },
32
32
  "devDependencies": {
33
- "@unpackjs/core": "3.5.0"
33
+ "@unpackjs/core": "3.5.4"
34
34
  },
35
35
  "peerDependencies": {
36
36
  "@unpackjs/core": "3.x"