wip: track

This commit is contained in:
Anthony Fu 2025-01-13 15:22:29 +08:00
parent c1b8b921f1
commit cd946d1773
No known key found for this signature in database
GPG Key ID: 179936958CD423FF
6 changed files with 82 additions and 9 deletions

View File

@ -39,6 +39,7 @@
"klona": "^2.0.6",
"mlly": "^1.7.3",
"ohash": "^1.1.4",
"on-change": "^5.0.1",
"pathe": "^1.1.2",
"pkg-types": "^1.3.0",
"scule": "^1.3.0",

View File

@ -1,6 +1,6 @@
import { existsSync, promises as fsp, lstatSync } from 'node:fs'
import { fileURLToPath, pathToFileURL } from 'node:url'
import type { ModuleMeta, Nuxt, NuxtConfig, NuxtModule } from '@nuxt/schema'
import type { ModuleMeta, Nuxt, NuxtConfig, NuxtDebugModuleMutationRecord, NuxtModule } from '@nuxt/schema'
import { dirname, isAbsolute, join, resolve } from 'pathe'
import { defu } from 'defu'
import { createJiti } from 'jiti'
@ -27,8 +27,44 @@ export async function installModule<
}
}
let _nuxt = nuxt
if (nuxt.options.debug) {
const onChange = await import('on-change').then(r => r.default)
const moduleName = (await nuxtModule.getMeta?.())?.name || buildTimeModuleMeta?.name || resolvedModulePath
// Unwrap onChange proxy if already wrapped
nuxt = onChange.target(nuxt)
nuxt._debug ||= {}
nuxt._debug.moduleMutationRecords ||= []
_nuxt = onChange(
nuxt,
(keys, value, _, applyData) => {
// We only listen to changes in the `options` object
if (keys[0] !== 'options') {
return
}
const record: NuxtDebugModuleMutationRecord = {
module: moduleName,
keys: keys.slice(1),
value,
timestamp: Date.now(),
}
if (applyData?.name) {
record.method = applyData.name
}
nuxt._debug!.moduleMutationRecords!.push(record)
}, {
ignoreUnderscores: true,
ignoreSymbols: true,
pathAsArray: true,
},
)
}
// Call module
const res = await nuxtModule(inlineOptions || {}, nuxt) ?? {}
const res = await nuxtModule(inlineOptions || {}, _nuxt) ?? {}
if (res === false /* setup aborted */) {
return
}

View File

@ -8,6 +8,7 @@ export type { AppHeadMetaObject, MetaObject, MetaObjectRaw, HeadAugmentations }
export type { ModuleDefinition, ModuleMeta, ModuleOptions, ModuleSetupInstallResult, ModuleSetupReturn, NuxtModule, ResolvedModuleOptions } from './types/module'
export type { Nuxt, NuxtApp, NuxtPlugin, NuxtPluginTemplate, NuxtTemplate, NuxtTypeTemplate, NuxtServerTemplate, ResolvedNuxtTemplate } from './types/nuxt'
export type { RouterConfig, RouterConfigSerializable, RouterOptions } from './types/router'
export type { NuxtDebugContext, NuxtDebugModuleMutationRecord } from './types/debug'
// Schema
export { default as NuxtConfigSchema } from './config/index'

View File

@ -0,0 +1,14 @@
export interface NuxtDebugContext {
/**
* Module mutation records to the `nuxt` instance.
*/
moduleMutationRecords?: NuxtDebugModuleMutationRecord[]
}
export interface NuxtDebugModuleMutationRecord {
module: string | undefined
keys: (string | symbol)[]
value: any
method?: string
timestamp: number
}

View File

