Skip to content

Commit

Permalink
feat: new Nuxt package (vitest-dev#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 8, 2021
1 parent 88db283 commit a115cf6
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 25 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,18 @@ Refer to [functions list](https://vueuse.org/functions) or [documentations](http
npm i @vueuse/core
```

Vue 3 Demo: [Vite](https://github.com/vueuse/vueuse-vite-starter), [Webpack](https://github.com/vueuse/vueuse-vue3-example) / Vue 2 Demo: [Vue CLI](https://github.com/vueuse/vueuse-vue2-example)
[Add ons](https://vueuse.org/add-ons.html) | [Nuxt Module](https://vueuse.org/guide/index.html#nuxt)

> From v6.0, VueUse requires `vue` >= v3.2 or `@vue/composition-api` >= v1.1
###### Demos

- [Vite + Vue 3](https://github.com/vueuse/vueuse-vite-starter)
- [Nuxt 3 + Vue 3](https://github.com/antfu/vitesse-nuxt3)
- [Webpack + Vue 3](https://github.com/vueuse/vueuse-vue3-example)
- [Nuxt 2 + Vue 2](https://github.com/antfu/vitesse-nuxt-bridge)
- [Vue CLI + Vue 2](https://github.com/vueuse/vueuse-vue2-example)

### CDN

```html
Expand Down
13 changes: 13 additions & 0 deletions meta/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ export const packages: PackageManifest[] = [
'@vueuse/shared',
],
},
{
name: 'nuxt',
display: 'Nuxt',
description: 'VueUse Nuxt Module',
addon: true,
iife: false,
manualImport: true,
external: [
'@vueuse/core',
'@vueuse/shared',
'nuxt3',
],
},
{
name: 'router',
display: 'Router',
Expand Down
3 changes: 3 additions & 0 deletions packages/add-ons.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ Vue composables for playing sound effects.
<!--GENERATED LIST, DO NOT MODIFY MANUALLY-->
<!--ADDONS_LIST_STARTS-->
## Nuxt - [`@vueuse/nuxt`](https://vueuse.org/nuxt/README.html)
VueUse Nuxt Module

## Router - [`@vueuse/router`](https://vueuse.org/router/README.html)
Utilities for vue-router
- [`useRouteHash`](https://vueuse.org/router/useRouteHash/) — shorthand for reactive route.hash
Expand Down
1 change: 1 addition & 0 deletions packages/core/nuxt.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function(): void
4 changes: 3 additions & 1 deletion packages/core/nuxt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url))
const disabled = [
'useFetch',
'toRefs',
'useCookie'
'useCookie',
]

/**
Expand All @@ -26,6 +26,8 @@ const disabled = [
export default function() {
const { nuxt } = this

console.log('[@vueuse/core] Installing Nuxt module with `@vueuse/core/nuxt` is deprecated. Please use `@vueuse/nuxt` instead.')

// opt-out Vite deps optimization for VueUse
nuxt.hook('vite:extend', ({ config }) => {
config.optimizeDeps = config.optimizeDeps || {}
Expand Down
20 changes: 16 additions & 4 deletions packages/guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ VueUse is a collection of utility functions based on [Composition API](https://v

## Installation

> 🎩 VueUse works for Vue 2 & 3 **within a single package** by the power of [vue-demi](https://github.com/vueuse/vue-demi)!
> 🎩 From v4.0, it works for Vue 2 & 3 **within a single package** by the power of [vue-demi](https://github.com/vueuse/vue-demi)!
```bash
npm i @vueuse/core
```

Vue 3 Demo: [Vite](https://github.com/vueuse/vueuse-vite-starter), [Webpack](https://github.com/vueuse/vueuse-vue3-example) / Vue 2 Demo: [Vue CLI](https://github.com/vueuse/vueuse-vue2-example)
[Add ons](/add-ons.html) | [Nuxt Module](/guide/index.html#nuxt)

> From v6.0, VueUse requires `vue` >= v3.2 or `@vue/composition-api` >= v1.1
###### Demos

- [Vite + Vue 3](https://github.com/vueuse/vueuse-vite-starter)
- [Nuxt 3 + Vue 3](https://github.com/antfu/vitesse-nuxt3)
- [Webpack + Vue 3](https://github.com/vueuse/vueuse-vue3-example)
- [Nuxt 2 + Vue 2](https://github.com/antfu/vitesse-nuxt-bridge)
- [Vue CLI + Vue 2](https://github.com/vueuse/vueuse-vue2-example)

### CDN

```html
Expand All @@ -25,13 +33,17 @@ It will be exposed to global as `window.VueUse`

### Nuxt

From v6.7.0, we shipped a Nuxt module to enable auto importing for Nuxt 3 and Nuxt Bridge.
From v7.2.0, we shipped a Nuxt module to enable auto importing for Nuxt 3 and Nuxt Bridge.

```bash
npm i -D @vueuse/nuxt
```

```ts
// nuxt.config.js
export default {
buildModules: [
'@vueuse/core/nuxt'
'@vueuse/nuxt'
]
}
```
Expand Down
34 changes: 34 additions & 0 deletions packages/nuxt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# @vueuse/nuxt

[![NPM version](https://img.shields.io/npm/v/@vueuse/nuxt?color=a1b858)](https://www.npmjs.com/package/@vueuse/nuxt)

> This is an add-on of [VueUse](https://github.com/vueuse/vueuse), provides Nuxt specific composables and auto-import capabilities.
> Experimental. **Will NOT follow semvar**.
## Install

<pre class='language-bash'>
npm i <b>@vueuse/nuxt</b>
</pre>

```ts
// nuxt.config

export function defineNuxtConfig({
buildModules: [
'@vueuse/nuxt'
]
})
```

## Functions

`@vueuse/nuxt` provides the following functions

<!--GENERATED LIST, DO NOT MODIFY MANUALLY-->
<!--FUNCTIONS_LIST_STARTS--><!--FUNCTIONS_LIST_ENDS-->

## License

[MIT License](https://github.com/vueuse/vueuse/blob/master/LICENSE) © 2021-PRESENT [Anthony Fu](https://github.com/antfu)
6 changes: 6 additions & 0 deletions packages/nuxt/module.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// CommonJS proxy to bypass jiti transforms from nuxt 2 and using native ESM
module.exports = function(...args) {
return import('./module.mjs').then(m => m.default.call(this, ...args))
}

module.exports.meta = require('./package.json')
1 change: 1 addition & 0 deletions packages/nuxt/module.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function(): void
103 changes: 103 additions & 0 deletions packages/nuxt/module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import fs from 'fs'
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
import { isPackageExists } from 'local-pkg'

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

const disabledFunctions = [
'useFetch',
'toRefs',
'useCookie',
]

const packages = [
'core',
'shared',
'nuxt',
'integrations',
'components',
'motion',
'firebase',
'rxjs',
'sound',
'head',
]

const fullPackages = packages.map(p => `@vueuse/${p}`)

/**
* Auto import for VueUse in Nuxt
* Usage:
*
* ```ts
* // nuxt.config.js
* export deafult {
* buildModules: [
* '@vueuse/nuxt'
* ]
* }
* ```
*/
export default function() {
const { nuxt } = this

// opt-out Vite deps optimization for VueUse
nuxt.hook('vite:extend', ({ config }) => {
config.optimizeDeps = config.optimizeDeps || {}
config.optimizeDeps.exclude = config.optimizeDeps.exclude || []
config.optimizeDeps.exclude.push(...fullPackages)
})

// add @vueuse/nuxt to transpile target for alias resolution
nuxt.options.build = nuxt.options.build || {}
nuxt.options.build.transpile = nuxt.options.build.transpile || []
nuxt.options.build.transpile.push('@vueuse/nuxt')

let indexes

// auto Import
nuxt.hook('autoImports:sources', (sources) => {
if (sources.find(i => fullPackages.includes(i.from)))
return

if (!indexes) {
try {
indexes = JSON.parse(fs.readFileSync(resolve(__dirname, './indexes.json'), 'utf-8'))
indexes.functions.forEach((i) => {
if (i.package === 'shared')
i.package = 'core'
})
}
catch (e) {
throw new Error('[@vueuse/nuxt] Failed to load indexes.json')
}
}

if (!indexes)
return

for (const pkg of packages) {
if (pkg === 'core')
continue

if (!isPackageExists(`@vueuse/${pkg}`))
continue

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

if (functions.length) {
sources.push({
from: `@vueuse/${pkg}`,
names: indexes
.functions
.filter(i => i.package === pkg && !i.internal)
.map(i => i.name)
.filter(i => i.length >= 4 && !disabledFunctions.includes(i)),
})
}
}
})
}
46 changes: 46 additions & 0 deletions packages/nuxt/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@vueuse/nuxt",
"version": "7.1.2",
"description": "VueUse Nuxt Module",
"keywords": [
"vue",
"vueuse",
"nuxt",
"nuxt3",
"nuxt-module"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/vueuse/vueuse.git",
"directory": "packages/nuxt"
},
"funding": "https://github.com/sponsors/antfu",
"author": "Anthony Fu <https://github.com/antfu>",
"exports": {
".": {
"import": "./index.mjs",
"require": "./index.cjs",
"types": "./index.d.ts"
},
"./*": "./*",
"./": {
"import": "./module.mjs",
"require": "./module.cjs",
"types": "./module.d.ts"
}
},
"main": "./index.cjs",
"types": "./index.d.ts",
"module": "./index.mjs",
"sideEffects": false,
"bugs": {
"url": "https://github.com/vueuse/vueuse/issues"
},
"homepage": "https://github.com/vueuse/vueuse/tree/main/packages/nuxt#readme",
"dependencies": {
"@vueuse/core": "workspace:*",
"local-pkg": "^0.4.0",
"vue-demi": "*"
}
}

0 comments on commit a115cf6

Please sign in to comment.