@a-la/detect-jsx 中文文档教程
@a-la/detect-jsx
@a-la/detect-jsx
检测 JSX 标签在 JavaScript 文件中的位置。
yarn add -E @a-la/detect-jsx
Table Of Contents
API
该包可通过导入其默认函数获得:
import detectJsx from '@a-la/detect-jsx'
<ins>detectJsx</ins>(
string: string,
): <i>number</i>
通过使用 Node 的 vm
包评估代码返回第一个 JSX 标签的位置:由于 JSX 引用会抛出一个错误,这个错误可以被捕获并用于找出第一个 JSX 标签的索引在 JSX 中打开 <
符号。
例如,给定以下 JSX 组件:
const Component = ({ example }) => (
<div class="Test" id={example}></div>
)
可以检测 JSX
的起始位置:
/* yarn example/ */
import read from '@wrote/read'
import detectJSX from '@a-la/detect-jsx'
(async () => {
const code = await read('example/Component.jsx')
const res = detectJSX(code)
console.log(res)
})()
39
Copyright
(c) À La Mode 2019
@a-la/detect-jsx
@a-la/detect-jsx
Detects Position Of JSX Tag In JavaScript File.
yarn add -E @a-la/detect-jsx
Table Of Contents
API
The package is available by importing its default function:
import detectJsx from '@a-la/detect-jsx'
<ins>detectJsx</ins>(
string: string,
): <i>number</i>
Returns the position of the first JSX tag by evaluating the code using Node's vm
package: since JSX reference will throw an error, this error can be caught and used to find out the index of the opening <
symbol in JSX.
For example, given the following JSX component:
const Component = ({ example }) => (
<div class="Test" id={example}></div>
)
It is possible to detect where JSX
starts:
/* yarn example/ */
import read from '@wrote/read'
import detectJSX from '@a-la/detect-jsx'
(async () => {
const code = await read('example/Component.jsx')
const res = detectJSX(code)
console.log(res)
})()
39
Copyright
(c) À La Mode 2019