@@ -133,6 +133,10 @@ export interface ResolvedImport {
133133 path : string
134134}
135135
136+ export type InternalImportTarget = string | { default ?: string ; import ?: string } | null | undefined
137+
138+ export type InternalImportsMap = Record < string , InternalImportTarget >
139+
136140/**
137141 * Resolve a relative import specifier to an actual file path.
138142 *
@@ -200,6 +204,55 @@ export function resolveRelativeImport(
200204 return null
201205}
202206
207+ function normalizeInternalImportTarget ( target : InternalImportTarget ) : string | null {
208+ if ( typeof target === 'string' ) {
209+ return target
210+ }
211+
212+ if ( target && typeof target === 'object' ) {
213+ if ( typeof target . import === 'string' ) {
214+ return target . import
215+ }
216+
217+ if ( typeof target . default === 'string' ) {
218+ return target . default
219+ }
220+ }
221+
222+ return null
223+ }
224+
225+ /**
226+ * import ... from '#components/Button.vue'
227+ * import ... from '#/components/Button.vue'
228+ * import ... from '~/components/Button.vue'
229+ * import ... from '~components/Button.vue'
230+ */
231+ export function resolveInternalImport (
232+ specifier : string ,
233+ imports : InternalImportsMap | undefined ,
234+ files : FileSet ,
235+ ) : ResolvedImport | null {
236+ const cleanSpecifier = specifier . replace ( / ^ [ ' " ] | [ ' " ] $ / g, '' ) . trim ( )
237+
238+ if ( ( ! cleanSpecifier . startsWith ( '#' ) && ! cleanSpecifier . startsWith ( '~' ) ) || ! imports ) {
239+ return null
240+ }
241+
242+ const target = normalizeInternalImportTarget ( imports [ cleanSpecifier ] )
243+ console . log ( 'resolved internal import' , imports , cleanSpecifier , target )
244+ if ( ! target || ! target . startsWith ( './' ) ) {
245+ return null
246+ }
247+
248+ const path = normalizePath ( target )
249+ if ( ! path || path . startsWith ( '..' ) || ! files . has ( path ) ) {
250+ return null
251+ }
252+
253+ return { path }
254+ }
255+
203256/**
204257 * Create a resolver function bound to a specific file tree and current file.
205258 */
@@ -208,9 +261,13 @@ export function createImportResolver(
208261 currentFile : string ,
209262 packageName : string ,
210263 version : string ,
264+ internalImports ?: InternalImportsMap ,
211265) : ( specifier : string ) => string | null {
212266 return ( specifier : string ) => {
213- const resolved = resolveRelativeImport ( specifier , currentFile , files )
267+ const relativeResolved = resolveRelativeImport ( specifier , currentFile , files )
268+ const internalResolved = resolveInternalImport ( specifier , internalImports , files )
269+ const resolved = relativeResolved ?? internalResolved
270+
214271 if ( resolved ) {
215272 return `/package-code/${ packageName } /v/${ version } /${ resolved . path } `
216273 }
0 commit comments