Skip to content

Commit

Permalink
fix(dom-to-code): 修复 React.Fragment 编译插入文件信息的 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
2214962083 committed Oct 24, 2022
1 parent 9f912c2 commit e36362a
Show file tree
Hide file tree
Showing 23 changed files with 3,775 additions and 730 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public-hoist-pattern[] = regenerator-runtime
public-hoist-pattern[] = esbuild
public-hoist-pattern[] = *conventional*
public-hoist-pattern[] = jsdom
public-hoist-pattern[] = postcss*

hoist-pattern[] = typescript
hoist-pattern[] = @vue/*
Expand Down
52 changes: 43 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,24 @@ Example: [`playgrounds/vite-vue3`](./playgrounds/vite-vue3/)

<br></details>

<details>
<summary>Webpack</summary><br>

```ts
// webpack.config.js
const { domToCodePlugin } = require('dom-to-code/webpack')
module.exports = {
/* ... */
plugins: [
domToCodePlugin({
mode: 'vue'
})
]
}
```

<br></details>

<details>
<summary>Vue CLI</summary><br>

Expand Down Expand Up @@ -137,21 +155,37 @@ Example: [`playgrounds/webpack5-vue2`](./playgrounds/webpack5-vue2/)
<br></details>

<details>
<summary>Webpack</summary><br>
<summary>Create-react-app + react-app-rewired</summary><br>

```ts
// webpack.config.js
const { domToCodePlugin } = require('dom-to-code/webpack')
// config-overrides.js
const { domToCodePlugin, domToCodeDevServerV4, domToCodeDevServerV5 } = require('dom-to-code/webpack')

