Skip to content

Commit

Permalink
feat: introduce removeVersionQuery options
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 22, 2024
1 parent 6694364 commit 374c6f9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/node/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@ import type { ResolvedConfig, ViteDevServer } from 'vite'
import type { ModuleInfo, PluginMetricInfo, ResolveIdInfo } from '../types'
import { Recorder } from './recorder'
import { DUMMY_LOAD_PLUGIN_NAME } from './constants'
import { removeVersionQuery } from './utils'

export class ViteInspectContext {
public filter: (id: string) => boolean
public config: ResolvedConfig = undefined!

public recorderClient = new Recorder()
public recorderServer = new Recorder()
public recorderClient: Recorder
public recorderServer: Recorder

constructor(public options: any) {
this.filter = createFilter(options.include, options.exclude)
this.recorderClient = new Recorder(this)
this.recorderServer = new Recorder(this)
}

normalizeId(id: string) {
if (this.options.removeVersionQuery !== false)
return removeVersionQuery(id)
return id
}

getRecorder(ssr: boolean | undefined) {
Expand Down
6 changes: 6 additions & 0 deletions src/node/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,10 @@ export interface Options {
* @default false
*/
open?: boolean
/**
* Remove version query `?v=xxx` and treat them as the same module
*
* @default true
*/
removeVersionQuery?: boolean
}
11 changes: 11 additions & 0 deletions src/node/recorder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import type { ResolveIdInfo, TransformInfo } from '../types'
import { DUMMY_LOAD_PLUGIN_NAME } from './constants'
import type { ViteInspectContext } from './context'

export class Recorder {
transform: Record<string, TransformInfo[]> = {}
resolveId: Record<string, ResolveIdInfo[]> = {}
transformCounter: Record<string, number> = {}

constructor(
public context: ViteInspectContext,
) {
this.context = context
}

recordTransform(id: string, info: TransformInfo, preTransformCode: string) {
id = this.context.normalizeId(id)
// initial transform (load from fs), add a dummy
if (!this.transform[id] || !this.transform[id].some(tr => tr.result)) {
this.transform[id] = [{
Expand All @@ -23,17 +31,20 @@ export class Recorder {
}

recordLoad(id: string, info: TransformInfo) {
id = this.context.normalizeId(id)
this.transform[id] = [info]
this.transformCounter[id] = (this.transformCounter[id] || 0) + 1
}

recordResolveId(id: string, info: ResolveIdInfo) {
id = this.context.normalizeId(id)
if (!this.resolveId[id])
this.resolveId[id] = []
this.resolveId[id].push(info)
}

invalidate(id: string) {
id = this.context.normalizeId(id)
delete this.transform[id]
}
}
10 changes: 10 additions & 0 deletions src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@ export async function openBrowser(address: string) {
.then(r => r.default(address, { newInstance: true }))
.catch(() => {})
}

export function removeVersionQuery(url: string) {
if (url.includes('v=')) {
return url
.replace(/&v=\w+/, '')
.replace(/\?v=\w+/, '?')
.replace(/\?$/, '')
}
return url
}

0 comments on commit 374c6f9

Please sign in to comment.