Skip to content

Commit ddaedfa

Browse files
committed
refactor(kit): extract trace utilities
1 parent cbad63c commit ddaedfa

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

packages/kit/src/internal/esm.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { pathToFileURL } from 'node:url'
22
import { interopDefault } from 'mlly'
33
import { resolveModulePath } from 'exsolve'
44
import { createJiti } from 'jiti'
5+
import { getUserCaller, warn } from './trace'
6+
import { resolveAlias } from '../resolve'
57

68
export interface ResolveModuleOptions {
79
/** @deprecated use `url` with URLs pointing at a file - never a directory */
@@ -58,17 +60,14 @@ export function tryImportModule<T = unknown> (id: string, opts?: ImportModuleOpt
5860
}
5961
}
6062

61-
const warnings = new Set<string>()
62-
6363
/**
6464
* @deprecated Please use `importModule` instead.
6565
*/
6666
export function requireModule<T = unknown> (id: string, opts?: ImportModuleOptions) {
67-
if (!warnings.has(id)) {
68-
// TODO: add more information on stack trace
69-
console.warn('[@nuxt/kit] `requireModule` is deprecated. Please use `importModule` instead.')
70-
warnings.add(id)
71-
}
67+
const caller = getUserCaller()
68+
const explanation = caller ? ` (used at \`${resolveAlias(caller.source)}:${caller.line}:${caller.column}\`)` : ''
69+
const warning = `[@nuxt/kit] \`requireModule\` is deprecated${explanation}. Please use \`importModule\` instead.`
70+
warn(warning)
7271
const resolvedPath = resolveModule(id, opts)
7372
const jiti = createJiti(import.meta.url, {
7473
interopDefault: opts?.interopDefault !== false,

packages/kit/src/internal/trace.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { captureStackTrace } from 'errx'
2+
3+
const distURL = import.meta.url.replace(/\/dist\/.*$/, '/')
4+
5+
export function getUserCaller () {
6+
if (!import.meta.dev) {
7+
return null
8+
}
9+
10+
const { source, line, column } = captureStackTrace().find(entry => !entry.source.startsWith(distURL)) ?? {}
11+
12+
if (!source) {
13+
return null
14+
}
15+
16+
return {
17+
source: source.replace(/^file:\/\//, ''),
18+
line,
19+
column,
20+
}
21+
}
22+
23+
const warnings = new Set<string>()
24+
25+
export function warn (warning: string) {
26+
if (!warnings.has(warning)) {
27+
console.warn(warning)
28+
warnings.add(warning)
29+
}
30+
}

0 commit comments

Comments
 (0)