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
@@ -649,7 +649,7 @@ When the `mode` option is not specified in the configuration, you can use the `-
649
649
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.
650
650
651
651
```javascript
652
-
module.exports= (env, argv) => {
652
+
exportdefault (env, argv) => {
653
653
console.log(argv.defineProcessEnvNodeEnv); // 'production' if --config-node-env production is used
Or you can use [`this.async`](#thisasync) to retrieve the `callback` function:
81
81
82
82
**async-loader-with-callback.js**
83
83
84
84
```javascript
85
-
module.exports=function (content, map, meta) {
85
+
exportdefaultfunction (content, map, meta) {
86
86
var callback =this.async();
87
87
someAsyncOperation(content, function (err, result) {
88
88
if (err) returncallback(err);
89
89
callback(null, result, map, meta);
90
90
});
91
-
};
91
+
}
92
92
```
93
93
94
94
**async-loader-with-multiple-results.js**
95
95
96
96
```javascript
97
-
module.exports=function (content, map, meta) {
97
+
exportdefaultfunction (content, map, meta) {
98
98
var callback =this.async();
99
99
someAsyncOperation(content, function (err, result, sourceMaps, meta) {
100
100
if (err) returncallback(err);
101
101
callback(null, result, sourceMaps, meta);
102
102
});
103
-
};
103
+
}
104
104
```
105
105
106
106
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
112
112
**raw-loader.js**
113
113
114
114
```javascript
115
-
module.exports=function (content) {
115
+
exportdefaultfunction (content) {
116
116
assert(content instanceof Buffer);
117
117
returnsomeSyncOperation(content);
118
118
// return value can be a `Buffer` too
119
119
// This is also allowed if loader is not "raw"
120
-
};
121
-
module.exports.raw=true;
120
+
}
121
+
exportconstraw=true;
122
122
```
123
123
124
124
### Pitching Loader
@@ -130,7 +130,7 @@ T> Loaders may be added inline in requests and disabled via inline prefixes, whi
130
130
For the following configuration of [`use`](/configuration/module/#ruleuse):
131
131
132
132
```javascript
133
-
module.exports= {
133
+
exportdefault {
134
134
//...
135
135
module: {
136
136
rules: [
@@ -160,26 +160,26 @@ So why might a loader take advantage of the "pitching" phase?
160
160
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.
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:
@@ -397,10 +397,10 @@ All dependencies of the resolving operation are automatically added as dependenc
397
397
Information about HMR for loaders.
398
398
399
399
```javascript
400
-
module.exports=function (source) {
400
+
exportdefaultfunction (source) {
401
401
console.log(this.hot); // true if HMR is enabled via --hot flag or webpack configuration
402
402
returnsource;
403
-
};
403
+
}
404
404
```
405
405
406
406
### this.hashDigest
@@ -452,7 +452,7 @@ An alternative lightweight solution for the child compiler to compile and execut
452
452
**webpack.config.js**
453
453
454
454
```js
455
-
module.exports= {
455
+
exportdefault {
456
456
module: {
457
457
rules: [
458
458
{
@@ -624,7 +624,7 @@ Access to the following utilities.
624
624
**my-sync-loader.js**
625
625
626
626
```js
627
-
module.exports = function (content) {
627
+
export default function (content) {
628
628
this.utils.contextify(
629
629
this.context,
630
630
this.utils.absolutify(this.context, './index.js')
@@ -637,7 +637,7 @@ module.exports = function (content) {
637
637
mainHash.digest('hex');
638
638
// …
639
639
returncontent;
640
-
};
640
+
}
641
641
```
642
642
643
643
### this.version
@@ -711,21 +711,21 @@ Throwing an error from loader:
711
711
**./src/loader.js**
712
712
713
713
```javascript
714
-
module.exports=function (source) {
714
+
exportdefaultfunction (source) {
715
715
thrownewError('This is a Fatal Error!');
716
-
};
716
+
}
717
717
```
718
718
719
719
Or pass an error to the callback in async mode:
720
720
721
721
**./src/loader.js**
722
722
723
723
```javascript
724
-
module.exports=function (source) {
724
+
exportdefaultfunction (source) {
725
725
const callback =this.async();
726
726
//...
727
727
callback(newError('This is a Fatal Error!'), source);
728
-
};
728
+
}
729
729
```
730
730
731
731
The module will get bundled like this:
@@ -738,7 +738,7 @@ The module will get bundled like this:
738
738
/*! no static exports found */
739
739
/***/ (function(module, exports) {
740
740
741
-
thrownewError("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
+
thrownewError("Module build failed (from ./src/loader.js):\nError: This is a Fatal Error!\n at Object.<Module Execution> (/workspace/src/loader.js:3:9)");
742
742
743
743
/***/ })
744
744
```
@@ -749,7 +749,7 @@ Then the build output will also display the error (Similar to `this.emitError`):
Copy file name to clipboardExpand all lines: src/content/api/logging.mdx
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,12 +50,12 @@ export class MyWebpackPlugin {
50
50
**my-webpack-loader.js**
51
51
52
52
```js
53
-
module.exports=function (source) {
53
+
exportdefaultfunction (source) {
54
54
// you can get Logger with `this.getLogger` in your webpack loaders
55
55
constlogger=this.getLogger('my-webpack-loader');
56
56
logger.info('hello Logger');
57
57
return source;
58
-
};
58
+
}
59
59
```
60
60
61
61
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
84
84
85
85
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).
86
86
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
88
88
-`logging.getLogger('name')`: to get individual logger by name
89
89
-`logging.configureDefaultLogger(...)`: to override the default logger.
Copy file name to clipboardExpand all lines: src/content/api/module-variables.mdx
+8-10Lines changed: 8 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,17 +36,17 @@ Indicates whether or not [Hot Module Replacement](/concepts/hot-module-replaceme
36
36
The ID of the current module.
37
37
38
38
```javascript
39
-
module.id===require.resolve('./file.js');
39
+
console.log('Current module ID concept is available via import.meta');
40
40
```
41
41
42
42
## module.exports (CommonJS)
43
43
44
44
Defines the value that will be returned when a consumer makes a `require` call to the module (defaults to a new object).
45
45
46
46
```javascript
47
-
module.exports=functiondoSomething() {
47
+
exportdefaultfunctiondoSomething() {
48
48
// Do something...
49
-
};
49
+
}
50
50
```
51
51
52
52
W> This CANNOT be used in an asynchronous function.
@@ -56,13 +56,13 @@ W> This CANNOT be used in an asynchronous function.
56
56
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.
57
57
58
58
```javascript
59
-
exports.someValue=42;
60
-
exports.anObject= {
59
+
exportconstsomeValue=42;
60
+
exportconstanObject= {
61
61
x:123,
62
62
};
63
-
exports.aFunction=functiondoSomething() {
63
+
exportfunctionaFunction() {
64
64
// Do something
65
-
};
65
+
}
66
66
```
67
67
68
68
## global (NodeJS)
@@ -170,7 +170,7 @@ If used inside an expression that is parsed by the Parser, the configuration opt
170
170
The resource query of the current module. If the following `require` call was made, then the query string would be available in `file.js`.
171
171
172
172
```javascript
173
-
require('file.js?test');
173
+
import'file.js?test';
174
174
```
175
175
176
176
**file.js**
@@ -265,14 +265,12 @@ In modules, `__webpack_exports_info__` is available to allow exports introspecti
265
265
- `__webpack_exports_info__.<exportName>.used` is `false` when the export is known to be unused, `true` otherwise
266
266
267
267
- `__webpack_exports_info__.<exportName>.useInfo` is
268
-
269
268
- `false` when the export is known to be unused
270
269
- `true` when the export is known to be used
271
270
- `null` when the export usage could depend on runtime conditions
272
271
- `undefined` when no info is available
273
272
274
273
- `__webpack_exports_info__.<exportName>.provideInfo` is
275
-
276
274
- `false` when the export is known to be not provided
277
275
- `true` when the export is known to be provided
278
276
- `null` when the export provision could depend on runtime conditions
0 commit comments