You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 3, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: en/guide/modules.md
+49-70Lines changed: 49 additions & 70 deletions
Original file line number
Diff line number
Diff line change
@@ -3,69 +3,56 @@ title: Modules
3
3
description: Modules are Nuxt.js extensions which can extend it's core functionality and add endless integrations.
4
4
---
5
5
6
-
> Modules are Nuxt.js extensions which can extend it's core functionality and add endless integrations.
6
+
> Modules are Nuxt.js extensions which can extend it's core functionality and add endless integrations.
7
7
8
8
## Introduction
9
9
10
-
While developing production grade application using Nuxt, you will find out soon that nuxt core functionalities are not enough
11
-
and writing configs and plugins for each project is a repetitive, boring and time consuming job.
12
-
Also adding every new feature into Nuxt is not possible as it would make it a fat framework.
13
-
14
-
This was the reason Nuxt introduces a higher order modular system to easily extend the core.
15
-
Modules are basically **functions** which are called sequentially when booting Nuxt and core awaits for all of them
16
-
to be finished before continue it's job. So they have the chance to customize almost any aspect of Nuxt and thanks to modular design of nuxt itself and Webpack [Tapable](https://github.com/webpack/tapable) technology they can also register hooks
17
-
for certain entry points like builder initialization.
10
+
While developing production grade application using Nuxt, you will find out soon that Nuxt core functionalities are not enough and writing configs and plugins for each project is a repetitive, boring and time consuming job. Also adding every new feature into Nuxt is not possible as it would make it a fat framework.
18
11
19
-
Another point of using modules is that they can be refactored and packaged out of the project and released as NPM packages
20
-
so you can share and use high quality integration and solutions from nuxt community with no pain! You might interested in modules if you:
12
+
This was the reason Nuxt introduces a higher order modular system to easily extend the core. Modules are basically **functions** which are called sequentially when booting Nuxt and core awaits for all of them to be finished before continue it's job. So they have the chance to customize almost any aspect of Nuxt and thanks to modular design of Nuxt itself and webpack [Tapable](https://github.com/webpack/tapable) technology they can also register hooks for certain entry points like builder initialization.
21
13
22
-
- Are a member of an **agile team** that want to set up your project instantly and avoid **re-inventing** the wheel for common tasks like google-analytics for your new project.
14
+
Another point of using modules is that they can be refactored and packaged out of the project and released as npm packages so you can share and use high quality integration and solutions from Nuxt community with no pain! You might interested in modules if you:
15
+
16
+
- Are a member of an **agile team** that want to set up your project instantly and avoid **re-inventing** the wheel for common tasks like Google Analytics tools for your new project.
23
17
- Are an **enterprise** company which **quality** and **reusability** is important for your projects.
24
-
- Are a lovely **open source** enthusiast and interested in **sharing** your works with community in an easy manner.
25
-
- Are a lazy programmer and don't like digging into details setting up every new library or integration.
26
-
(Someone else should already provided a module for that but you can always ask community for making one)
18
+
- Are a lovely **Open Source** enthusiast and interested in **sharing** your works with community in an easy manner.
19
+
- Are a lazy programmer and don't like digging into details setting up every new library or integration (Someone else should already provided a module for that but you can always ask community for making one).
27
20
- Tired of breaking low level API and Usage changes, and need **things that just work™**.
28
21
29
-
30
22
## Write a basic Module
31
-
As already mentioned modules are just simple functions.
32
-
They can be packaged as NPM modules or directly included in project source code.
23
+
24
+
As already mentioned modules are just simple functions. They can be packaged as npm modules or directly included in project source code.
This is the object passed using `modules` array by user we can use it to customize it's behavior.
47
40
48
-
49
41
**`this.options`**
50
42
51
-
You can directly access to Nuxt options using this reference.
52
-
This is _nuxt.config.js_ with all default options assigned to and can be used for shared options between modules.
53
-
43
+
You can directly access to Nuxt options using this reference. This is `nuxt.config.js` with all default options assigned to and can be used for shared options between modules.
54
44
55
45
**`this.nuxt`**
56
46
57
-
This is a reference to current nuxt instance. Refer to nuxt class docs for available methods.
58
-
47
+
This is a reference to current Nuxt instance. Refer to [Nuxt class docs for available methods](/api/internals-nuxt).
59
48
60
49
**`this`**
61
50
62
51
Context of modules. Refer to [ModuleContainer](/api/internals-module-container) class docs for available methods.
63
52
64
53
**`module.exports.meta`**
65
54
66
-
This line is **required** if you are publishing module as an NPM package.
67
-
Nuxt internally uses meta to work better with your package.
68
-
55
+
This line is **required** if you are publishing module as an npm package. Nuxt internally uses meta to work better with your package.
69
56
70
57
**nuxt.config.js**
71
58
@@ -81,29 +68,21 @@ module.exports = {
81
68
}
82
69
```
83
70
84
-
We then tell Nuxt to load some specific modules for a project with optional parameters as options.
85
-
Please refer to [modules configuration](/api/configuration-modules) docs for more info!
86
-
71
+
We then tell Nuxt to load some specific modules for a project with optional parameters as options. Please refer to [modules configuration](/api/configuration-modules) docs for more info!
87
72
88
73
## Async Modules
89
74
90
-
Not all modules will do everything synchronous.
91
-
For example you may want to develop a module which needs fetching some API or doing async IO.
92
-
For this, Nuxt supports async modules which can return a Promise or call a callback.
75
+
Not all modules will do everything synchronous. For example you may want to develop a module which needs fetching some API or doing async IO. For this, Nuxt supports async modules which can return a Promise or call a callback.
93
76
94
77
### Use async/await
95
78
96
-
<pclass="Alert Alert--orange">
97
-
Be aware that async/await is only supported in Node.js > 7.2
98
-
So if you are a module developer at least warn users about that if using them.
99
-
For heavily async modules or better legacy support you can use either a bundler to transform it for older node comparability or using promise method.
100
-
</p>
79
+
<pclass="Alert Alert--orange">Be aware that `async`/`await` is only supported in Node.js > 7.2. So if you are a module developer at least warn users about that if using them. For heavily async modules or better legacy support you can use either a bundler to transform it for older Node.js comparability or using promise method.</p>
101
80
102
81
```js
103
82
constfse=require('fs-extra')
104
83
105
84
module.exports=asyncfunctionasyncModule() {
106
-
// You can do async works here using async/await
85
+
// You can do async works here using `async`/`await`
107
86
let pages =awaitfse.readJson('./pages.json')
108
87
}
109
88
```
@@ -117,7 +96,7 @@ module.exports = function asyncModule() {
@@ -140,8 +119,8 @@ module.exports = function asyncModule(callback) {
140
119
## Common Snippets
141
120
142
121
### Top level options
143
-
Sometimes it is more convenient if we can use top level options while register modules in `nuxt.config.js`.
144
-
So we can combine multiply option sources.
122
+
123
+
Sometimes it is more convenient if we can use top level options while register modules in `nuxt.config.js`. So we can combine multiply option sources.
145
124
146
125
**nuxt.config.js**
147
126
@@ -151,12 +130,12 @@ module.exports = {
151
130
'@nuxtjs/axios'
152
131
],
153
132
154
-
// axios module is aware of this by using this.options.axios
133
+
// axios module is aware of this by using `this.options.axios`
155
134
axios: {
156
135
option1,
157
136
option2
158
137
}
159
-
}
138
+
}
160
139
```
161
140
162
141
**module.js**
@@ -169,11 +148,11 @@ module.exports = function (moduleOptions) {
169
148
```
170
149
171
150
### Provide plugins
172
-
It is common that modules provide one or more plugins when added.
173
-
For example [bootstrap-vue](https://bootstrap-vue.js.org) module would require to register itself into Vue.
174
-
For this we can use `this.addPlugin` helper.
151
+
152
+
It is common that modules provide one or more plugins when added. For example [bootstrap-vue](https://bootstrap-vue.js.org) module would require to register itself into Vue. For this we can use `this.addPlugin` helper.
// Nuxt will replace options.ua with 123 when copying plugin to project
199
+
// Nuxt will replace `options.ua` with `123` when copying plugin to project
218
200
ua:123,
219
201
220
202
// conditional parts with dev will be stripped from plugin code on production builds
@@ -225,33 +207,34 @@ module.exports = function nuxtBootstrapVue (moduleOptions) {
225
207
```
226
208
227
209
### Add a CSS library
228
-
It is recommended checking if user already not provided same library to avoid adding duplicates.
229
-
Also always consider having **an option to disable** adding css files by module.
210
+
211
+
It is recommended checking if user already not provided same library to avoid adding duplicates. Also always consider having **an option to disable** adding css files by module.
@@ -287,9 +270,8 @@ module.exports = function (moduleOptions) {
287
270
```
288
271
289
272
## Run Tasks on Specific hooks
290
-
Your module may need to do things only on specific conditions not just during Nuxt initialization.
291
-
We can use powerful [tapable](https://github.com/webpack/tapable) plugin system to do tasks on specific events.
292
-
Nuxt will await for us if hooks return a Promise or are defined as `async`.
273
+
274
+
Your module may need to do things only on specific conditions not just during Nuxt initialization. We can use powerful [Tapable](https://github.com/webpack/tapable) plugin system to do tasks on specific events. Nuxt will await for us if hooks return a Promise or are defined as `async`.
293
275
294
276
```js
295
277
module.exports=function () {
@@ -305,7 +287,7 @@ module.exports = function () {
305
287
306
288
// Add hook for build
307
289
this.nuxt.plugin('build', asyncbuilder=> {
308
-
// This Will be called once when builder created
290
+
// This will be called once when builder created
309
291
310
292
// We can even register internal hooks here
311
293
builder.plugin('compile', ({compiler}) => {
@@ -315,12 +297,9 @@ module.exports = function () {
315
297
316
298
// Add hook for generate
317
299
this.nuxt.plugin('generate', asyncgenerator=> {
318
-
// This Will be called when a nuxt generate starts
300
+
// This will be called when a Nuxt generate starts
319
301
})
320
302
}
321
303
```
322
304
323
-
<pclass="Alert">
324
-
There are many many more hooks and possibilities for modules.
325
-
Please refer to [Nuxt Internals](/api/internals) to learn more about Nuxt internal API.
326
-
</p>
305
+
<pclass="Alert">There are many many more hooks and possibilities for modules. Please refer to [Nuxt Internals](/api/internals) to learn more about Nuxt internal API.</p>
0 commit comments