Skip to content

Commit be62eab

Browse files
committed
refactor: simpfied for vite 2
1 parent d0143be commit be62eab

File tree

11 files changed

+22
-136
lines changed

11 files changed

+22
-136
lines changed

example/src/components/ComponentA.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
import { defineProps } from 'vue'
77
88
const props = defineProps({
9-
msg: String
9+
msg: String,
1010
})
1111
</script>

example/src/components/ComponentB.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
import { defineProps } from 'vue'
77
88
const props = defineProps({
9-
msg: String
9+
msg: String,
1010
})
1111
</script>

example/src/components/ComponentD.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<h3>Component D: <component-b msg="wrapper"/></h3>
2+
<h3>Component D: <component-b msg="wrapper" /></h3>
33
</template>
44

55
<script lang='ts'>

example/src/components/component-c.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
import { defineProps } from 'vue'
77
88
const props = defineProps({
9-
msg: String
9+
msg: String,
1010
})
1111
</script>

example/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createApp } from 'vue'
22
import App from '/src/App.vue'
33
import './index.css'
4-
// import 'vant/lib/index.css'
4+
import '../node_modules/vant/lib/index.css'
55

66
createApp(App).mount('#app')

src/context.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export class Context {
160160
return await Promise.resolve(this._importsResolveTasks[key][0])
161161
}
162162

163-
private _searchGlob: Promise<void> | undefined
163+
private _searched = 0
164164

165165
/**
166166
* This search for components in with the given options.
@@ -170,13 +170,14 @@ export class Context {
170170
* @param ctx
171171
* @param force
172172
*/
173-
async searchGlob() {
174-
if (!this._searchGlob) {
175-
this._searchGlob = (async() => {
176-
await searchComponents(this)
177-
})()
173+
searchGlob(forceMs = -1) {
174+
if (this._searched && forceMs < 0)
175+
return
176+
177+
const now = +new Date()
178+
if (now - this._searched > forceMs) {
179+
searchComponents(this)
180+
this._searched = now
178181
}
179-
180-
return await this._searchGlob
181182
}
182183
}

src/fs/glob.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { Context } from '../context'
44

55
const debug = Debug('vite-plugin-components:glob')
66

7-
export async function searchComponents(ctx: Context) {
8-
debug(`searching start with: [${ctx.globs.join(', ')}]`)
7+
export function searchComponents(ctx: Context) {
8+
debug(`started with: [${ctx.globs.join(', ')}]`)
99

10-
const files = await fg(ctx.globs, {
10+
const files = fg.sync(ctx.globs, {
1111
ignore: ['node_modules'],
1212
onlyFiles: true,
1313
})

src/generator/importer.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/index.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import type { Plugin } from 'vite'
22
import { Options } from './types'
33
import { Context } from './context'
44
import { VueTransformer } from './transforms/vue'
5-
import { CustomComponentTransformer } from './transforms/customComponent'
65
import { parseId, resolveOptions } from './utils'
7-
import { generateResolver, isResolverPath } from './generator/importer'
86

97
const defaultOptions: Required<Options> = {
108
dirs: 'src/components',
@@ -25,26 +23,13 @@ function VitePluginComponents(options: Options = {}): Plugin {
2523

2624
const transformer = [
2725
VueTransformer(ctx),
28-
CustomComponentTransformer(ctx),
2926
]
3027

3128
return {
3229
name: 'vite-plugin-components',
33-
resolveId(source) {
34-
if (isResolverPath(source))
35-
return source
36-
return null
37-
},
3830
configResolved(config) {
3931
ctx.viteConfig = config
4032
},
41-
async load(id) {
42-
if (isResolverPath(id)) {
43-
await ctx.searchGlob()
44-
return await generateResolver(ctx, id.slice(1)) // remove the heading '/'
45-
}
46-
return null
47-
},
4833
transform(code, id) {
4934
const { path, query } = parseId(id)
5035
for (const trans of transformer)

src/transforms/customComponent.ts

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)