module.exports = {
/* ... */
plugins: [
domToCodePlugin({
mode: 'vue'
})
]
webpack(config) {
config.plugins.push(domToCodePlugin({
mode: 'react',
}))
return config
},
devServer(configFunction) {
return function (proxy, allowedHost) {
const config = configFunction(proxy, allowedHost)

// 如果你的 package.json 里的 react-scripts 版本 <= 4.x.x,则使用 domToCodeDevServerV4
// Object.assign(config, domToCodeDevServerV4)

// 如果你的 package.json 里的 react-scripts 版本 >= 5.x.x,则使用 domToCodeDevServerV5
Object.assign(config, domToCodeDevServerV5)

return config
}
},
}
```

Example: [`playgrounds/webpack5-react`](./playgrounds/webpack5-react/)

<br></details>

## 📚 文档
Expand Down
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,10 @@
"pnpm": {
"overrides": {
"@types/node": "18.7.23",
"@types/react": "18.0.21",
"@types/react-dom": "18.0.6",
"@types/react-router-dom": "5.3.3",
"@vitejs/plugin-react": "2.1.0",
"concurrently": "7.4.0",
"conventional-changelog-cli": "2.2.2",
"esno": "0.16.3",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-router-dom": "6.3.0",
"typescript": "4.7.4",
"unbuild": "0.8.11",
"vite": "3.0.9",
Expand Down Expand Up @@ -86,7 +80,9 @@
"@hapi/address": "*",
"@hapi/hoek": "*",
"har-validator": "*",
"svgo": "*"
"svgo": "*",
"@hapi/bourne": "*",
"w3c-hr-time": "*"
},
"packageExtensions": {
"vue-template-compiler": {
Expand Down
17 changes: 14 additions & 3 deletions packages/dom-to-code/src/core/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { JSXIdentifier, JSXMemberExpression } from '@babel/types'
import type { JSXIdentifier, JSXMemberExpression, JSXNamespacedName, JSXOpeningElement } from '@babel/types'
import type { FilterPattern } from '@rollup/pluginutils'
import type MagicString from 'magic-string'
import type { TransformResult } from 'unplugin'
Expand Down Expand Up @@ -45,13 +45,24 @@ export function resolveOption(options: Options): OptionsResolved {
/**
* 处理 jsx ast
*/
export function parseJSXIdentifier(name: JSXIdentifier | JSXMemberExpression): string {
export function parseJSXIdentifier(name: JSXIdentifier | JSXMemberExpression | JSXNamespacedName): string {
if (name.type === 'JSXIdentifier')
return name.name
return name.name || ''
else if (name.type === 'JSXNamespacedName')
return parseJSXIdentifier(name.name) || ''
else
return `${parseJSXIdentifier(name.object)}.${parseJSXIdentifier(name.property)}`
}

/**
* 获取 jsx 元素 tag 名称
* @param openingElement jsx 元素
* @returns jsx 元素名称 <div></div> 返回 div, <React.Fragment></React.Fragment> 返回 React.Fragment
*/
export function getJsxElementName(openingElement: JSXOpeningElement) {
return parseJSXIdentifier(openingElement.name)
}

/**
* 判断对象是否拥有某个 key
* @param obj 对象
Expand Down
4 changes: 2 additions & 2 deletions packages/dom-to-code/src/core/transform/react.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// see: https://github.com/sudongyuer/vite-plugin-react-inspector/blob/master/packages/vite-plugin-react-inspector/src/index.ts
import { parseSync, traverse } from '@babel/core'
import MagicString from 'magic-string'
import { createDomAttrLineInfo, parseJSXIdentifier } from '../helpers'
import { createDomAttrLineInfo, getJsxElementName, parseJSXIdentifier } from '../helpers'

/**
* 转换 react 项目
Expand All @@ -25,7 +25,7 @@ export const transformReact = (code: string, id: string) => {
})
traverse(ast, {
enter({ node }) {
if (node.type === 'JSXElement') {
if (node.type === 'JSXElement' && !getJsxElementName(node.openingElement).endsWith('Fragment')) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
if (node?.openingElement?.name?.object?.name === 'React')
Expand Down
10 changes: 6 additions & 4 deletions playgrounds/vite-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
"dependencies": {
"dom-to-code": "workspace:*",
"clsx": "^1.2.1",
"react": "*",
"react-dom": "*"
"react": "17.0.2",
"react-dom": "17.0.2",
"react-router-dom": "6.3.0"
},
"devDependencies": {
"@types/node": "^18.7.23",
"@types/react": "*",
"@types/react-dom": "*",
"@types/react": "18.0.21",
"@types/react-dom": "18.0.6",
"@types/react-router-dom": "5.3.3",
"@unocss/reset": "^0.45.26",
"@vitejs/plugin-react": "^2.1.0",
"concurrently": "*",
Expand Down
20 changes: 20 additions & 0 deletions playgrounds/webpack5-react/config-overrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { domToCodePlugin, domToCodeDevServerV5 } = require('dom-to-code/webpack')

// see: https://github.com/timarney/react-app-rewired
module.exports = {
webpack(config) {
config.plugins.push(domToCodePlugin({
mode: 'react',
}))
return config
},
devServer(configFunction) {
return function (proxy, allowedHost) {
const config = configFunction(proxy, allowedHost)

Object.assign(config, domToCodeDevServerV5)

return config
}
},
}
40 changes: 40 additions & 0 deletions playgrounds/webpack5-react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "playground-webpack5-react",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-app-rewired eject"
},
"dependencies": {
"@types/node": "*",
"@types/react": "^18.0.21",
"@types/react-dom": "^18.0.6",
"dom-to-code": "workspace:*",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"typescript": "*",
"web-vitals": "^2.1.4"
},
"devDependencies": {
"@babel/core": "^7.19.6",
"@babel/plugin-syntax-flow": "^7.18.6",
"@babel/plugin-transform-react-jsx": "^7.19.0",
"react-app-rewired": "^2.2.1"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file added playgrounds/webpack5-react/public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions playgrounds/webpack5-react/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added playgrounds/webpack5-react/public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added playgrounds/webpack5-react/public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions playgrounds/webpack5-react/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions playgrounds/webpack5-react/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
38 changes: 38 additions & 0 deletions playgrounds/webpack5-react/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
34 changes: 34 additions & 0 deletions playgrounds/webpack5-react/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react'
import logo from './logo.svg'
import './App.css'

function Test() {
return <React.Fragment>
<div>Test</div>
<div>Test2</div>
</React.Fragment>
}

function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.tsx</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
<Test></Test>
</header>
</div>
)
}

export default App
Loading

0 comments on commit e36362a

Please sign in to comment.