React源码调试环境搭建
#react #source-code
jerrywu001
创建时间:2023-09-02 06:14:41
创建react项目
TIP
🧙🏽♂️ 版本:
create-react-app@5.0.0
npm i create-react-app -g
use npm
create-react-app react-debug
use create-react-app
弹出webpack配置
cd react-test
npm eject
react源码下载
进入react-debug/src目录
cd src
git clone https://github.com/facebook/react.git -b 17.0.2
解决 Building fresh packages问题(可跳过此步骤)
进入react-debug/src/react目录,并创建.yarnrc文件
添加以下内容
registry "https://registry.npm.taobao.org"
sass_binary_site "https://npm.taobao.org/mirrors/node-sass/"
phantomjs_cdnurl "http://cnpmjs.org/downloads"
electron_mirror "https://npm.taobao.org/mirrors/electron/"
sqlite3_binary_host_mirror "https://foxgis.oss-cn-shanghai.aliyuncs.com/"
profiler_binary_host_mirror "https://npm.taobao.org/mirrors/node-inspector/"
chromedriver_cdnurl "https://cdn.npm.taobao.org/dist/chromedriver
install packages(可跳过此步骤)
npm i
webpack配置修改
config/webpack.config.js
- alias中添加以下配置
{
alias: {
...(modules.webpackAliases | {}),
+ 'react': path.resolve(__dirname, '../src/react/packages/react'),
+ 'react-dom': path.resolve(__dirname, '../src/react/packages/react-dom'),
+ 'shared': path.resolve(__dirname, '../src/react/packages/shared'),
+ 'react-reconciler': path.resolve(__dirname, '../src/react/packages/react-reconciler'),
+ scheduler: path.resolve(__dirname, '../src/react/packages/scheduler'),
},
}
- 关闭eslint, tslint
- const disableESLintPlugin = process.env.DISABLE_ESLINT_PLUGIN === 'true';
+ const disableESLintPlugin = true;
- useTypeScript &&
+ false &&
new ForkTsCheckerWebpackPlugin({
// ...
});
- config/env.js
const stringified = {
'process.env': Object.keys(raw).reduce((env, key) => {
env[key] = JSON.stringify(raw[key]);
return env;
}, {}),
+ __DEV__: true,
+ __PROFILE__: true,
+ __UMD__: true,
+ __EXPERIMENTAL__: true,
+ __VARIANT__: false
};
- .eslintrc.json
{
"extends": "react-app",
"globals": {
"__DEV__": true,
"__PROFILE__": true,
"__UMD__": true,
"__EXPERIMENTAL__": true
}
}
React源码修改
- src/react/packages/scheduler/index.js
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
export * from './src/Scheduler';
+ export * from './src/SchedulerHostConfig.js';
- src/react/packages/scheduler/src/SchedulerHostConfig.js
- throw new Error('This module must be shimmed by a specific build.');
+ export {
+ unstable_flushAllWithoutAsserting,
+ unstable_flushNumberOfYields,
+ unstable_flushExpired,
+ unstable_clearYields,
+ unstable_flushUntilNextPaint,
+ unstable_flushAll,
+ unstable_yieldValue,
+ unstable_advanceTime
+ } from './forks/SchedulerHostConfig.mock.js';
+ export {
+ requestHostCallback,
+ requestHostTimeout,
+ cancelHostTimeout,
+ shouldYieldToHost,
+ getCurrentTime,
+ forceFrameRate,
+ requestPaint
+ } from './forks/SchedulerHostConfig.default.js';
- src/react/packages/react-reconciler/src/ReactFiberHostConfig.js
- import invariant from 'shared/invariant';
- invariant(false, 'This module must be shimmed by a specific renderer.');
+ export * from './forks/ReactFiberHostConfig.dom';
- src/react/packages/shared/ReactSharedInternals.js
- import * as React from 'react';
- const ReactSharedInternals =
- React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
+ import ReactSharedInternals from '../react/src/ReactSharedInternals';
export default ReactSharedInternals;
- src/react/packages/shared/invariant.js
export default function invariant(condition, format, a, b, c, d, e, f) {
+ if (condition) return;
throw new Error(
'Internal React error: invariant() is meant to be replaced at compile ' +
'time. There is no runtime version.',
);
}
react, react-dom引用方式调整
src/index.tsx
- import React from 'react';
- import ReactDOM from 'react-dom';
+ import * as React from 'react';
+ import * as ReactDOM from 'react-dom';
typescript报错解决
如果创建的是ts模版的react项目,修改以下文件
- src/react/packages/react/src/tests/testDefinitions/React.d.ts
// 注释掉以下代码
- declare module 'react' {
- export class Component {
- props: any;
- state: any;
- context: any;
- static name: string;
- constructor(props?, context?);
- setState(partial : any, callback ?: any) : void;
- forceUpdate(callback ?: any) : void;
- }
- export let PropTypes : any;
- export function createElement(tag : any, props ?: any, ...children : any[]) : any
- }
最后启动npm start即可进行调试了
附:在线调试
(不一定能打开哦~ ♂️):React App