Skip to content

Commit da90251

Browse files
feat: add plugins option (#8)
1 parent 2baa7d5 commit da90251

File tree

10 files changed

+96
-10
lines changed

10 files changed

+96
-10
lines changed

README.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,37 @@ You can use `'pusher'`, `'socket.io'` or `'null'`.
7272

7373
See https://laravel.com/docs/broadcasting#driver-prerequisites
7474

75+
### `plugins`
76+
77+
- Type: `Array`
78+
- Default: `null`
79+
80+
If you have plugins that need to access `$echo`, you can use `echo.plugins` option.
81+
82+
> **Note:** Plugins are pushed in client mode only (`ssr: false`).
83+
84+
`nuxt.config.js`
85+
86+
```js
87+
{
88+
buildModules: [
89+
'@nuxtjs/laravel-echo'
90+
],
91+
echo: {
92+
plugins: [ '~/plugins/echo.js' ]
93+
}
94+
}
95+
```
96+
97+
`plugins/echo.js`
98+
99+
```js
100+
export default function ({ $echo }) {
101+
// Echo is available here
102+
console.log($echo)
103+
}
104+
```
105+
75106
### `authModule`
76107

77108
- Type: `Boolean`
@@ -116,12 +147,6 @@ export default {
116147
</script>
117148
```
118149

119-
## Development
120-
121-
1. Clone this repository
122-
2. Install dependencies using `yarn install` or `npm install`
123-
3. Start development server using `npm run dev`
124-
125150
## License
126151

127152
[MIT License](./LICENSE)

lib/module.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ module.exports = function (moduleOptions) {
44
this.nuxt.hook('builder:extendPlugins', (plugins) => {
55
const options = {
66
broadcaster: 'null',
7+
plugins: null,
78
authModule: false,
89
connectOnLogin: true,
910
disconnectOnLogout: true,
1011
...this.options.echo,
1112
...moduleOptions
1213
}
1314

15+
// Copy echo plugin
1416
const { dst } = this.addTemplate({
1517
src: resolve(__dirname, 'plugin.js'),
1618
fileName: 'echo.js',
@@ -21,6 +23,16 @@ module.exports = function (moduleOptions) {
2123
ssr: false,
2224
src: resolve(this.options.buildDir, dst)
2325
})
26+
27+
// Extend echo with plugins
28+
if (options.plugins) {
29+
options.plugins.forEach(p => plugins.push({
30+
ssr: false,
31+
src: p
32+
}))
33+
34+
delete options.plugins
35+
}
2436
})
2537
}
2638

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
],
1313
"main": "lib/module.js",
1414
"scripts": {
15-
"dev": "nuxt test/fixture",
1615
"lint": "eslint --ext .js,.vue .",
1716
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
1817
"test": "yarn lint && jest"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const { setup, loadConfig, get, url } = require('@nuxtjs/module-test-utils')
22

3-
describe('module', () => {
3+
describe('basic', () => {
44
let nuxt
55

66
beforeAll(async () => {
7-
({ nuxt } = (await setup(loadConfig(__dirname))))
7+
({ nuxt } = (await setup(loadConfig(__dirname, 'basic'))))
88
}, 60000)
99

1010
afterAll(async () => {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ module.exports = {
44
resourceHints: false
55
},
66
buildModules: [
7-
{ handler: require('../../') }
7+
{ handler: require('../../../') }
88
]
99
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
rootDir: __dirname,
3+
render: {
4+
resourceHints: false
5+
},
6+
buildModules: [
7+
{ handler: require('../../../') }
8+
],
9+
echo: {
10+
plugins: [ '~/plugins/echo.js' ]
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<template>
2+
<div>
3+
Works!
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
10+
}
11+
</script>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function ({ $echo }) {
2+
$echo.plugin = true
3+
}

test/with-plugins.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { setup, loadConfig, get, url } = require('@nuxtjs/module-test-utils')
2+
3+
describe('with-plugins', () => {
4+
let nuxt
5+
6+
beforeAll(async () => {
7+
({ nuxt } = (await setup(loadConfig(__dirname, 'with-plugins'))))
8+
}, 60000)
9+
10+
afterAll(async () => {
11+
await nuxt.close()
12+
})
13+
14+
test('echo plugin should be defined', async () => {
15+
const window = await nuxt.renderAndGetWindow(url('/'))
16+
expect(window.$nuxt.$echo).toBeDefined()
17+
expect(window.$nuxt.$echo.plugin).toBe(true)
18+
})
19+
20+
test('render', async () => {
21+
const html = await get('/')
22+
expect(html).toContain('Works!')
23+
})
24+
})

0 commit comments

Comments
 (0)