@ -3,6 +3,7 @@ import type { Ignore } from 'ignore'
import type { NuxtHooks, NuxtLayout, NuxtMiddleware, NuxtPage } from './hooks'
import type { Component } from './components'
import type { NuxtOptions } from './config'
import type { NuxtDebugContext } from './debug'
export interface NuxtPlugin {
/** @deprecated use mode */
@ -83,6 +84,7 @@ export interface Nuxt {
_version: string
_ignore?: Ignore
_dependencies?: Set<string>
_debug?: NuxtDebugContext
/** The resolved Nuxt configuration. */
options: NuxtOptions

View File

@ -230,6 +230,9 @@ importers:
ohash:
specifier: 1.1.4
version: 1.1.4
on-change:
specifier: ^5.0.1
version: 5.0.1
pathe:
specifier: ^1.1.2
version: 1.1.2
@ -1064,7 +1067,7 @@ importers:
devDependencies:
'@vue/devtools-api':
specifier: latest
version: 7.6.8
version: 7.6.7
defu:
specifier: latest
version: 6.1.4
@ -1073,7 +1076,7 @@ importers:
version: 1.5.4
unplugin:
specifier: latest
version: 2.1.2
version: 1.16.1
vue:
specifier: 3.5.13
version: 3.5.13(typescript@5.7.2)
@ -3038,8 +3041,8 @@ packages:
'@vue/devtools-api@6.6.4':
resolution: {integrity: sha512-sGhTPMuXqZ1rVOk32RylztWkfXTRhuS7vgAKv0zjqk8gbsHkJ7xfFf+jbySxt7tWObEJwyKaHMikV/WGDiQm8g==}
'@vue/devtools-api@7.6.8':
resolution: {integrity: sha512-ma6dY/sZR36zALVsV1W7eC57c6IJPXsy8SNgZn1PLVWU4z4dPn5TIBmnF4stmdJ4sQcixqKaQ8pwjbMPzEZwiA==}
'@vue/devtools-api@7.6.7':
resolution: {integrity: sha512-PV4I31WaV2rfA8RGauM+69uFEzWkqtP561RiLU2wK+Ce85u3zyKW3aoESlLCNzkc4y0JaJyskH6zAE3xWOP8+Q==}
'@vue/devtools-core@7.6.8':
resolution: {integrity: sha512-8X4roysTwzQ94o7IobjVcOd1aZF5iunikrMrHPI2uUdigZCi2kFTQc7ffYiFiTNaLElCpjOhCnM7bo7aK1yU7A==}
@ -5817,6 +5820,10 @@ packages:
ohash@1.1.4:
resolution: {integrity: sha512-FlDryZAahJmEF3VR3w1KogSEdWX3WhA5GPakFx4J81kEAiHyLMpdLLElS8n8dfNadMgAne/MywcvmogzscVt4g==}
on-change@5.0.1:
resolution: {integrity: sha512-n7THCP7RkyReRSLkJb8kUWoNsxUIBxTkIp3JKno+sEz6o/9AJ3w3P9fzQkITEkMwyTKJjZciF3v/pVoouxZZMg==}
engines: {node: '>=18'}
on-finished@2.4.1:
resolution: {integrity: sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==}
engines: {node: '>= 0.8'}
@ -6590,6 +6597,7 @@ packages:
rollup-plugin-visualizer@5.13.1:
resolution: {integrity: sha512-vMg8i6BprL8aFm9DKvL2c8AwS8324EgymYQo9o6E26wgVvwMhsJxS37aNL6ZsU7X9iAcMYwdME7gItLfG5fwJg==}
engines: {node: '>=18'}
deprecated: Contains unintended breaking changes
hasBin: true
peerDependencies:
rolldown: 1.x
@ -7278,6 +7286,10 @@ packages:
resolution: {integrity: sha512-5liCNPuJW8dqh3+DM6uNM2EI3MLLpCKp/KY+9pB5M2S2SR2qvvDHhKgBOaTWEbZTAws3CXfB0rKTIolWKL05VQ==}
engines: {node: '>=14.0.0'}
unplugin@1.16.1:
resolution: {integrity: sha512-4/u/j4FrCKdi17jaxuJA0jClGxB1AvU2hw/IuayPc4ay1XGaJs/rbb4v5WKwAjNifjmXK9PIFyuPiaK8azyR9w==}
engines: {node: '>=14.0.0'}
unplugin@2.0.0-beta.1:
resolution: {integrity: sha512-2qzQo5LN2DmUZXkWDHvGKLF5BP0WN+KthD6aPnPJ8plRBIjv4lh5O07eYcSxgO2znNw9s4MNhEO1sB+JDllDbQ==}
engines: {node: '>=18.12.0'}
@ -10006,7 +10018,7 @@ snapshots:
chokidar: 3.6.0
magic-string: 0.30.17
tinyglobby: 0.2.10
unplugin: 1.16.0
unplugin: 1.16.1
webpack: 5.97.1(esbuild@0.24.2)
webpack-sources: 3.2.3
transitivePeerDependencies:
@ -10235,7 +10247,7 @@ snapshots:
'@vue/devtools-api@6.6.4': {}
'@vue/devtools-api@7.6.8':
'@vue/devtools-api@7.6.7':
dependencies:
'@vue/devtools-kit': 7.6.8
@ -13547,6 +13559,8 @@ snapshots:
ohash@1.1.4: {}
on-change@5.0.1: {}
on-finished@2.4.1:
dependencies:
ee-first: 1.1.1
@ -15247,6 +15261,11 @@ snapshots:
acorn: 8.14.0
webpack-virtual-modules: 0.6.2
unplugin@1.16.1:
dependencies:
acorn: 8.14.0
webpack-virtual-modules: 0.6.2
unplugin@2.0.0-beta.1:
dependencies:
acorn: 8.14.0
@ -15299,7 +15318,7 @@ snapshots:
mlly: 1.7.3
pathe: 1.1.2
pkg-types: 1.3.0
unplugin: 1.16.0
unplugin: 1.16.1
update-browserslist-db@1.1.0(browserslist@4.24.0):
dependencies: