Skip to content

Commit 53cba25

Browse files
docs: Migrate configuration and plugin examples to ES Modules syntax
1 parent a78a4db commit 53cba25

File tree

80 files changed

+1320
-1074
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1320
-1074
lines changed

src/components/Footer/Footer.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ const Footer = () => (
3939
<Link className="footer__link" to="/branding/">
4040
Branding
4141
</Link>
42-
<Link className="footer__link" to="https://discord.com/invite/5sxFZPdx2k">
42+
<Link
43+
className="footer__link"
44+
to="https://discord.com/invite/5sxFZPdx2k"
45+
>
4346
Discord
4447
</Link>
4548
<Link

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: 37 additions & 37 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,26 +160,26 @@ 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+
'export default import.meta.require(' +
183183
JSON.stringify('-!' + remainingRequest) +
184184
');'
185185
);
@@ -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
@@ -711,21 +711,21 @@ Throwing an error from loader:
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:
@@ -738,7 +738,7 @@ The module will get bundled like this:
738738
/*! no static exports found */
739739
/***/ (function(module, exports) {
740740

741-
throw new Error("Module build failed (from ./src/loader.js):\nError: This is a Fatal Error!\n at Object.module.exports (/workspace/src/loader.js:3:9)");
741+
throw new Error("Module build failed (from ./src/loader.js):\nError: This is a Fatal Error!\n at Object.<Module Execution> (/workspace/src/loader.js:3:9)");
742742

743743
/***/ })
744744
```
@@ -749,7 +749,7 @@ Then the build output will also display the error (Similar to `this.emitError`):
749749
ERROR in ./src/lib.js (./src/loader.js!./src/lib.js)
750750
Module build failed (from ./src/loader.js):
751751
Error: This is a Fatal Error!
752-
at Object.module.exports (/workspace/src/loader.js:2:9)
752+
at Object.<Module Execution> (/workspace/src/loader.js:2:9)
753753
@ ./src/index.js 1:0-25
754754
```
755755
@@ -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, import 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/,

src/content/api/module-variables.mdx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ Indicates whether or not [Hot Module Replacement](/concepts/hot-module-replaceme
3636
The ID of the current module.
3737

3838
```javascript
39-
module.id === require.resolve('./file.js');
39+
console.log('Current module ID concept is available via import.meta');
4040
```
4141

4242
## module.exports (CommonJS)
4343

4444
Defines the value that will be returned when a consumer makes a `require` call to the module (defaults to a new object).
4545

4646
```javascript
47-
module.exports = function doSomething() {
47+
export default function doSomething() {
4848
// Do something...
49-
};
49+
}
5050
```
5151

5252
W> This CANNOT be used in an asynchronous function.
@@ -56,13 +56,13 @@ W> This CANNOT be used in an asynchronous function.
5656
This variable is equal to the default value of `module.exports` (i.e. an object). If `module.exports` gets overwritten, `exports` will no longer be exported.
5757

5858
```javascript
59-
exports.someValue = 42;
60-
exports.anObject = {
59+
export const someValue = 42;
60+
export const anObject = {
6161
x: 123,
6262
};
63-
exports.aFunction = function doSomething() {
63+
export function aFunction() {
6464
// Do something
65-
};
65+
}
6666
```
6767

6868
## global (NodeJS)
@@ -170,7 +170,7 @@ If used inside an expression that is parsed by the Parser, the configuration opt
170170
The resource query of the current module. If the following `require` call was made, then the query string would be available in `file.js`.
171171
172172
```javascript
173-
require('file.js?test');
173+
import 'file.js?test';
174174
```
175175
176176
**file.js**
@@ -265,14 +265,12 @@ In modules, `__webpack_exports_info__` is available to allow exports introspecti
265265
- `__webpack_exports_info__.<exportName>.used` is `false` when the export is known to be unused, `true` otherwise
266266
267267
- `__webpack_exports_info__.<exportName>.useInfo` is
268-
269268
- `false` when the export is known to be unused
270269
- `true` when the export is known to be used
271270
- `null` when the export usage could depend on runtime conditions
272271
- `undefined` when no info is available
273272
274273
- `__webpack_exports_info__.<exportName>.provideInfo` is
275-
276274
- `false` when the export is known to be not provided
277275
- `true` when the export is known to be provided
278276
- `null` when the export provision could depend on runtime conditions

0 commit comments

Comments
 (0)