包详细信息

@a-la/detect-jsx

a-la3.9kMIT1.0.2

Detects Position Of JSX Tag In JavaScript File.

detect-jsx, a-la

自述文件

@a-la/detect-jsx

npm version

@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'

detectJsx(
  string: string,
): number

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

更新日志

3 February 2020

1.0.2

  • [fix] Fix for Node 12.

15 January 2019

1.0.1

  • [package] Add the "module" field.

10 January 2019

1.0.0