Skip to content

Commit 12614a9

Browse files
authored
feat: group assets in duplicate paths between root and public by relativePath (#514)
1 parent 0796768 commit 12614a9

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

packages/client/src/pages/assets.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const filtered = computed(() => {
4444
const byFolders = computed(() => {
4545
const result: Record<string, AssetInfo[]> = {}
4646
for (const asset of filtered.value) {
47-
const folder = `${asset.path.split('/').slice(0, -1).join('/')}/`
47+
const folder = `${asset.relativePath.split('/').slice(0, -1).join('/')}/`
4848
if (!result[folder])
4949
result[folder] = []
5050
result[folder].push(asset)
@@ -71,7 +71,7 @@ const byTree = computed(() => {
7171
}
7272
7373
filtered.value.forEach((file) => {
74-
const pathParts = file.path.split('/').filter(part => part !== '')
74+
const pathParts = file.relativePath.split('/').filter(part => part !== '')
7575
addToTree(root, pathParts, file)
7676
})
7777

packages/core/src/rpc/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface AssetInfo {
66
path: string
77
type: AssetType
88
publicPath: string
9+
relativePath: string
910
filePath: string
1011
size: number
1112
mtime: number
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# asset-in-root-assets
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# asset-in-public-assets

packages/vite/src/rpc/assets.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { debounce } from 'perfect-debounce'
33
import { getViteRpcServer } from '@vue/devtools-kit'
44
import type { AssetImporter, AssetInfo, AssetType, ImageMeta, ViteRPCFunctions } from '@vue/devtools-core'
55
import fg from 'fast-glob'
6-
import { join, resolve } from 'pathe'
6+
import { join, relative, resolve } from 'pathe'
77
import { imageMeta } from 'image-meta'
88
import { RpcFunctionCtx } from './types'
99

@@ -63,6 +63,11 @@ export function getAssetsFunctions(ctx: RpcFunctionCtx) {
6363
async function scan() {
6464
const dir = resolve(config.root)
6565
const baseURL = config.base
66+
67+
// publicDir in ResolvedConfig is an absolute path
68+
const publicDir = config.publicDir
69+
const relativePublicDir = publicDir === '' ? '' : `${relative(dir, publicDir)}/`
70+
6671
const files = await fg([
6772
// image
6873
'**/*.(png|jpg|jpeg|gif|svg|webp|avif|ico|bmp|tiff)',
@@ -89,16 +94,17 @@ export function getAssetsFunctions(ctx: RpcFunctionCtx) {
8994
],
9095
})
9196

92-
cache = await Promise.all(files.map(async (path) => {
93-
const filePath = resolve(dir, path)
97+
cache = await Promise.all(files.map(async (relativePath) => {
98+
const filePath = resolve(dir, relativePath)
9499
const stat = await fsp.lstat(filePath)
95100
// remove public prefix to resolve vite assets warning
96-
path = path.startsWith('public/') ? path.slice(7) : path
101+
const path = relativePath.replace(relativePublicDir, '')
97102
return {
98103
path,
104+
relativePath,
99105
publicPath: join(baseURL, path),
100106
filePath,
101-
type: guessType(path),
107+
type: guessType(relativePath),
102108
size: stat.size,
103109
mtime: stat.mtimeMs,
104110
}

0 commit comments

Comments
 (0)