@@ -42,14 +42,12 @@ import webpack from 'webpack';
42
42
43
43
The imported ` webpack ` function is fed a webpack [ Configuration Object] ( /configuration/ ) and runs the webpack compiler if a callback function is provided:
44
44
45
- ``` js-with-links
45
+ ``` js
46
46
const webpack = require (' webpack' );
47
47
48
- webpack({
49
- // [Configuration Object](/configuration/)
50
- }, (err, stats) => { // [Stats Object](#stats-object)
48
+ webpack ({}, (err , stats ) => {
51
49
if (err || stats .hasErrors ()) {
52
- // [Handle errors here](#error-handling)
50
+ // ...
53
51
}
54
52
// Done processing
55
53
});
@@ -96,14 +94,14 @@ again. Concurrent compilations will corrupt the output files.
96
94
Calling the ` run ` method on the ` Compiler ` instance is much like the quick run
97
95
method mentioned above:
98
96
99
- ``` js-with-links
97
+ ``` js
100
98
const webpack = require (' webpack' );
101
99
102
100
const compiler = webpack ({
103
- // [Configuration Object](/configuration/)
101
+ // ...
104
102
});
105
103
106
- compiler.run((err, stats) => { // [Stats Object](#stats-object)
104
+ compiler .run ((err , stats ) => {
107
105
// ...
108
106
109
107
compiler .close ((closeErr ) => {
@@ -124,21 +122,24 @@ change, runs again. Returns an instance of `Watching`.
124
122
watch (watchOptions, callback);
125
123
```
126
124
127
- ``` js-with-links
125
+ ``` js
128
126
const webpack = require (' webpack' );
129
127
130
128
const compiler = webpack ({
131
- // [Configuration Object](/configuration/)
129
+ // ...
132
130
});
133
131
134
- const watching = compiler.watch({
135
- // Example [watchOptions](/configuration/watch/#watchoptions)
136
- aggregateTimeout: 300,
137
- poll: undefined
138
- }, (err, stats) => { // [Stats Object](#stats-object)
139
- // Print watch/build result here...
140
- console.log(stats);
141
- });
132
+ const watching = compiler .watch (
133
+ {
134
+ // Example
135
+ aggregateTimeout: 300 ,
136
+ poll: undefined ,
137
+ },
138
+ (err , stats ) => {
139
+ // Print watch/build result here...
140
+ console .log (stats);
141
+ }
142
+ );
142
143
```
143
144
144
145
` Watching ` options are covered in detail
@@ -206,8 +207,8 @@ Can be used to check if there were warnings while compiling. Returns `true` or
206
207
Returns compilation information as a JSON object. ` options ` can be either a
207
208
string (a preset) or an object for more granular control:
208
209
209
- ``` js-with-links
210
- stats.toJson('minimal'); // [more options: 'verbose', etc](/configuration/stats).
210
+ ``` js
211
+ stats .toJson (' minimal' );
211
212
```
212
213
213
214
``` js
@@ -238,22 +239,27 @@ stats.toString({
238
239
239
240
Here’s an example of ` stats.toString() ` usage:
240
241
241
- ``` js-with-links
242
+ ``` js
242
243
const webpack = require (' webpack' );
243
244
244
- webpack({
245
- // [Configuration Object](/configuration/)
246
- }, (err, stats) => {
247
- if (err) {
248
- console.error(err);
249
- return;
250
- }
245
+ webpack (
246
+ {
247
+ // ...
248
+ },
249
+ (err , stats ) => {
250
+ if (err) {
251
+ console .error (err);
252
+ return ;
253
+ }
251
254
252
- console.log(stats.toString({
253
- chunks: false, // Makes the build much quieter
254
- colors: true // Shows colors in the console
255
- }));
256
- });
255
+ console .log (
256
+ stats .toString ({
257
+ chunks: false , // Makes the build much quieter
258
+ colors: true , // Shows colors in the console
259
+ })
260
+ );
261
+ }
262
+ );
257
263
```
258
264
259
265
## MultiCompiler
@@ -263,15 +269,18 @@ separate compilers. If the `options` parameter in the webpack's NodeJS api is
263
269
an array of options, webpack applies separate compilers and calls the
264
270
` callback ` after all compilers have been executed.
265
271
266
- ``` js-with-links
272
+ ``` js
267
273
var webpack = require (' webpack' );
268
274
269
- webpack([
270
- { entry: './index1.js', output: { filename: 'bundle1.js' } },
271
- { entry: './index2.js', output: { filename: 'bundle2.js' } }
272
- ], (err, stats) => { // [Stats Object](#stats-object)
273
- process.stdout.write(stats.toString() + '\n');
274
- })
275
+ webpack (
276
+ [
277
+ { entry: ' ./index1.js' , output: { filename: ' bundle1.js' } },
278
+ { entry: ' ./index2.js' , output: { filename: ' bundle2.js' } },
279
+ ],
280
+ (err , stats ) => {
281
+ process .stdout .write (stats .toString () + ' \n ' );
282
+ }
283
+ );
275
284
```
276
285
277
286
W> Multiple configurations will ** not be run in parallel** . Each
@@ -288,32 +297,35 @@ For good error handling, you need to account for these three types of errors:
288
297
289
298
Here’s an example that does all that:
290
299
291
- ``` js-with-links
300
+ ``` js
292
301
const webpack = require (' webpack' );
293
302
294
- webpack({
295
- // [Configuration Object](/configuration/)
296
- }, (err, stats) => {
297
- if (err) {
298
- console.error(err.stack || err);
299
- if (err.details) {
300
- console.error(err.details);
303
+ webpack (
304
+ {
305
+ // ...
306
+ },
307
+ (err , stats ) => {
308
+ if (err) {
309
+ console .error (err .stack || err);
310
+ if (err .details ) {
311
+ console .error (err .details );
312
+ }
313
+ return ;
301
314
}
302
- return;
303
- }
304
315
305
- const info = stats.toJson();
316
+ const info = stats .toJson ();
306
317
307
- if (stats.hasErrors()) {
308
- console.error(info.errors);
309
- }
318
+ if (stats .hasErrors ()) {
319
+ console .error (info .errors );
320
+ }
310
321
311
- if (stats.hasWarnings()) {
312
- console.warn(info.warnings);
313
- }
322
+ if (stats .hasWarnings ()) {
323
+ console .warn (info .warnings );
324
+ }
314
325
315
- // Log result...
316
- });
326
+ // Log result...
327
+ }
328
+ );
317
329
```
318
330
319
331
## Custom File Systems
0 commit comments