Skip to content

Commit

Permalink
chore: try rewrite resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
vtrbo committed Aug 1, 2023
1 parent bc7d967 commit 3ed9e47
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 32 deletions.
7 changes: 5 additions & 2 deletions examples/vite-vue3/App.vue
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import Account from '~images:others/account.png'
import AccountP from '~images:others/account.png'

Check warning on line 2 in examples/vite-vue3/App.vue

View workflow job for this annotation

GitHub Actions / lint

'~images:others/account.png' imported multiple times
import AccountS from '~images/account.svg'
import Password from '~images:normal/password.png'
import OA from '~images:others/account.png'

Check warning on line 5 in examples/vite-vue3/App.vue

View workflow job for this annotation

GitHub Actions / lint

'~images:others/account.png' imported multiple times
Expand All @@ -11,7 +11,10 @@ import Test2Password from '~images/test/test/password.png?gif&width=100&height=1

<template>
<div>
<Account width="150" @click="() => console.log('on click')" />
<account />
<account-png />
<normal-account />
<AccountP width="150" @click="() => console.log('on click')" />
<AccountS />
<OA />
<Password />
Expand Down
31 changes: 11 additions & 20 deletions examples/vite-vue3/vite.config.ts
@@ -1,36 +1,27 @@
import process from 'node:process'
import type { UserConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import VueImages from 'unplugin-vue-images/vite'
import Components from 'unplugin-vue-components/vite'
import createDebugger from 'debug'

import fg from 'fast-glob'
import VueImages from 'unplugin-vue-images/vite'
import { ImagesResolver } from 'unplugin-vue-images/resolver'

const debug = createDebugger('unplugin-vue-images:unplugin')
const collectionDirs = [
'src/assets/images',
{ others: 'src/assets/others' },
]

const config: UserConfig = {
plugins: [
Vue(),
VueImages({
dirs: ['src/assets/images', { others: 'src/assets/others' }],
dirs: collectionDirs,
compiler: 'vue3',
}),
Components({
resolvers: [
(name: string) => {
// debug('name =>', name)
const dirs = [{ alias: 'normal', path: 'src/assets/images' }, { alias: 'others', path: 'src/assets/others' }]
const extensions = ['jpg', 'jpeg', 'png', 'svg', 'gif', 'webp'].join(',')
const globs = dirs.map(dir => `${dir.path}/**/*.{${extensions}}`)
const files = fg.sync(globs, {
ignore: ['**/node_modules/**'],
onlyFiles: true,
cwd: process.cwd(),
})
// debug('files => ', files)
return ''
},
ImagesResolver({
prefix: false,
dirs: collectionDirs,
}),
],
}),
],
Expand Down
9 changes: 4 additions & 5 deletions package.json
Expand Up @@ -55,11 +55,10 @@
"require": "./dist/types.cjs",
"import": "./dist/types.js"
},
"./types/vue": {
"types": "./types/vue.d.ts"
},
"./types/vue3": {
"types": "./types/vue3.d.ts"
"./resolver": {
"types": "./dist/resolver.d.ts",
"require": "./dist/resolver.cjs",
"import": "./dist/resolver.js"
},
"./*": "./*"
},
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts → src/core/constants.ts
Expand Up @@ -5,3 +5,5 @@ export const DEFAULT_ALIAS = 'normal'
export const DEFAULT_PATH = 'src/assets/images'

export const DEFAULT_EXTENSIONS = ['jpg', 'jpeg', 'png', 'svg', 'webp', 'gif']

export const DEFAULT_PREFIX = 'img'
4 changes: 3 additions & 1 deletion src/core/context.ts
Expand Up @@ -6,7 +6,7 @@ import type { ViteDevServer } from 'vite'
import { ensurePrefix, toCamelCase } from '@vtrbo/utils/string'
import createDebugger from 'debug'
import type { Options, ResolvedOptions } from '../types'
import { UNPLUGIN_NAME } from '../constants'
import { UNPLUGIN_NAME } from './constants'
import { resolveOptions } from './options'
import { getAlias, getName } from './utils'

Expand Down Expand Up @@ -200,9 +200,11 @@ export class Context {
setupWatcher(watcher: fs.FSWatcher) {
watcher
.on('add', (path) => {
debug('setupWatcher add path =>', path)
this.addImage(path)
})
.on('unlink', (path) => {
debug('setupWatcher unlink path =>', path)
this.delImage(path)
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/loader.ts
@@ -1,6 +1,6 @@
import createDebugger from 'debug'
import { DEFAULT_ALIAS, DEFAULT_PATH, UNPLUGIN_NAME } from '../constants'
import type { ResolvedOptions } from '../types'
import { DEFAULT_ALIAS, DEFAULT_PATH, UNPLUGIN_NAME } from './constants'
import type { Context } from './context'
import { compilers } from './compilers'

Expand Down
2 changes: 1 addition & 1 deletion src/core/options.ts
@@ -1,7 +1,7 @@
import { isArray, isObject, isString } from '@vtrbo/utils/fn'
import createDebugger from 'debug'
import type { Dir, Options, ResolvedOptions } from '../types'
import { DEFAULT_ALIAS, DEFAULT_EXTENSIONS, DEFAULT_PATH, UNPLUGIN_NAME } from '../constants'
import { DEFAULT_ALIAS, DEFAULT_EXTENSIONS, DEFAULT_PATH, UNPLUGIN_NAME } from './constants'
import { removeSlash } from './utils'

const debug = createDebugger(`${UNPLUGIN_NAME}:options`)
Expand Down
14 changes: 13 additions & 1 deletion src/core/utils.ts
@@ -1,6 +1,6 @@
import createDebugger from 'debug'
import type { ResolvedOptions } from '../types'
import { DEFAULT_ALIAS, UNPLUGIN_NAME } from '../constants'
import { DEFAULT_ALIAS, UNPLUGIN_NAME } from './constants'

const debug = createDebugger(`${UNPLUGIN_NAME}:utils`)

Check warning on line 5 in src/core/utils.ts

View workflow job for this annotation

GitHub Actions / lint

'debug' is assigned a value but never used. Allowed unused vars must match /^_/u

Expand Down Expand Up @@ -38,3 +38,15 @@ export function removeSlash(path: string) {
path = path.slice(0, path.length - 1)
return path
}

export function pathsEqual(oldPaths: string[], newPaths: string[]) {
if (oldPaths.length !== newPaths.length)
return false

for (let i = 0; i < oldPaths.length; i++) {
if (!oldPaths[i].includes(newPaths[i]))
return false
}

return true
}
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -3,7 +3,7 @@ import chokidar from 'chokidar'
import createDebugger from 'debug'
import type { ResolvedConfig, ViteDevServer } from 'vite'
import type { Options } from './types'
import { UNPLUGIN_NAME } from './constants'
import { UNPLUGIN_NAME } from './core/constants'
import { generateComponentFromPath, generateImagePath, isImagePath, normalizeImagePath } from './core/loader'
import { Context } from './core/context'

Expand Down
97 changes: 97 additions & 0 deletions src/resolver.ts
@@ -0,0 +1,97 @@
import { isArray, isObject, isString } from '@vtrbo/utils/fn'
import { toLinesCase } from '@vtrbo/utils/string'
import createDebugger from 'debug'
import type { Dir, Options } from './types'
import { DEFAULT_ALIAS, DEFAULT_EXTENSIONS, DEFAULT_PATH, DEFAULT_PREFIX, UNPLUGIN_NAME } from './core/constants'
import { removeSlash } from './core/utils'

const debug = createDebugger(`${UNPLUGIN_NAME}:resolver`)

export interface ImagesResolverOptions extends Omit<Options, 'compiler'> {
/**
* Prefix for resolving components name.
* Set '' to disable prefix.
*
* @default 'img'
*/
prefix?: string | false
}

interface ResolvedImagesResolverOptions extends Omit<Required<ImagesResolverOptions>, 'dirs'> {
dirs: Dir[]
}

/**
* Resolver for unplugin-vue-components and unplugin-auto-import
*
* @param options
*/
export function ImagesResolver(options: ImagesResolverOptions = {}) {
const resolvedOptions = resolveOptions(options)
debug('ImagesResolver resolvedOptions =>', resolvedOptions)

return (name: string) => {
debug('ImagesResolver name =>', name)

return ''
}
}

function resolveOptions(options: ImagesResolverOptions = {}): ResolvedImagesResolverOptions {
const rawPrefix = options.prefix ?? DEFAULT_PREFIX
const prefix = rawPrefix ? `${toLinesCase(rawPrefix)}-` : ''

const dirs: Dir[] = []

if (options?.dirs) {
if (isString(options?.dirs)) {
dirs.push({
alias: DEFAULT_ALIAS,
path: removeSlash(options?.dirs),
})
}
else if (isArray(options?.dirs)) {
options?.dirs.forEach((dir) => {
if (isString(dir)) {
dirs.push({
alias: DEFAULT_ALIAS,
path: removeSlash(dir),
})
}
else {
for (const alias in dir) {
dirs.push({
alias,
path: removeSlash(dir[alias]),
})
}
}
})
}
else if (isObject(options?.dirs)) {
for (const alias in options?.dirs) {
dirs.push({
alias,
path: removeSlash(options?.dirs[alias]),
})
}
}
}

if (!dirs.length) {
dirs.push({
alias: DEFAULT_ALIAS,
path: removeSlash(DEFAULT_PATH),
})
}

const extensions = options?.extensions || DEFAULT_EXTENSIONS

return {
prefix,
dirs,
extensions,
}
}

export default ImagesResolver

0 comments on commit 3ed9e47

Please sign in to comment.