Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 9c5dee7

Browse files
danielroeregenrek
andauthored
feat: enable this to be used as a nuxt module (#13)
Co-authored-by: Kevin Regenrek <hello@regenrek.at>
1 parent a1491ec commit 9c5dee7

File tree

10 files changed

+109
-59
lines changed

10 files changed

+109
-59
lines changed

README.md

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535

3636
## Quick Start
3737

38-
> This project requires usage of [`@vue/composition-api`](https://github.com/vuejs/composition-api). Make sure you've set that up correctly first.
39-
4038
First install `nuxt-composition-api`:
4139

4240
```bash
@@ -47,6 +45,18 @@ yarn add nuxt-composition-api
4745
npm install nuxt-composition-api --save
4846
```
4947

48+
Enable the module in your `nuxt.config.js`
49+
50+
```
51+
{
52+
buildModules: [
53+
'nuxt-composition-api'
54+
]
55+
}
56+
```
57+
58+
The module automatically installs [`@vue/composition-api`](https://github.com/vuejs/composition-api) as a plugin, so you shouldn't need to do so separately.
59+
5060
You will now be able to access the following hooks:
5161

5262
### useFetch
@@ -56,8 +66,7 @@ Versions of Nuxt newer than v2.12 support a [custom hook called `fetch`](https:/
5666
You can access this with this package as follows:
5767

5868
```ts
59-
import { defineComponent, ref } from '@vue/composition-api'
60-
import { useFetch } from 'nuxt-composition-api'
69+
import { defineComponent, ref, useFetch } from 'nuxt-composition-api'
6170
import axios from 'axios'
6271

6372
export default defineComponent({
@@ -76,8 +85,7 @@ export default defineComponent({
7685
You can access the Nuxt context more easily using `withContext`, which runs synchronously within the setup function.
7786

7887
```ts
79-
import { defineComponent, ref } from '@vue/composition-api'
80-
import { withContext } from 'nuxt-composition-api'
88+
import { defineComponent, ref, withContext } from 'nuxt-composition-api'
8189

8290
export default defineComponent({
8391
setup() {
@@ -88,10 +96,30 @@ export default defineComponent({
8896
})
8997
```
9098

99+
### Additional `@vue/composition-api` functions
100+
101+
For convenience, this package also exports the [`@vue/composition-api`](https://github.com/vuejs/composition-api) methods and hooks, so you can import directly from `nuxt-composition-api`.
102+
91103
## Contributors
92104

93105
Contributions are very welcome.
94106

107+
Clone this repo
108+
109+
```
110+
git clone git@github.com:danielroe/nuxt-composition-api.git
111+
```
112+
113+
Install dependencies and build project
114+
115+
```
116+
yarn install
117+
118+
yarn build
119+
```
120+
121+
**Tip:** You can use `yarn link` to test the module locally with your nuxt project.
122+
95123
## License
96124

97125
[MIT License](./LICENSE) - Copyright &copy; Daniel Roe

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"sideEffects": false,
3131
"scripts": {
3232
"build": "yarn clean && yarn compile",
33+
"watch": "yarn compile -w",
3334
"clean": "rm -rf lib/*",
3435
"compile": "rollup -c",
3536
"lint": "run-s lint:all:*",
@@ -42,8 +43,7 @@
4243
"release": "release-it",
4344
"test": "run-s test:*",
4445
"test:types": "tsd",
45-
"test:e2e": "testcafe firefox:headless test/e2e --app 'node test/start-fixture.js'",
46-
"test:unit": "jest"
46+
"test:e2e": "testcafe firefox:headless test/e2e --app 'node test/start-fixture.js'"
4747
},
4848
"tsd": {
4949
"directory": "test/tsd",

rollup.config.js

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
11
import typescript from 'rollup-plugin-typescript2'
22
import pkg from './package.json'
33

4-
export default {
5-
input: 'src/index.ts',
6-
output: [
7-
{
8-
file: pkg.main,
9-
format: 'cjs',
10-
},
11-
{
12-
file: pkg.module,
13-
format: 'es',
14-
},
15-
],
16-
external: [
17-
...Object.keys(pkg.dependencies || {}),
18-
...Object.keys(pkg.peerDependencies || {}),
19-
],
20-
plugins: [
21-
typescript({
22-
typescript: require('typescript'),
23-
}),
24-
],
25-
}
4+
export default [
5+
{
6+
input: 'src/index.ts',
7+
output: [
8+
{
9+
file: pkg.main,
10+
format: 'cjs',
11+
},
12+
{
13+
file: pkg.module,
14+
format: 'es',
15+
},
16+
],
17+
external: [
18+
...Object.keys(pkg.dependencies || {}),
19+
...Object.keys(pkg.peerDependencies || {}),
20+
// 'prop-types',
21+
],
22+
plugins: [
23+
typescript({
24+
typescript: require('typescript'),
25+
}),
26+
],
27+
},
28+
{
29+
input: 'src/plugin.js',
30+
output: [
31+
{
32+
file: 'lib/plugin.js',
33+
format: 'es',
34+
},
35+
],
36+
external: [
37+
...Object.keys(pkg.dependencies || {}),
38+
...Object.keys(pkg.peerDependencies || {}),
39+
],
40+
},
41+
]

src/fetch.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ async function callFetches(this: AugmentedComponentInstance) {
3434
const fetchesToCall = fetches.get(this)
3535
if (!fetchesToCall) return
3636
;(this.$nuxt as any).nbFetching++
37-
this.$fetchState = this.$fetchState || {}
37+
this.$fetchState =
38+
this.$fetchState ||
39+
Vue.observable({
40+
error: null,
41+
pending: false,
42+
timestamp: 0,
43+
})
3844
this.$fetchState.pending = true
3945
this.$fetchState.error = null
4046
this._hydrated = false
@@ -62,7 +68,13 @@ async function callFetches(this: AugmentedComponentInstance) {
6268

6369
async function serverPrefetch(vm: AugmentedComponentInstance) {
6470
// Call and await on $fetch
65-
vm.$fetchState = vm.$fetchState || {}
71+
vm.$fetchState =
72+
vm.$fetchState ||
73+
Vue.observable({
74+
error: null,
75+
pending: false,
76+
timestamp: 0,
77+
})
6678
try {
6779
await callFetches.call(vm)
6880
} catch (err) {

src/index.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
import Vue from 'vue'
2-
import CompositionApi from '@vue/composition-api'
1+
import { resolve, join } from 'path'
2+
import { Module } from '@nuxt/types'
33

4-
Vue.use(CompositionApi)
4+
const compositionApiModule: Module<any> = function () {
5+
const libRoot = resolve(__dirname, '..')
6+
const { dst } = this.addTemplate({
7+
src: resolve(libRoot, 'lib', 'plugin.js'),
8+
fileName: join('composition-api', 'plugin.js'),
9+
options: {},
10+
})
11+
this.options.plugins = this.options.plugins || []
12+
this.options.plugins.push(resolve(this.options.buildDir || '', dst))
13+
}
14+
15+
export default compositionApiModule
16+
// eslint-disable-next-line
17+
export const meta = require('../package.json')
518

619
export { useFetch } from './fetch'
720
export { withContext } from './context'
21+
22+
export * from '@vue/composition-api'

test/fixture/nuxt.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ module.exports = {
55
rootDir: resolve(__dirname, '../..'),
66
buildDir: resolve(__dirname, '.nuxt'),
77
srcDir: __dirname,
8-
plugins: [require.resolve('./plugins/composition-api')],
8+
buildModules: [require.resolve('../..')],
99
}

test/fixture/pages/index.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
</template>
77

88
<script>
9-
import { defineComponent, ref } from '@vue/composition-api'
10-
import { useFetch } from '../../..'
9+
import { defineComponent, ref, useFetch } from '../../..'
1110
1211
export function fetcher(result) {
1312
return new Promise(resolve => {

test/index.spec.ts

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

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"declaration": true,
1313
"skipLibCheck": true,
1414
"allowSyntheticDefaultImports": true,
15-
"types": ["@nuxt/types"]
15+
"types": ["node", "@nuxt/types"]
1616
},
1717
"exclude": ["node_modules", "lib", "test"]
1818
}

0 commit comments

Comments
 (0)