@@ -70,20 +70,44 @@ AST 转换为 JavaScript 函数调用,并处理语法规则(如 `class` →
7070- React 17 前(传统运行时):
7171
7272 ``` js
73+ React .createElement (type, config, ... children)
74+
7375 React .createElement (" div" , { className: " header" }, " Hello" );
7476 ```
7577
7678- React 17+(自动运行时):
7779
7880 ``` js
7981 import { jsx as _jsx } from ' react/jsx-runtime' ;
82+ _jsx (Component, config)
8083 _jsx (" div" , { className: " header" , children: " Hello" });
8184 ```
82-
85+
8386 优势:自动导入 jsx-runtime,无需手动引入 React,支持 Tree-shaking
84-
8587
86-
88+
89+
90+ 还有17前后编译的结果也不同
91+
92+ ```
93+ <div>
94+ hello
95+ <span>world</span>
96+ </div>
97+
98+ // 17后编译成:
99+ jsx('div', {
100+ children: ['hello', jsx('span', { children: 'world' })]
101+ })
102+
103+ // 17前编译成:
104+ React.createElement('div',
105+ null,
106+ 'hello',
107+ React.createElement('span', null, 'world')
108+ )
109+ ```
110+
87111
88112
89113#### ** 运行时:**
@@ -179,15 +203,122 @@ reactElement() 函数进行统一的工厂对象创建输出
179203
180204新建script/rollup/react.config.js
181205
206+ 进行书写打包配置规则,根据模块的package.json中的信息 生成路径
207+
208+ 配置关键
209+
210+ ``` js
211+ {
212+ input: ` ${ pkgPath} /${ module } ` ,
213+ output: {
214+ file: ` ${ pkgDistPath} /index.js` ,
215+ name: ' React' ,
216+ format: ' umd' // UMD 格式(浏览器+Node都可用)
217+ },
218+ plugins: [
219+ ... getBaseRollupPlugins (), // TS编译 + CommonJS转换
220+ generatePackageJson ({
221+ // 自动生成 package.json
222+ inputFolder: ` ${ pkgPath} ` ,
223+ outputFolder: ` ${ pkgDistPath} ` ,
224+ baseContents : (name , description , version ) => ({
225+ name,
226+ description,
227+ version,
228+ main: ' index.js'
229+ })
230+ ]
231+
232+ }
233+ ` ` `
234+
235+
236+
237+
238+
239+ 并配置打包命令
240+
241+
182242
243+ 接下来打包成功后,进行调试
183244
245+ ` ` `
246+ pnpm link -- global // 请求被连接
247+
248+ pnpm link react -- global // 主动去连接
249+ ` ` `
250+
251+ 将当前项目链接到全局 node_modules 中,使得该项目可以在全局范围内被其他项目引用或使用。
184252
185253
186254
255+ 创建调试测试项目
256+
257+ ` ` ` cmd
258+ npx create- react- app react- demo
259+ ` ` `
260+
187261
188262
263+ index中
264+
265+ ` ` ` js
266+ import React from ' react' ;
267+
268+ const aa =
269+ < div >
270+ hello
271+ < span> world< / span>
272+ < / div>
273+
274+ console .log (React)
275+ console .log (aa)
276+ ` ` `
277+
189278
190279
280+ 经过编译后是
281+
282+ ` ` ` js
283+ import { jsx as _jsx } from ' react/jsx-runtime' ;
284+ import React from ' react' ;
285+
286+ const aa = _jsx (' div' , {
287+ children: [
288+ ' hello' ,
289+ _jsx (' span' , { children: ' world' })
290+ ]
291+ });
292+ ` ` `
293+
294+ 编译器会自动添加运行时的库,并使用的
295+
296+ > import { jsx as _jsx } from 'react/jsx-runtime';
297+
298+
299+
300+ 因此为一下逻辑
301+
302+ ` ` ` md
303+ 你的代码:
304+ < div> hello< / div>
305+ ↓
306+ Babel/ TypeScript 编译
307+ ↓
308+ 自动导入:import { jsx } from ' react/jsx-runtime'
309+ ↓
310+ 转换为:jsx (' div' , { children: ' hello' })
311+ ↓
312+ 执行 jsx () 函数(来自 dist/ modules/ react/ jsx- runtime .js )
313+ ↓
314+ 返回 ReactElement 对象:
315+ {
316+ $$typeof: Symbol (react .element ),
317+ type: ' div' ,
318+ props: { children: ' hello' },
319+ _mark: ' juanlou' ← 你的自定义标记!
320+ }
321+ ` ` `
191322
192323
193324
0 commit comments