Skip to content

Commit fb0ad77

Browse files
Update documentation examples to ES6 #7636 (#7704)
* docs: Update Getting Started guide to ES Modules syntax * docs: Update Asset Management guide to ES Modules syntax (fixes #7636) * successfully update * Refactor API folder files for ESM compatibility
1 parent a2a4d07 commit fb0ad77

File tree

11 files changed

+147
-136
lines changed

11 files changed

+147
-136
lines changed

src/content/api/cli.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ In case your configuration file exports multiple configurations, you can use `--
503503
Consider the following `webpack.config.js`:
504504

505505
```js
506-
module.exports = [
506+
export default [
507507
{
508508
output: {
509509
filename: './dist-first.js',
@@ -617,7 +617,7 @@ In addition to the customized `env` showed above, there are some built-in ones u
617617
Note that you can not access those built-in environment variables inside the bundled code.
618618

619619
```javascript
620-
module.exports = (env, argv) => {
620+
export default (env, argv) => {
621621
return {
622622
mode: env.WEBPACK_SERVE ? 'development' : 'production',
623623
};
@@ -649,7 +649,7 @@ When the `mode` option is not specified in the configuration, you can use the `-
649649
If your configuration exports a function, the value of `--config-node-env` is assigned to mode after the function returns. This means that `mode` will not be available in the function arguments (`env` and `argv`). However, the value of `--config-node-env` is accessible as `argv.nodeEnv` within the function and can be used accordingly.
650650

651651
```javascript
652-
module.exports = (env, argv) => {
652+
export default (env, argv) => {
653653
console.log(argv.defineProcessEnvNodeEnv); // 'production' if --config-node-env production is used
654654
return {
655655
// your configuration

src/content/api/loaders.mdx

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,20 @@ Either `return` or `this.callback` can be used to return the transformed `conten
4848
**sync-loader.js**
4949

5050
```javascript
51-
module.exports = function (content, map, meta) {
51+
export default function (content, map, meta) {
5252
return someSyncOperation(content);
53-
};
53+
}
5454
```
5555

5656
The `this.callback` method is more flexible as you pass multiple arguments instead of using `content` only.
5757

5858
**sync-loader-with-multiple-results.js**
5959

6060
```javascript
61-
module.exports = function (content, map, meta) {
61+
export default function (content, map, meta) {
6262
this.callback(null, someSyncOperation(content), map, meta);
6363
return; // always return undefined when calling callback()
64-
};
64+
}
6565
```
6666

6767
### Asynchronous Loaders
@@ -71,36 +71,36 @@ For asynchronous loaders, you can return the transformed `content` from an `asyn
7171
**async-loader.js**
7272

7373
```javascript
74-
module.exports = async function (content, map, meta) {
74+
export default async function (content, map, meta) {
7575
var result = await someAsyncOperation(content);
7676
return result;
77-
};
77+
}
7878
```
7979

8080
Or you can use [`this.async`](#thisasync) to retrieve the `callback` function:
8181

8282
**async-loader-with-callback.js**
8383

8484
```javascript
85-
module.exports = function (content, map, meta) {
85+
export default function (content, map, meta) {
8686
var callback = this.async();
8787
someAsyncOperation(content, function (err, result) {
8888
if (err) return callback(err);
8989
callback(null, result, map, meta);
9090
});
91-
};
91+
}
9292
```
9393

9494
**async-loader-with-multiple-results.js**
9595

9696
```javascript
97-
module.exports = function (content, map, meta) {
97+
export default function (content, map, meta) {
9898
var callback = this.async();
9999
someAsyncOperation(content, function (err, result, sourceMaps, meta) {
100100
if (err) return callback(err);
101101
callback(null, result, sourceMaps, meta);
102102
});
103-
};
103+
}
104104
```
105105

106106
T> Loaders were originally designed to work in synchronous loader pipelines, like Node.js (using [enhanced-require](https://github.com/webpack/enhanced-require)), _and_ asynchronous pipelines, like in webpack. However, since expensive synchronous computations are a bad idea in a single-threaded environment like Node.js, we advise making your loader asynchronous if possible. Synchronous loaders are ok if the amount of computation is trivial.
@@ -112,13 +112,13 @@ By default, the resource file is converted to a UTF-8 string and passed to the l
112112
**raw-loader.js**
113113

114114
```javascript
115-
module.exports = function (content) {
115+
export default function (content) {
116116
assert(content instanceof Buffer);
117117
return someSyncOperation(content);
118118
// return value can be a `Buffer` too
119119
// This is also allowed if loader is not "raw"
120-
};
121-
module.exports.raw = true;
120+
}
121+
export const raw = true;
122122
```
123123

124124
### Pitching Loader
@@ -130,7 +130,7 @@ T> Loaders may be added inline in requests and disabled via inline prefixes, whi
130130
For the following configuration of [`use`](/configuration/module/#ruleuse):
131131

132132
```javascript
133-
module.exports = {
133+
export default {
134134
//...
135135
module: {
136136
rules: [
@@ -160,28 +160,28 @@ So why might a loader take advantage of the "pitching" phase?
160160
First, the `data` passed to the `pitch` method is exposed in the execution phase as well under `this.data` and could be useful for capturing and sharing information from earlier in the cycle.
161161

162162
```javascript
163-
module.exports = function (content) {
163+
export default function (content) {
164164
return someSyncOperation(content, this.data.value);
165-
};
165+
}
166166

167-
module.exports.pitch = function (remainingRequest, precedingRequest, data) {
167+
export const pitch = function (remainingRequest, precedingRequest, data) {
168168
data.value = 42;
169169
};
170170
```
171171

172172
Second, if a loader delivers a result in the `pitch` method, the process turns around and skips the remaining loaders. In our example above, if the `b-loader`s `pitch` method returned something:
173173

174174
```javascript
175-
module.exports = function (content) {
175+
export default function (content) {
176176
return someSyncOperation(content);
177-
};
177+
}
178178

179-
module.exports.pitch = function (remainingRequest, precedingRequest, data) {
179+
export const pitch = function (remainingRequest, precedingRequest, data) {
180180
if (someCondition()) {
181181
return (
182-
'module.exports = require(' +
182+
'import _from_loader from ' +
183183
JSON.stringify('-!' + remainingRequest) +
184-
');'
184+
'; export default _from_loader;'
185185
);
186186
}
187187
};
@@ -206,7 +206,7 @@ Given the following example, this require call is used:
206206
In `/abc/file.js`:
207207

208208
```javascript
209-
require('./loader1?xyz!loader2!./resource?rrr');
209+
import './loader1?xyz!loader2!./resource?rrr';
210210
```
211211

212212
### this.addContextDependency
@@ -397,10 +397,10 @@ All dependencies of the resolving operation are automatically added as dependenc
397397
Information about HMR for loaders.
398398
399399
```javascript
400-
module.exports = function (source) {
400+
export default function (source) {
401401
console.log(this.hot); // true if HMR is enabled via --hot flag or webpack configuration
402402
return source;
403-
};
403+
}
404404
```
405405
406406
### this.hashDigest
@@ -452,7 +452,7 @@ An alternative lightweight solution for the child compiler to compile and execut
452452
**webpack.config.js**
453453
454454
```js
455-
module.exports = {
455+
export default {
456456
module: {
457457
rules: [
458458
{
@@ -624,7 +624,7 @@ Access to the following utilities.
624624
**my-sync-loader.js**
625625

626626
```js
627-
module.exports = function (content) {
627+
export default function (content) {
628628
this.utils.contextify(
629629
this.context,
630630
this.utils.absolutify(this.context, './index.js')
@@ -637,7 +637,7 @@ module.exports = function (content) {
637637
mainHash.digest('hex');
638638
//
639639
return content;
640-
};
640+
}
641641
```
642642
643643
### this.version
@@ -703,29 +703,29 @@ For example:
703703
**./src/index.js**
704704
705705
```javascript
706-
require('./loader!./lib');
706+
import './loader!./lib';
707707
```
708708
709709
Throwing an error from loader:
710710
711711
**./src/loader.js**
712712
713713
```javascript
714-
module.exports = function (source) {
714+
export default function (source) {
715715
throw new Error('This is a Fatal Error!');
716-
};
716+
}
717717
```
718718
719719
Or pass an error to the callback in async mode:
720720
721721
**./src/loader.js**
722722
723723
```javascript
724-
module.exports = function (source) {
724+
export default function (source) {
725725
const callback = this.async();
726726
//...
727727
callback(new Error('This is a Fatal Error!'), source);
728-
};
728+
}
729729
```
730730
731731
The module will get bundled like this:
@@ -801,9 +801,9 @@ The loader could look like this:
801801
**extract-style-loader/index.js**
802802
803803
```javascript
804-
const getStylesLoader = require.resolve('./getStyles');
804+
import getStylesLoader from './getStyles';
805805

806-
module.exports = function (source) {
806+
export default function (source) {
807807
if (STYLES_REGEXP.test(source)) {
808808
source = source.replace(STYLES_REGEXP, '');
809809
return `import ${JSON.stringify(
@@ -814,16 +814,16 @@ module.exports = function (source) {
814814
)};${source}`;
815815
}
816816
return source;
817-
};
817+
}
818818
```
819819
820820
**extract-style-loader/getStyles.js**
821821
822822
```javascript
823-
module.exports = function (source) {
823+
export default function (source) {
824824
const match = source.match(STYLES_REGEXP);
825825
return match[0];
826-
};
826+
}
827827
```
828828
829829
## Logging

src/content/api/logging.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ export class MyWebpackPlugin {
5050
**my-webpack-loader.js**
5151

5252
```js
53-
module.exports = function (source) {
53+
export default function (source) {
5454
// you can get Logger with `this.getLogger` in your webpack loaders
5555
const logger = this.getLogger('my-webpack-loader');
5656
logger.info('hello Logger');
5757
return source;
58-
};
58+
}
5959
```
6060

6161
As you can see from the above `my-webpack-plugin.js` example, there're two types of logging methods,
@@ -84,12 +84,12 @@ It's advised to use `compilation.getLogger` when plugin/logging is related to th
8484

8585
Runtime logger API is only intended to be used as a development tool, it is not intended to be included in [production mode](/configuration/mode/#mode-production).
8686

87-
- `const logging = require('webpack/lib/logging/runtime')`: to use the logger in runtime, require it directly from webpack
87+
- `import logging from 'webpack/lib/logging/runtime'`: to use the logger in runtime, require it directly from webpack
8888
- `logging.getLogger('name')`: to get individual logger by name
8989
- `logging.configureDefaultLogger(...)`: to override the default logger.
9090

9191
```javascript
92-
const logging = require('webpack/lib/logging/runtime');
92+
import logging from 'webpack/lib/logging/runtime';
9393
logging.configureDefaultLogger({
9494
level: 'log',
9595
debug: /something/,

0 commit comments

Comments
 (0)