Skip to content

Commit

Permalink
feat: group assets in duplicate paths between root and public by rela…
Browse files Browse the repository at this point in the history
…tivePath (#514)
  • Loading branch information
LoTwT authored Jul 17, 2024
1 parent 0796768 commit 12614a9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/client/src/pages/assets.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const filtered = computed(() => {
const byFolders = computed(() => {
const result: Record<string, AssetInfo[]> = {}
for (const asset of filtered.value) {
const folder = `${asset.path.split('/').slice(0, -1).join('/')}/`
const folder = `${asset.relativePath.split('/').slice(0, -1).join('/')}/`
if (!result[folder])
result[folder] = []
result[folder].push(asset)
Expand All @@ -71,7 +71,7 @@ const byTree = computed(() => {
}
filtered.value.forEach((file) => {
const pathParts = file.path.split('/').filter(part => part !== '')
const pathParts = file.relativePath.split('/').filter(part => part !== '')
addToTree(root, pathParts, file)
})
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/rpc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface AssetInfo {
path: string
type: AssetType
publicPath: string
relativePath: string
filePath: string
size: number
mtime: number
Expand Down
1 change: 1 addition & 0 deletions packages/playground/basic/assets/asset-in-root-assets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# asset-in-root-assets
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# asset-in-public-assets
16 changes: 11 additions & 5 deletions packages/vite/src/rpc/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { debounce } from 'perfect-debounce'
import { getViteRpcServer } from '@vue/devtools-kit'
import type { AssetImporter, AssetInfo, AssetType, ImageMeta, ViteRPCFunctions } from '@vue/devtools-core'
import fg from 'fast-glob'
import { join, resolve } from 'pathe'
import { join, relative, resolve } from 'pathe'
import { imageMeta } from 'image-meta'
import { RpcFunctionCtx } from './types'

Expand Down Expand Up @@ -63,6 +63,11 @@ export function getAssetsFunctions(ctx: RpcFunctionCtx) {
async function scan() {
const dir = resolve(config.root)
const baseURL = config.base

// publicDir in ResolvedConfig is an absolute path
const publicDir = config.publicDir
const relativePublicDir = publicDir === '' ? '' : `${relative(dir, publicDir)}/`

const files = await fg([
// image
'**/*.(png|jpg|jpeg|gif|svg|webp|avif|ico|bmp|tiff)',
Expand All @@ -89,16 +94,17 @@ export function getAssetsFunctions(ctx: RpcFunctionCtx) {
],
})

cache = await Promise.all(files.map(async (path) => {
const filePath = resolve(dir, path)
cache = await Promise.all(files.map(async (relativePath) => {
const filePath = resolve(dir, relativePath)
const stat = await fsp.lstat(filePath)
// remove public prefix to resolve vite assets warning
path = path.startsWith('public/') ? path.slice(7) : path
const path = relativePath.replace(relativePublicDir, '')
return {
path,
relativePath,
publicPath: join(baseURL, path),
filePath,
type: guessType(path),
type: guessType(relativePath),
size: stat.size,
mtime: stat.mtimeMs,
}
Expand Down

0 comments on commit 12614a9

Please sign in to comment.