Skip to content

Commit

Permalink
fix: plugin not working on windows (#3)
Browse files Browse the repository at this point in the history
* fix: plugin not working on windows

* chore: create array args with original splice args and unshift the id
chore: missing args for load

* chore: simplify logic resolving args
chore: update `plugin.transform` logic, code was removed

* chore: simplify logic resolving args also on `plugin.transform`

* chore: undo simplify logic resolving args also on `plugin.transform`

* chore: use array constructor to build the new args

* chore: remove array usage on new args build
  • Loading branch information
userquin committed Sep 18, 2021
1 parent b41e551 commit d86bd4b
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { ModuleInfo, TransformInfo } from '../types'

const debug = _debug('vite-plugin-inspect')

const isWindows = process.platform === 'win32'
const windowsRe = /^([A-Z]+):\/(.*)/
const WindowResolveIdPrefix = '/__resolve_windows/'

export interface Options {
/**
* Enable the inspect plugin (could be some performance overhead)
Expand Down Expand Up @@ -45,14 +49,27 @@ function PluginInspect(options: Options = {}): Plugin {

const transformMap: Record<string, TransformInfo[]> = {}
const idMap: Record<string, string> = {}
const windowsIdMap: Record<string, string> = {}

function hijackPlugin(plugin: Plugin) {
function resolveArguments(...args: any[]) {
const id = args[0]
if (isWindows && id && id.startsWith(WindowResolveIdPrefix))
args[0] = windowsIdMap[id]

return args
}
if (plugin.transform) {
debug('hijack plugin transform', plugin.name)
const _transform = plugin.transform
plugin.transform = async function(this: any, ...args: any[]) {
const code = args[0]
const id = args[1]
let id = args[1]
if (isWindows && id && id.startsWith(WindowResolveIdPrefix)) {
id = windowsIdMap[id]
args[1] = id
}

const start = Date.now()
const _result = await _transform.apply(this, args as any)
const end = Date.now()
Expand All @@ -78,7 +95,9 @@ function PluginInspect(options: Options = {}): Plugin {
debug('hijack plugin load', plugin.name)
const _load = plugin.load
plugin.load = async function(this: any, ...args: any[]) {
args = resolveArguments(args)
const id = args[0]

const start = Date.now()
const _result = await _load.apply(this, args as any)
const end = Date.now()
Expand All @@ -96,7 +115,9 @@ function PluginInspect(options: Options = {}): Plugin {
debug('hijack plugin resolveId', plugin.name)
const _resolveId = plugin.resolveId
plugin.resolveId = async function(this: any, ...args: any[]) {
args = resolveArguments(args)
const id = args[0]

const _result = await _resolveId.apply(this, args as any)

const result = typeof _result === 'object' ? _result?.id : _result
Expand Down Expand Up @@ -177,8 +198,16 @@ function PluginInspect(options: Options = {}): Plugin {
res.end()
}
else if (pathname === '/resolve') {
const id = parseQuery(search).id as string
res.write(JSON.stringify({ id: resolveId(id) }, null, 2))
let id = resolveId(parseQuery(search).id as string)
if (isWindows) {
const match = id.match(windowsRe)
if (match) {
const original = id
id = `${WindowResolveIdPrefix}${match[1]}/${match[2]}`
windowsIdMap[id] = original
}
}
res.write(JSON.stringify({ id }, null, 2))
res.end()
}
else if (pathname === '/clear') {
Expand Down

0 comments on commit d86bd4b

Please sign in to comment.