Skip to content

Commit

Permalink
fix(nuxt): support import submodules for @vueuse/integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 20, 2022
1 parent 3a8ccb0 commit 18ba05e
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 18 deletions.
4 changes: 4 additions & 0 deletions package.json
Expand Up @@ -50,6 +50,10 @@
"@vitest/ui": "^0.18.0",
"@vue/compiler-sfc": "^3.2.37",
"@vue/test-utils": "^2.0.2",
"@vueuse/core": "./packages/core",
"@vueuse/integrations": "./packages/integrations",
"@vueuse/math": "./packages/math",
"@vueuse/shared": "./packages/shared",
"axios": "^0.27.2",
"bumpp": "^8.2.1",
"consola": "^2.15.3",
Expand Down
3 changes: 3 additions & 0 deletions packages/metadata/scripts/update.ts
Expand Up @@ -113,6 +113,9 @@ export async function readMetadata() {
if (related?.length)
fn.related = related

if (pkg.submodules)
fn.importPath = `${pkg.name}/${fn.name}`

indexes.functions.push(fn)
}))
}
Expand Down
1 change: 1 addition & 0 deletions packages/metadata/types.ts
Expand Up @@ -22,6 +22,7 @@ export interface PackageManifest {
export interface VueUseFunction {
name: string
package: string
importPath?: string
lastUpdated?: number
category?: string
description?: string
Expand Down
36 changes: 20 additions & 16 deletions packages/nuxt/index.ts
Expand Up @@ -3,6 +3,7 @@ import { fileURLToPath } from 'url'
import { isPackageExists } from 'local-pkg'
import { defineNuxtModule } from '@nuxt/kit'
import { metadata } from '@vueuse/metadata'
import type { Import } from 'unimport'

const _dirname = dirname(fileURLToPath(import.meta.url))

Expand All @@ -23,6 +24,8 @@ const packages = [
'firebase',
'rxjs',
'sound',
'math',
'integrations',
]

const fullPackages = packages.map(p => `@vueuse/${p}`)
Expand Down Expand Up @@ -100,24 +103,25 @@ export default defineNuxtModule<VueUseNuxtOptions>({
if (!isPackageExists(`@vueuse/${pkg}`))
continue

const functions = metadata
const imports = metadata
.functions
.filter(i => (i.package === 'core' || i.package === 'shared') && !i.internal)

if (functions.length) {
const imports = metadata
.functions
.filter(i => i.package === pkg && !i.internal)
.flatMap(i => [i.name, ...i.alias || []])
.filter(i => i.length >= 4 && !disabledFunctions.includes(i))

sources.push({
from: `@vueuse/${pkg}`,
names: imports,
imports,
priority: -1,
.filter(i => i.package === pkg && !i.internal)
.flatMap((i): Import[] => {
const names = [i.name, ...i.alias || []]
return names.map(n => ({
from: `@vueuse/${i.importPath || i.package}`,
name: n,
as: n,
priority: -1,
}))
})
}
.filter(i => i.name.length >= 4 && !disabledFunctions.includes(i.name))

sources.push({
from: '@vueuse/core',
imports,
priority: -1,
})
}
})
}
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/package.json
Expand Up @@ -41,6 +41,7 @@
"vue-demi": "*"
},
"devDependencies": {
"@nuxt/schema": "^3.0.0-rc.4"
"@nuxt/schema": "^3.0.0-rc.4",
"unimport": "^0.4.5"
}
}
1 change: 1 addition & 0 deletions playgrounds/nuxt3/.npmrc
@@ -0,0 +1 @@
shamefully-hoist=true
2 changes: 2 additions & 0 deletions playgrounds/nuxt3/app.vue
Expand Up @@ -6,6 +6,8 @@ const mounted = ref(false)
const { width, height } = useWindowSize()
const color = useColorMode()
const data = useAxios('/api/data')
onMounted(() => {
mounted.value = true
})
Expand Down
2 changes: 2 additions & 0 deletions playgrounds/nuxt3/nuxt.config.mjs
Expand Up @@ -11,6 +11,8 @@ export default defineNuxtConfig({
alias: {
'@vueuse/core': resolve(__dirname, '../../packages/core/index.ts'),
'@vueuse/shared': resolve(__dirname, '../../packages/shared/index.ts'),
'@vueuse/math': resolve(__dirname, '../../packages/math/index.ts'),
'@vueuse/integrations': resolve(__dirname, '../../packages/integrations/index.ts'),
},
vueuse: {
ssrHandlers: true,
Expand Down
6 changes: 5 additions & 1 deletion playgrounds/nuxt3/package.json
@@ -1,11 +1,15 @@
{
"private": true,
"packageManager": "pnpm@7.5.0",
"scripts": {
"dev": "nuxi dev",
"build": "nuxi build",
"start": "node .output/server/index.mjs"
},
"devDependencies": {
"nuxt": "^3.0.0-rc.4"
"@vueuse/core": "../../packages/core",
"@vueuse/integrations": "../../packages/integrations",
"@vueuse/shared": "../../packages/shared",
"nuxt": "^3.0.0-rc.6"
}
}
77 changes: 77 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 18ba05e

Please sign in to comment.