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

Commit 0c36bb6

Browse files
authored
Merge pull request #267 from Haeresis/modules
[Doc EN]: `modules.md` review.
2 parents 2303b8a + c233437 commit 0c36bb6

File tree

1 file changed

+49
-70
lines changed

1 file changed

+49
-70
lines changed

en/guide/modules.md

Lines changed: 49 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,56 @@ title: Modules
33
description: Modules are Nuxt.js extensions which can extend it's core functionality and add endless integrations.
44
---
55

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.
77
88
## Introduction
99

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.
1811

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.
2113

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.
2317
- 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).
2720
- Tired of breaking low level API and Usage changes, and need **things that just work™**.
2821

29-
3022
## 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.
3325

3426
**modules/simple.js**
27+
3528
```js
36-
module.exports = function SimpleModule (moduleOptions) {
29+
module.exports = function SimpleModule (moduleOptions) {
3730
// Write your code here
3831
}
3932

40-
// REQUIRED if publishing as an NPM package
33+
// REQUIRED if publishing as an npm package
4134
// module.exports.meta = require('./package.json')
4235
```
4336

4437
**`moduleOptions`**
4538

4639
This is the object passed using `modules` array by user we can use it to customize it's behavior.
4740

48-
4941
**`this.options`**
5042

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.
5444

5545
**`this.nuxt`**
5646

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).
5948

6049
**`this`**
6150

6251
Context of modules. Refer to [ModuleContainer](/api/internals-module-container) class docs for available methods.
6352

6453
**`module.exports.meta`**
6554

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.
6956

7057
**nuxt.config.js**
7158

@@ -81,29 +68,21 @@ module.exports = {
8168
}
8269
```
8370

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!
8772

8873
## Async Modules
8974

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.
9376

9477
### Use async/await
9578

96-
<p class="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+
<p class="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>
10180

10281
```js
10382
const fse = require('fs-extra')
10483

10584
module.exports = async function asyncModule() {
106-
// You can do async works here using async/await
85+
// You can do async works here using `async`/`await`
10786
let pages = await fse.readJson('./pages.json')
10887
}
10988
```
@@ -117,7 +96,7 @@ module.exports = function asyncModule() {
11796
return axios.get('https://jsonplaceholder.typicode.com/users')
11897
.then(res => res.data.map(user => '/users/' + user.username))
11998
.then(routes => {
120-
// Do something by extending nuxt routes
99+
// Do something by extending Nuxt routes
121100
})
122101
}
123102
```
@@ -140,8 +119,8 @@ module.exports = function asyncModule(callback) {
140119
## Common Snippets
141120

142121
### 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.
145124

146125
**nuxt.config.js**
147126

@@ -151,12 +130,12 @@ module.exports = {
151130
'@nuxtjs/axios'
152131
],
153132

154-
// axios module is aware of this by using this.options.axios
133+
// axios module is aware of this by using `this.options.axios`
155134
axios: {
156135
option1,
157136
option2
158137
}
159-
}
138+
}
160139
```
161140

162141
**module.js**
@@ -169,11 +148,11 @@ module.exports = function (moduleOptions) {
169148
```
170149

171150
### 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.
175153

176154
**plugin.js**
155+
177156
```js
178157
import Vue from 'vue'
179158
import BootstrapVue from 'bootstrap-vue/dist/bootstrap-vue.esm'
@@ -182,20 +161,22 @@ Vue.use(BootstrapVue)
182161
```
183162

184163
**module.js**
164+
185165
```js
186166
const path = require('path')
187167

188168
module.exports = function nuxtBootstrapVue (moduleOptions) {
189-
// Register plugin.js template
169+
// Register `plugin.js` template
190170
this.addPlugin(path.resolve(__dirname, 'plugin.js'))
191171
}
192172
```
193173

194174
### Template plugins
195-
Registered templates and plugins can leverage [lodash templates](https://lodash.com/docs/4.17.4#template)
196-
to conditionally change registered plugins output.
175+
176+
Registered templates and plugins can leverage [lodash templates](https://lodash.com/docs/4.17.4#template) to conditionally change registered plugins output.
197177

198178
**plugin.js**
179+
199180
```js
200181
// Set Google Analytics UA
201182
ga('create', '<%= options.ua %>', 'auto')
@@ -206,15 +187,16 @@ ga('create', '<%= options.ua %>', 'auto')
206187
```
207188

208189
**module.js**
190+
209191
```js
210192
const path = require('path')
211193

212194
module.exports = function nuxtBootstrapVue (moduleOptions) {
213-
// Register plugin.js template
195+
// Register `plugin.js` template
214196
this.addPlugin({
215197
src: path.resolve(__dirname, 'plugin.js'),
216198
options: {
217-
// 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
218200
ua: 123,
219201

220202
// conditional parts with dev will be stripped from plugin code on production builds
@@ -225,33 +207,34 @@ module.exports = function nuxtBootstrapVue (moduleOptions) {
225207
```
226208

227209
### 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.
230212

231213
**module.js**
232214

233215
```js
234216
module.exports = function (moduleOptions) {
235217
if (moduleOptions.fontAwesome !== false) {
236-
// Add font-awesome
218+
// Add Font Awesome
237219
this.options.css.push('font-awesome/css/font-awesome.css')
238220
}
239221
}
240222
```
241223

242224
### Emit assets
225+
243226
We can register webpack plugins to emit assets during build.
244227

245228
**module.js**
246229

247230
```js
248231
module.exports = function (moduleOptions) {
249-
const info = 'Built by awesome module - 1.3 alpha on ' + Date.now()
232+
const info = 'Built by awesome module - 1.3 alpha on ' + Date.now()
250233

251234
this.options.build.plugins.push({
252235
apply (compiler) {
253236
compiler.plugin('emit', (compilation, cb) => {
254-
237+
255238
// This will generate `.nuxt/dist/info.txt' with contents of info variable.
256239
// Source can be buffer too
257240
compilation.assets['info.txt'] = { source: () => info, size: () => info.length }
@@ -265,14 +248,14 @@ module.exports = function (moduleOptions) {
265248

266249
### Register custom loaders
267250

268-
We can do the same as `build.extend` in `nuxt.config.js` using `this.extendBuild`
251+
We can do the same as `build.extend` in `nuxt.config.js` using `this.extendBuild`.
269252

270253
**module.js**
271254

272255
```js
273256
module.exports = function (moduleOptions) {
274257
this.extendBuild((config, { isClient, isServer }) => {
275-
// .foo Loader
258+
// `.foo` Loader
276259
config.module.rules.push({
277260
test: /\.foo$/,
278261
use: [...]
@@ -287,9 +270,8 @@ module.exports = function (moduleOptions) {
287270
```
288271

289272
## 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`.
293275

294276
```js
295277
module.exports = function () {
@@ -305,7 +287,7 @@ module.exports = function () {
305287

306288
// Add hook for build
307289
this.nuxt.plugin('build', async builder => {
308-
// This Will be called once when builder created
290+
// This will be called once when builder created
309291

310292
// We can even register internal hooks here
311293
builder.plugin('compile', ({compiler}) => {
@@ -315,12 +297,9 @@ module.exports = function () {
315297

316298
// Add hook for generate
317299
this.nuxt.plugin('generate', async generator => {
318-
// This Will be called when a nuxt generate starts
300+
// This will be called when a Nuxt generate starts
319301
})
320302
}
321303
```
322304

323-
<p class="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+
<p class="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

Comments
 (0)