Skip to content

Commit 843c0b6

Browse files
committed
pre-compile error overlay to avoid duplicate react dependencies
1 parent 1ed569c commit 843c0b6

20 files changed

+115
-64
lines changed

gulpfile.js

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const IGNORED_PACKAGES = [
2121
'!packages/core/utils/**',
2222
'!packages/reporters/cli/**',
2323
'!packages/reporters/dev-server/**',
24+
'!packages/utils/error-overlay/**',
2425
];
2526

2627
const paths = {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"scripts": {
1414
"build": "yarn build-bundles && cross-env NODE_ENV=production PARCEL_BUILD_ENV=production gulp",
15-
"build-bundles": "rimraf --glob packages/*/*/lib && cross-env NODE_ENV=production PARCEL_BUILD_ENV=production PARCEL_SELF_BUILD=true parcel build --no-cache packages/core/{fs,codeframe,package-manager,utils} packages/reporters/{cli,dev-server} packages/utils/{parcel-lsp,parcel-lsp-protocol,parcel-watcher-watchman-js}",
15+
"build-bundles": "rimraf --glob packages/*/*/lib && cross-env NODE_ENV=production PARCEL_BUILD_ENV=production PARCEL_SELF_BUILD=true parcel build --no-cache packages/core/{fs,codeframe,package-manager,utils} packages/reporters/{cli,dev-server} packages/utils/{parcel-lsp,parcel-lsp-protocol,parcel-watcher-watchman-js,error-overlay}",
1616
"build-ts": "lerna run build-ts && lerna run check-ts",
1717
"build-native": "node scripts/build-native.js",
1818
"build-native-release": "node scripts/build-native.js --release",

packages/transformers/js/core/src/dependency_collector.rs

+48
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,22 @@ impl<'a> DependencyCollector<'a> {
15101510
if let Some((name, span)) = name {
15111511
match name.as_str() {
15121512
"distDir" => {
1513+
if self.config.is_library {
1514+
return if self.config.is_esm_output {
1515+
Some(Expr::Member(member_expr!(
1516+
Default::default(),
1517+
span,
1518+
import.meta.distDir
1519+
)))
1520+
} else {
1521+
Some(Expr::Member(member_expr!(
1522+
Default::default(),
1523+
span,
1524+
parcelRequire.meta.distDir
1525+
)))
1526+
};
1527+
}
1528+
15131529
self.helpers |= Helpers::DIST_DIR;
15141530
if self.config.scope_hoist {
15151531
Some(Expr::Ident(Ident::new_no_ctxt(
@@ -1525,6 +1541,22 @@ impl<'a> DependencyCollector<'a> {
15251541
}
15261542
}
15271543
"publicUrl" => {
1544+
if self.config.is_library {
1545+
return if self.config.is_esm_output {
1546+
Some(Expr::Member(member_expr!(
1547+
Default::default(),
1548+
span,
1549+
import.meta.publicUrl
1550+
)))
1551+
} else {
1552+
Some(Expr::Member(member_expr!(
1553+
Default::default(),
1554+
span,
1555+
parcelRequire.meta.publicUrl
1556+
)))
1557+
};
1558+
}
1559+
15281560
self.helpers |= Helpers::PUBLIC_URL;
15291561
if self.config.scope_hoist {
15301562
Some(Expr::Ident(Ident::new_no_ctxt(
@@ -1540,6 +1572,22 @@ impl<'a> DependencyCollector<'a> {
15401572
}
15411573
}
15421574
"devServer" => {
1575+
if self.config.is_library {
1576+
return if self.config.is_esm_output {
1577+
Some(Expr::Member(member_expr!(
1578+
Default::default(),
1579+
span,
1580+
import.meta.devServer
1581+
)))
1582+
} else {
1583+
Some(Expr::Member(member_expr!(
1584+
Default::default(),
1585+
span,
1586+
parcelRequire.meta.devServer
1587+
)))
1588+
};
1589+
}
1590+
15431591
self.helpers |= Helpers::DEV_SERVER;
15441592
if self.config.scope_hoist {
15451593
Some(Expr::Ident(Ident::new_no_ctxt(
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "@parcel/eslint-config",
3+
"rules": {
4+
"react/react-in-jsx-scope": "off"
5+
}
6+
}

packages/utils/error-overlay/package.json

+9-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@
1313
"type": "git",
1414
"url": "https://github.com/parcel-bundler/parcel.git"
1515
},
16-
"main": "lib/index.js",
16+
"module": "lib/index.js",
1717
"source": "src/index.js",
1818
"engines": {
1919
"node": ">= 16.0.0"
2020
},
21-
"dependencies": {
21+
"devDependencies": {
2222
"ansi-html-community": "0.0.8",
23-
"react": "^18 || ^19",
24-
"react-dom": "^18 || ^19"
23+
"preact": "^10.26.4"
24+
},
25+
"targets": {
26+
"module": {
27+
"includeNodeModules": true,
28+
"optimize": true
29+
}
2530
}
2631
}

packages/utils/error-overlay/src/components/CloseButton.js

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
/* @flow */
9-
import React from 'react';
109
import {theme} from '../styles';
1110

1211
const closeButtonStyle = {

packages/utils/error-overlay/src/components/CodeBlock.js

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
/* @flow */
9-
import React from 'react';
109
import {theme} from '../styles';
1110

1211
const _preStyle = {

packages/utils/error-overlay/src/components/Collapsible.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
*/
77

88
/* @flow */
9-
import React, {useState} from 'react';
9+
import {useState} from 'preact/hooks';
1010
import {theme} from '../styles';
1111

12-
import type {Element as ReactElement} from 'react';
13-
1412
const _collapsibleStyle = {
1513
cursor: 'pointer',
1614
border: 'none',
@@ -38,7 +36,7 @@ const collapsibleExpandedStyle = {
3836
};
3937

4038
type CollapsiblePropsType = {|
41-
children: ReactElement<any>[],
39+
children: React$Element<any>[],
4240
|};
4341

4442
function Collapsible(props: CollapsiblePropsType): React$Element<'details'> {

packages/utils/error-overlay/src/components/ErrorOverlay.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77

88
/* eslint-env browser */
99
/* @flow */
10-
import React, {useEffect} from 'react';
10+
import {useEffect} from 'preact/hooks';
1111
import {theme} from '../styles';
1212

13-
import type {Node as ReactNode} from 'react';
14-
1513
const overlayStyle = {
1614
position: 'relative',
1715
display: 'inline-flex',
@@ -33,7 +31,7 @@ const overlayStyle = {
3331
};
3432

3533
type ErrorOverlayPropsType = {|
36-
children: ReactNode,
34+
children: React$Node,
3735
shortcutHandler?: (eventKey: string) => void,
3836
|};
3937

packages/utils/error-overlay/src/components/Footer.js

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
/* @flow */
9-
import React from 'react';
109
import {theme} from '../styles';
1110

1211
const footerStyle = {

packages/utils/error-overlay/src/components/Header.js

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
/* @flow */
9-
import React from 'react';
109
import {theme} from '../styles';
1110

1211
const headerStyle = {

packages/utils/error-overlay/src/components/HydrationDiff.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/** @flow */
2-
import React from 'react';
32
import {theme} from '../styles';
43

54
const diffStyle = {

packages/utils/error-overlay/src/components/NavigationBar.js

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
/* @flow */
9-
import React from 'react';
109
import {theme} from '../styles';
1110

1211
const navigationBarStyle = {

packages/utils/error-overlay/src/containers/RuntimeError.js

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
/* @flow */
9-
import React from 'react';
109
import Header from '../components/Header';
1110
import StackTrace from './StackTrace';
1211

packages/utils/error-overlay/src/containers/RuntimeErrorContainer.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
/* @flow */
9-
import React from 'react';
109
import ErrorOverlay from '../components/ErrorOverlay';
1110
import CloseButton from '../components/CloseButton';
1211
import NavigationBar from '../components/NavigationBar';
@@ -15,7 +14,7 @@ import Footer from '../components/Footer';
1514

1615
import type {ErrorRecord} from './RuntimeError';
1716
import type {ErrorLocation} from '..';
18-
import {useState} from 'react';
17+
import {useState} from 'preact/hooks';
1918

2019
type Props = {|
2120
errorRecords: ErrorRecord[],

packages/utils/error-overlay/src/containers/StackFrame.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
/* @flow */
9-
import React, {useState} from 'react';
9+
import {useState} from 'preact/hooks';
1010
import {theme} from '../styles';
1111
import CodeBlock from '../components/CodeBlock';
1212
import {getPrettyURL} from '../utils/getPrettyURL';

packages/utils/error-overlay/src/containers/StackTrace.js

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
/* @flow */
9-
import React from 'react';
109
import StackFrame from './StackFrame';
1110
import Collapsible from '../components/Collapsible';
1211
import {isInternalFile} from '../utils/isInternalFile';

packages/utils/error-overlay/src/index.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/* @flow */
1111
import type {ErrorRecord} from './listenToRuntimeErrors';
1212
import {listenToRuntimeErrors, crashWithFrames} from './listenToRuntimeErrors';
13-
import {createRoot} from 'react-dom/client';
13+
import {render} from 'preact';
1414
import RuntimeErrorContainer from './containers/RuntimeErrorContainer';
1515
import {overlayStyle} from './styles';
1616

@@ -96,33 +96,30 @@ export function stopReportingRuntimeErrors() {
9696
}
9797
}
9898

99-
let rootNode;
100-
let root;
99+
let rootNode, shadow;
101100

102101
function update() {
103-
if (!root) {
102+
if (!rootNode) {
104103
rootNode = document.createElement('parcel-error-overlay');
105-
let shadow = rootNode.attachShadow({mode: 'open'});
104+
shadow = rootNode.attachShadow({mode: 'open'});
106105
if (rootNode) {
107106
document.body?.appendChild(rootNode);
108-
root = createRoot(shadow);
109107
}
110108
}
111109

112-
if (currentRuntimeErrorRecords.length > 0 && root) {
113-
root.render(
110+
if (currentRuntimeErrorRecords.length > 0 && shadow) {
111+
render(
114112
<dialog
115113
ref={d => (d: any)?.showModal()}
116114
style={overlayStyle}
117115
onClose={dismissRuntimeErrors}
118116
>
119117
<ErrorOverlay />
120118
</dialog>,
119+
shadow,
121120
);
122121
} else {
123-
root?.unmount();
124122
rootNode?.remove();
125-
root = null;
126123
rootNode = null;
127124
}
128125
}

packages/utils/error-overlay/src/listenToRuntimeErrors.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
} from './effects/stackTraceLimit';
2222
import getStackFrames from './utils/getStackFrames';
2323
import type {StackFrame} from './utils/stack-frame';
24-
import React from 'react';
2524

2625
const CONTEXT_SIZE: number = 3;
2726

@@ -108,24 +107,27 @@ function patchConsole(method: string, onError: (err: Error) => void) {
108107
error = args.find(arg => arg instanceof Error);
109108
}
110109

111-
if (error && !error.message.includes('[parcel]')) {
110+
if (
111+
error &&
112+
!error.message.includes('[parcel]') &&
113+
typeof window !== 'undefined' &&
114+
window.__REACT_DEVTOOLS_GLOBAL_HOOK__
115+
) {
112116
// Attempt to append the React component stack
113-
// $FlowFixMe
114-
if (typeof React.captureOwnerStack === 'function') {
115-
// $FlowFixMe
116-
let stack = React.captureOwnerStack();
117-
error.stack += stack;
118-
} else {
119-
const ReactSharedInternals =
120-
// $FlowFixMe
121-
React.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE ||
122-
// $FlowFixMe
123-
React.__SERVER_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE;
124-
125-
if (typeof ReactSharedInternals?.getCurrentStack === 'function') {
126-
// $FlowFixMe
127-
let stack = ReactSharedInternals.getCurrentStack();
128-
error.stack += stack;
117+
// TODO: use React.captureOwnerStack once stable.
118+
let hook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
119+
if (hook.renderers instanceof Map) {
120+
for (let renderer of hook.renderers.values()) {
121+
if (
122+
typeof renderer?.currentDispatcherRef?.getCurrentStack ===
123+
'function'
124+
) {
125+
let stack = renderer.currentDispatcherRef.getCurrentStack();
126+
if (stack) {
127+
error.stack += stack;
128+
break;
129+
}
130+
}
129131
}
130132
}
131133

0 commit comments

Comments
 (0)