Skip to content

Commit

Permalink
Merge pull request #31 from alexzhang1030/master
Browse files Browse the repository at this point in the history
fix: windows platform path error && remove rendundancy code
  • Loading branch information
sudongyuer authored Jul 25, 2022
2 parents 84a9062 + fe73cb6 commit c87754f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-phones-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vite-plugin-react-inspector": patch
---

fix: fix windows platform path error && remove rendundancy code
9 changes: 3 additions & 6 deletions packages/vite-plugin-react-inspector/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Connect, Plugin } from 'vite'
import { parseSync, traverse } from '@babel/core'
import MagicString from 'magic-string'
import { queryParserMiddleware } from './middleware'
import { parseJSXIdentifier } from './utils'
import { parseFilePath, parseJSXIdentifier } from './utils'
import { launchEditor } from './launch-editor'
function VitePluginReactInspector(): Plugin {
return {
Expand Down Expand Up @@ -60,14 +60,11 @@ function VitePluginReactInspector(): Plugin {
if (req.url?.startsWith('/__react-inspector-launch-editor')) {
const { file } = req?.query as any
if (file) {
const [filePath, line, column] = file.split(':')
const [filePath, line, column] = parseFilePath(file)
launchEditor(filePath, Number(line), Number(column))
}
next()
}
else {
next()
}
next()
})
},
transformIndexHtml(html) {
Expand Down
16 changes: 16 additions & 0 deletions packages/vite-plugin-react-inspector/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,19 @@ export function parseJSXIdentifier(name: JSXIdentifier | JSXMemberExpression) {
else
return `${parseJSXIdentifier(name.object)}.${parseJSXIdentifier(name.property)}`
}

/**
* Because win platform path scheme is D:/xxx
* So if we wanna get the col and row number, cannot just `const [fileName, col, row] = filePath.split(':')`
* We can use split, and the last two are col and row, then splice rest part
*
* @param {string} filePath full filePath including filename col and row
* @return {[string, string, string]} [fileName, row, col]
*/
export function parseFilePath(filePath: string): [string, string, string] {
const splitPath = filePath.split(':')
const fileName = splitPath.splice(0, splitPath.length - 2).join(':')
const row = splitPath[splitPath.length - 2]
const col = splitPath[splitPath.length - 1]
return [fileName, row, col]
}
10 changes: 10 additions & 0 deletions packages/vite-plugin-react-inspector/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { describe, expect, it } from 'vitest'
import { parseFilePath } from '../src/utils'
describe('should', () => {
it('exported', () => {
expect(1).toEqual(1)
})
})

it('multiple platforms path splice test', () => {
const OSX = '/Users/user/Desktop/test/test.jsx:1:1'
expect(parseFilePath(OSX)).toEqual(['/Users/user/Desktop/test/test.jsx', '1', '1'])
const WINDOWS = 'D:/test/test.jsx:1:1'
expect(parseFilePath(WINDOWS)).toEqual(['D:/test/test.jsx', '1', '1'])
const LINUX = '/Users/user/Desktop/test/test.jsx:1:1'
expect(parseFilePath(LINUX)).toEqual(['/Users/user/Desktop/test/test.jsx', '1', '1'])
})

0 comments on commit c87754f

Please sign in to comment.