@@ -42,14 +42,12 @@ import webpack from 'webpack';
4242
4343The imported ` webpack ` function is fed a webpack [ Configuration Object] ( /configuration/ ) and runs the webpack compiler if a callback function is provided:
4444
45- ``` js-with-links
45+ ``` js
4646const webpack = require (' webpack' );
4747
48- webpack({
49- // [Configuration Object](/configuration/)
50- }, (err, stats) => { // [Stats Object](#stats-object)
48+ webpack ({}, (err , stats ) => {
5149 if (err || stats .hasErrors ()) {
52- // [Handle errors here](#error-handling)
50+ // ...
5351 }
5452 // Done processing
5553});
@@ -96,14 +94,14 @@ again. Concurrent compilations will corrupt the output files.
9694Calling the ` run ` method on the ` Compiler ` instance is much like the quick run
9795method mentioned above:
9896
99- ``` js-with-links
97+ ``` js
10098const webpack = require (' webpack' );
10199
102100const compiler = webpack ({
103- // [Configuration Object](/configuration/)
101+ // ...
104102});
105103
106- compiler.run((err, stats) => { // [Stats Object](#stats-object)
104+ compiler .run ((err , stats ) => {
107105 // ...
108106
109107 compiler .close ((closeErr ) => {
@@ -124,21 +122,24 @@ change, runs again. Returns an instance of `Watching`.
124122watch (watchOptions, callback);
125123```
126124
127- ``` js-with-links
125+ ``` js
128126const webpack = require (' webpack' );
129127
130128const compiler = webpack ({
131- // [Configuration Object](/configuration/)
129+ // ...
132130});
133131
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+ );
142143```
143144
144145` Watching ` options are covered in detail
@@ -206,8 +207,8 @@ Can be used to check if there were warnings while compiling. Returns `true` or
206207Returns compilation information as a JSON object. ` options ` can be either a
207208string (a preset) or an object for more granular control:
208209
209- ``` js-with-links
210- stats.toJson('minimal'); // [more options: 'verbose', etc](/configuration/stats).
210+ ``` js
211+ stats .toJson (' minimal' );
211212```
212213
213214``` js
@@ -238,22 +239,27 @@ stats.toString({
238239
239240Here’s an example of ` stats.toString() ` usage:
240241
241- ``` js-with-links
242+ ``` js
242243const webpack = require (' webpack' );
243244
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+ }
251254
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+ );
257263```
258264
259265## MultiCompiler
@@ -263,15 +269,18 @@ separate compilers. If the `options` parameter in the webpack's NodeJS api is
263269an array of options, webpack applies separate compilers and calls the
264270` callback ` after all compilers have been executed.
265271
266- ``` js-with-links
272+ ``` js
267273var webpack = require (' webpack' );
268274
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+ );
275284```
276285
277286W> 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:
288297
289298Here’s an example that does all that:
290299
291- ``` js-with-links
300+ ``` js
292301const webpack = require (' webpack' );
293302
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 ;
301314 }
302- return;
303- }
304315
305- const info = stats.toJson();
316+ const info = stats .toJson ();
306317
307- if (stats.hasErrors()) {
308- console.error(info.errors);
309- }
318+ if (stats .hasErrors ()) {
319+ console .error (info .errors );
320+ }
310321
311- if (stats.hasWarnings()) {
312- console.warn(info.warnings);
313- }
322+ if (stats .hasWarnings ()) {
323+ console .warn (info .warnings );
324+ }
314325
315- // Log result...
316- });
326+ // Log result...
327+ }
328+ );
317329```
318330
319331## Custom File Systems
0 commit comments