@unpackjs/plugin-react 3.2.3 → 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 [
@@ -102,7 +102,7 @@ import '${(0, core_namespaceObject.getPathInJs)(external_node_path_default().res
102
102
  ],
103
103
  ...pageConfig1,
104
104
  templateParameters: {
105
- mountId: unpackConfig.html?.mountId,
105
+ mountId: unpackConfig.html?.mountId || '',
106
106
  title: (0, core_namespaceObject.isFunction)(unpackConfig.html?.title) ? unpackConfig.html?.title({
107
107
  entryName
108
108
  }) : unpackConfig.html?.title || '',
@@ -137,7 +137,9 @@ ${renderer}
137
137
  plugins: html1.map((h)=>new core_namespaceObject.rspack.HtmlRspackPlugin(h))
138
138
  });
139
139
  }, applySplitChunksConfig = ({ config })=>{
140
- let currentConfig = config.optimization.splitChunks, extraGroups = {};
140
+ let currentSplitChunks = config.optimization?.splitChunks;
141
+ if (!currentSplitChunks) return config;
142
+ let extraGroups = {};
141
143
  return extraGroups.react = {
142
144
  name: 'lib-react',
143
145
  test: (0, core_namespaceObject.isDevServer)() ? /node_modules[\\/](?:react|react-dom|scheduler|react-refresh|@rspack[\\/]plugin-react-refresh)[\\/]/ : /node_modules[\\/](?:react|react-dom|scheduler)[\\/]/,
@@ -147,10 +149,10 @@ ${renderer}
147
149
  test: /node_modules[\\/](?:react-router|react-router-dom|history|@remix-run[\\/]router)[\\/]/,
148
150
  priority: 0
149
151
  }, config.optimization.splitChunks = {
150
- ...currentConfig,
152
+ ...currentSplitChunks,
151
153
  cacheGroups: {
152
154
  ...extraGroups,
153
- ...currentConfig.cacheGroups
155
+ ...currentSplitChunks.cacheGroups ?? {}
154
156
  }
155
157
  }, config;
156
158
  }, PLUGIN_NAME = 'unpack:react', pluginReact = (options = {})=>{
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 [
@@ -83,7 +83,7 @@ import '${getPathInJs(node_path.resolve(Object.values(config.entry)[0]))}'
83
83
  ],
84
84
  ...pageConfig1,
85
85
  templateParameters: {
86
- mountId: unpackConfig.html?.mountId,
86
+ mountId: unpackConfig.html?.mountId || '',
87
87
  title: isFunction(unpackConfig.html?.title) ? unpackConfig.html?.title({
88
88
  entryName
89
89
  }) : unpackConfig.html?.title || '',
@@ -124,7 +124,9 @@ ${renderer}
124
124
  config,
125
125
  unpackConfig
126
126
  })), unpackConfig.performance?.chunkSplit?.strategy === 'split-by-experience' && (config = (({ config })=>{
127
- let currentConfig = config.optimization.splitChunks, extraGroups = {};
127
+ let currentSplitChunks = config.optimization?.splitChunks;
128
+ if (!currentSplitChunks) return config;
129
+ let extraGroups = {};
128
130
  return extraGroups.react = {
129
131
  name: 'lib-react',
130
132
  test: isDevServer() ? /node_modules[\\/](?:react|react-dom|scheduler|react-refresh|@rspack[\\/]plugin-react-refresh)[\\/]/ : /node_modules[\\/](?:react|react-dom|scheduler)[\\/]/,
@@ -134,10 +136,10 @@ ${renderer}
134
136
  test: /node_modules[\\/](?:react-router|react-router-dom|history|@remix-run[\\/]router)[\\/]/,
135
137
  priority: 0
136
138
  }, config.optimization.splitChunks = {
137
- ...currentConfig,
139
+ ...currentSplitChunks,
138
140
  cacheGroups: {
139
141
  ...extraGroups,
140
- ...currentConfig.cacheGroups
142
+ ...currentSplitChunks.cacheGroups ?? {}
141
143
  }
142
144
  }, config;
143
145
  })({
@@ -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,oBA4GH,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"}
@@ -1 +1 @@
1
- {"version":3,"file":"splitChunks.d.ts","sourceRoot":"","sources":["../src/splitChunks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,oBAAoB,EAAiC,MAAM,gBAAgB,CAAA;AAEzF,eAAO,MAAM,sBAAsB,GAAI,YAAY;IAAE,MAAM,EAAE,oBAAoB,CAAA;CAAE,yCAwBlF,CAAA"}
1
+ {"version":3,"file":"splitChunks.d.ts","sourceRoot":"","sources":["../src/splitChunks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,oBAAoB,EAAiC,MAAM,gBAAgB,CAAA;AAEzF,eAAO,MAAM,sBAAsB,GAAI,YAAY;IAAE,MAAM,EAAE,oBAAoB,CAAA;CAAE,yCA4BlF,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unpackjs/plugin-react",
3
- "version": "3.2.3",
3
+ "version": "3.2.5",
4
4
  "description": "React plugin for Unpack",
5
5
  "type": "module",
6
6
  "exports": {
@@ -24,13 +24,13 @@
24
24
  "access": "public"
25
25
  },
26
26
  "dependencies": {
27
- "@rspack/plugin-react-refresh": "1.5.2",
27
+ "@rspack/plugin-react-refresh": "1.5.3",
28
28
  "@svgr/webpack": "8.1.0",
29
29
  "@swc/helpers": "0.5.17",
30
30
  "react-refresh": "0.18.0"
31
31
  },
32
32
  "devDependencies": {
33
- "@unpackjs/core": "3.3.5"
33
+ "@unpackjs/core": "3.5.4"
34
34
  },
35
35
  "peerDependencies": {
36
36
  "@unpackjs/core": "3.x"