Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge React Refresh runtime and transformer #10056

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/configs/default/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"runtimes": [
"@parcel/runtime-js",
"@parcel/runtime-browser-hmr",
"@parcel/runtime-react-refresh",
"@parcel/runtime-service-worker"
],
"optimizers": {
Expand Down
1 change: 0 additions & 1 deletion packages/configs/default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"@parcel/resolver-default": "2.13.3",
"@parcel/runtime-browser-hmr": "2.13.3",
"@parcel/runtime-js": "2.13.3",
"@parcel/runtime-react-refresh": "2.13.3",
"@parcel/runtime-service-worker": "2.13.3",
"@parcel/transformer-babel": "2.13.3",
"@parcel/transformer-css": "2.13.3",
Expand Down
6 changes: 1 addition & 5 deletions packages/configs/repl/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
"url:*": ["@parcel/transformer-raw"]
},
"namers": ["@parcel/namer-default"],
"runtimes": [
"@parcel/runtime-js",
"@parcel/runtime-browser-hmr",
"@parcel/runtime-react-refresh"
],
"runtimes": ["@parcel/runtime-js", "@parcel/runtime-browser-hmr"],
"optimizers": {
"*.{js,mjs,cjs}": ["@parcel/optimizer-terser"],
"*.css": ["@parcel/optimizer-css"]
Expand Down
1 change: 0 additions & 1 deletion packages/configs/repl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"@parcel/resolver-repl-runtimes": "2.13.3",
"@parcel/runtime-browser-hmr": "2.13.3",
"@parcel/runtime-js": "2.13.3",
"@parcel/runtime-react-refresh": "2.13.3",
"@parcel/transformer-babel": "2.13.3",
"@parcel/transformer-css": "2.13.3",
"@parcel/transformer-html": "2.13.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"dependencies": {
"react": "*"
"react": "^19"
}
}
2 changes: 0 additions & 2 deletions packages/dev/repl/src/parcel/BrowserPackageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import resolverDefault from '@parcel/resolver-default';
import resolverREPLRuntimes from '@parcel/resolver-repl-runtimes';
import runtimeHMR from '@parcel/runtime-browser-hmr';
import runtimeJs from '@parcel/runtime-js';
import runtimeReactRefresh from '@parcel/runtime-react-refresh';
import transformerBabel from '@parcel/transformer-babel';
import transformerCss from '@parcel/transformer-css';
import transformerHtml from '@parcel/transformer-html';
Expand Down Expand Up @@ -57,7 +56,6 @@ export const BUILTINS = {
'@parcel/resolver-repl-runtimes': resolverREPLRuntimes,
'@parcel/runtime-browser-hmr': runtimeHMR,
'@parcel/runtime-js': runtimeJs,
'@parcel/runtime-react-refresh': runtimeReactRefresh,
'@parcel/transformer-babel': transformerBabel,
'@parcel/transformer-css': transformerCss,
'@parcel/transformer-html': transformerHtml,
Expand Down
28 changes: 0 additions & 28 deletions packages/runtimes/react-refresh/package.json

This file was deleted.

74 changes: 0 additions & 74 deletions packages/runtimes/react-refresh/src/ReactRefreshRuntime.js

This file was deleted.

3 changes: 2 additions & 1 deletion packages/transformers/react-refresh-wrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"@parcel/plugin": "2.13.3",
"@parcel/utils": "2.13.3",
"react-refresh": ">=0.9 <=0.14"
"react-error-overlay": "6.0.9",
"react-refresh": ">=0.9 <=0.16"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default (new Transformer({
let name = `$parcel$ReactRefreshHelpers$${asset.id.slice(-4)}`;

code = `var ${name} = require(${JSON.stringify(wrapperPath)});
${name}.init();
var prevRefreshReg = globalThis.$RefreshReg$;
var prevRefreshSig = globalThis.$RefreshSig$;
${name}.prelude(module);
Expand Down
43 changes: 30 additions & 13 deletions packages/transformers/react-refresh-wrap/src/helpers/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var Refresh = require('react-refresh/runtime');
var {version} = require('react-refresh/package.json');

function debounce(func, delay) {
if (process.env.NODE_ENV === 'test') {
Expand Down Expand Up @@ -33,24 +32,42 @@ var enqueueUpdate = debounce(function () {
Refresh.performReactRefresh();
}, 30);

module.exports.init = function () {
if (!globalThis.$RefreshReg$) {
Refresh.injectIntoGlobalHook(globalThis);
globalThis.$RefreshReg$ = function () {};
globalThis.$RefreshSig$ = function () {
return function (type) {
return type;
};
};

if (typeof window !== 'undefined') {
let ErrorOverlay = require('react-error-overlay');
ErrorOverlay.setEditorHandler(function editorHandler(errorLocation) {
let file = `${errorLocation.fileName}:${
errorLocation.lineNumber || 1
}:${errorLocation.colNumber || 1}`;
fetch(`/__parcel_launch_editor?file=${encodeURIComponent(file)}`);
});

ErrorOverlay.startReportingRuntimeErrors({
onError: function () {},
});

window.addEventListener('parcelhmraccept', () => {
ErrorOverlay.dismissRuntimeErrors();
});
}
}
};

// Everything below is either adapted or copied from
// https://github.com/facebook/metro/blob/61de16bd1edd7e738dd0311c89555a644023ab2d/packages/metro/src/lib/polyfills/require.js
// MIT License - Copyright (c) Facebook, Inc. and its affiliates.

module.exports.prelude = function (module) {
globalThis.__REACT_REFRESH_VERSION_TRANSFORMER = version;
globalThis.$RefreshReg$ = function (type, id) {
if (
globalThis.__REACT_REFRESH_VERSION_TRANSFORMER &&
globalThis.__REACT_REFRESH_VERSION_RUNTIME &&
globalThis.__REACT_REFRESH_VERSION_TRANSFORMER !==
globalThis.__REACT_REFRESH_VERSION_RUNTIME
) {
// Both versions were set and they did not match
throw new Error(
`react-refresh versions did not match between transformer and runtime. Please check your dependencies. Transformer: ${globalThis.__REACT_REFRESH_VERSION_TRANSFORMER}, Runtime: ${globalThis.__REACT_REFRESH_VERSION_RUNTIME}`,
);
}
Refresh.register(type, module.id + ' ' + id);
};
globalThis.$RefreshSig$ = Refresh.createSignatureFunctionForTransform;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12322,10 +12322,10 @@ react-is@^18.0.0:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-18.3.1.tgz#e83557dc12eae63a99e003a46388b1dcbb44db7e"
integrity sha512-/LLMVyas0ljjAtoYiPqYiL8VWXzUUdThrmU5+n20DZv+a+ClRoevUzw5JxU+Ieh5/c87ytoTBV9G1FiKfNJdmg==

"react-refresh@>=0.9 <=0.14":
version "0.14.2"
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.14.2.tgz#3833da01ce32da470f1f936b9d477da5c7028bf9"
integrity sha512-jCvmsr+1IUSMUyzOkRcvnVbX3ZYC6g9TDrDbFuFmRDq7PD4yaGbLKNQL6k2jnArV8hjYxh7hVhAZB6s9HDGpZA==
"react-refresh@>=0.9 <=0.16":
version "0.16.0"
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.16.0.tgz#e7d45625f05c9709466d09348a25d22f79b2ad23"
integrity sha512-FPvF2XxTSikpJxcr+bHut2H4gJ17+18Uy20D5/F+SKzFap62R3cM5wH6b8WN3LyGSYeQilLEcJcR1fjBSI2S1A==

react-resizable-panels@^0.0.61:
version "0.0.61"
Expand Down
Loading