Skip to content

Commit 6702321

Browse files
committed
feat: supplement the path of components and styles
1 parent 1a5eddf commit 6702321

File tree

6 files changed

+29
-17
lines changed

6 files changed

+29
-17
lines changed

playground/app.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ onMounted(() => {
169169
@close="showCascader = false"
170170
/>
171171
</van-popup>
172-
<van-floating-panel
172+
<!-- <van-floating-panel
173173
v-model:show="showFloatingPanel"
174174
:content-draggable="false"
175175
:lock-scroll="true"
@@ -178,7 +178,7 @@ onMounted(() => {
178178
<van-cell-group>
179179
<van-cell v-for=" (n, i) in 150" :key="i" :title="n" :value="n" />
180180
</van-cell-group>
181-
</van-floating-panel>
181+
</van-floating-panel> -->
182182

183183
<van-back-top bottom="70" />
184184
<van-sticky position="bottom">

playground/nuxt.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { defineNuxtConfig } from 'nuxt/config'
22
import Vant from '..'
33

44
export default defineNuxtConfig({
5+
devtools: true,
56
modules: [Vant],
67
vant: {
78
lazyload: true

src/core/components.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
import { addComponent } from '@nuxt/kit'
1+
import { addComponent, createResolver } from '@nuxt/kit'
22
import { libraryName } from '../config'
33
import { hyphenate, toArray } from '../utils'
44
import type { Options } from '../types'
55

66
export function resolveComponents (config: Options) {
77
const { components, excludeExports } = config
8+
const { resolvePath } = createResolver(import.meta.url)
89

9-
components.forEach((item) => {
10+
components.forEach(async (item) => {
1011
const [name, alias, from] = toArray(item)
1112
if (excludeExports.includes(name)) { return }
1213
const filePath =
1314
!from || from === libraryName
14-
? `${libraryName}/es/${hyphenate(name)}/${name}`
15+
? `${libraryName}/es/${hyphenate(name)}/${name}.mjs`
1516
: from
1617

1718
addComponent({
1819
name: alias || `Van${name}`,
19-
filePath
20+
filePath: await resolvePath(filePath)
2021
})
2122
})
2223
}

src/core/options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { libraryName } from '../config'
33

44
export function resolveOptions () {
55
const nuxt = useNuxt()
6+
const regExp = new RegExp(`${libraryName}/es/.*/style/index.mjs`)
67

7-
nuxt.options.build.transpile.push(libraryName)
8+
nuxt.options.build.transpile.push(regExp)
89
}

src/core/styles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { hyphenate } from '../utils'
33
import type { Options } from '../types'
44

55
export function getStyleDir (name: string) {
6-
return `${libraryName}/es/${hyphenate(name)}/style/index`
6+
return `${libraryName}/es/${hyphenate(name)}/style/index.mjs`
77
}
88

99
export function resolveStyles (config: Options, name: string) {

src/core/transformPlugin.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createUnplugin } from 'unplugin'
22
import MagicString from 'magic-string'
33
import type { NuxtOptions } from '@nuxt/schema'
4+
import { createResolver } from '@nuxt/kit'
45
import { allImportsWithStyle, libraryName } from '../config'
56
import { camelize, genSideEffectsImport, toRegExp } from '../utils'
67
import type { TransformOptions } from '../types'
@@ -28,25 +29,33 @@ export const transformPlugin = createUnplugin((options: PluginOptions) => {
2829
return true
2930
}
3031
},
31-
transform (code, id) {
32-
const imports = new Set<string>()
32+
async transform (code, id) {
33+
const { resolvePath } = createResolver(import.meta.url)
34+
const styles = new Set<string>()
3335
const s = new MagicString(code)
3436

35-
const addStyles = (style?: string) => {
36-
style && imports.add(genSideEffectsImport(style))
37-
}
38-
3937
s.replace(componentsRegExp, (full, lazy, name) => {
40-
addStyles(transformStyles(camelize(name)))
38+
styles.add(camelize(name))
4139
return full
4240
})
4341

4442
s.replace(importsRegExp, (full, name) => {
45-
addStyles(transformStyles(name))
43+
styles.add(name)
4644
return full
4745
})
4846

49-
if (imports.size) {
47+
if (styles.size) {
48+
const imports: string[] = []
49+
50+
for await (const name of styles) {
51+
const style = transformStyles(name)
52+
if (style) {
53+
const path = await resolvePath(style)
54+
const importPath = genSideEffectsImport(path)
55+
imports.push(importPath)
56+
}
57+
}
58+
5059
s.prepend([...imports, ''].join('\n'))
5160
}
5261

0 commit comments

Comments
 (0)