Skip to content

Commit

Permalink
fix(timeline): duplication injection
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 20, 2023
1 parent ec1c821 commit f5e3766
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/devtools-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"error-stack-parser-es": "^0.1.0",
"hookable": "^5.5.3",
"unbuild": "^1.2.1",
"unimport": "^3.0.14",
"unimport": "^3.1.0",
"vite-plugin-vue-inspector": "^3.4.2",
"vue-router": "^4.2.4"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"rc9": "^2.1.1",
"semver": "^7.5.4",
"sirv": "^2.0.3",
"unimport": "^3.0.14",
"unimport": "^3.1.0",
"vite-plugin-inspect": "^0.7.32",
"vite-plugin-vue-inspector": "^3.4.2",
"wait-on": "^7.0.1",
Expand Down
15 changes: 12 additions & 3 deletions packages/devtools/src/integrations/timeline.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve } from 'pathe'
import type { Import } from 'unimport'
import semver from 'semver'
import type { NuxtDevtoolsServerContext } from '../types'
import { runtimeDir } from '../dirs'

Expand All @@ -10,6 +11,7 @@ export async function setup({ nuxt, options }: NuxtDevtoolsServerContext) {
'#app',
'@unhead/vue',
]

const include = options.timeline?.functions?.include || [
i => includeFrom.includes(i.from),
i => i.from.includes('composables'),
Expand All @@ -32,6 +34,10 @@ export async function setup({ nuxt, options }: NuxtDevtoolsServerContext) {

nuxt.hook('imports:context', (unimport) => {
const ctx = unimport.getInternalContext()

if (!ctx.version || !semver.gte(ctx.version, '3.1.0'))
throw new Error(`[Nuxt DevTools] The timeline feature requires \`unimport\` >= v3.1.0, but got \`${ctx.version || '(unknown)'}\`. Please upgrade using \`nuxi upgrade --force\`.`)

ctx.addons.push(
{
injectImportsResolved(imports) {
Expand All @@ -50,13 +56,16 @@ export async function setup({ nuxt, options }: NuxtDevtoolsServerContext) {
}
})
},
injectImportsStringified(str, imports) {
injectImportsStringified(str, imports, s) {
const code = s.toString()
const injected = imports.filter(i => i.meta?.wrapperOriginalAs)
if (injected.length) {
const result = [
str,
`import { __wrapFunction } from ${JSON.stringify(helperPath)}`,
...injected.map(i => `const ${i.meta!.wrapperOriginalAs} = __wrapFunction(${JSON.stringify(i.name)}, ${i.as})`),
code.includes('__nuxtTimelineWrap')
? ''
: `import { __nuxtTimelineWrap } from ${JSON.stringify(helperPath)}`,
...injected.map(i => `const ${i.meta!.wrapperOriginalAs} = __nuxtTimelineWrap(${JSON.stringify(i.name)}, ${i.as})`),
'',
].join(';')
return result
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/src/runtime/function-metrics-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function initTimelineMetrics(): TimelineMetrics {

const wrapperFunctions = new WeakMap<any, any>()

export function __wrapFunction(name: string, fn: any) {
export function __nuxtTimelineWrap(name: string, fn: any) {
if (process.server)
return fn
if (typeof fn !== 'function')
Expand Down
2 changes: 2 additions & 0 deletions playgrounds/tab-timeline/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export default defineNuxtConfig({
'@vueuse/core',
],
},

enabled: true,
},
},
})
30 changes: 19 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f5e3766

Please sign in to comment.