- Notifications
You must be signed in to change notification settings - Fork125
node.js api
varwebpack=require("webpack");// returns a Compiler instancewebpack({// configuration},function(err,stats){// ...});
varwebpack=require("webpack");// returns a Compiler instancevarcompiler=webpack({// configuration});compiler.run(function(err,stats){// ...});// orcompiler.watch({// watch options:aggregateTimeout:300,// wait so long for more changespoll:true// use polling instead of native watchers// pass a number to set the polling interval},function(err,stats){// ...});
An instance ofCompiler has the following methods
compiler.run(callback) - Builds the bundle(s). It makes an incremental build.
- callback(err, stats) - A function that will be called with the build is complete.
var watcher = compiler.watch(watchOptions, handler) - Builds the bundle(s) then starts the watcher, which rebuilds bundles whenever their source files change. Returns aWatching instance. Note: since this will automatically run an initial build, so you only need to runwatch (and notrun).
watchOptionswatchOptions.aggregateTimeout- After a change the watcher waits that time (in milliseconds) for more changes. Default: 300.watchOptions.poll- The watcher uses polling instead of native watchers.trueuses the default interval, a number specifies a interval in milliseconds. Default: undefined (automatic).
handler(err, stats)- A function that will be called when a build has been completed, or an error or warning has occurred. (Note thathandleris called multiple times. It even can occur thathandleris called for the same bundle multiple times. In this cases webpack is not sure about changes and rebuilds.)
An instance ofWatching has the following method:
watcher.close(callback) - stops the watcher.
callback- A function that's called when the watcher has closed.
TheStats object exposes these methods:
Returnstrue if there were errors while compiling.
Returnstrue if there were warnings while compiling.
Return information as JSON object.
There are a few different logging levels, pass these asoptions (e.g. `stats.toJson("verbose")):
"none" (orfalse) output nothing
"errors-only" only output when errors happen
"minimal" only output when errors or a new compilation happen
"normal" (ortrue) standard output
"verbose" output all the information webpack has
For more granular control, pass an object that can have these booleans set:
options.context (string) context directory for request shortening
options.hash add the hash of the compilation
options.version add webpack version information
options.timings add timing information
options.assets add assets information
options.chunks add chunk information (setting this tofalse allows for a less verbose output)
options.chunkModules add built modules information to chunk information
options.modules add built modules information
options.children add children information
options.cached add also information about cached (not built) modules
options.reasons add information about the reasons why modules are included
options.source add the source code of modules
options.errorDetails add details to errors (like resolving log)
options.chunkOrigins add the origins of chunks and chunk merging info
options.modulesSort (string) sort the modules by that field
options.chunksSort (string) sort the chunks by that field
options.assetsSort (string) sort the assets by that field
Here is anexample of the resulting JSON.
Note: If you want to extract the asset name for generating the HTML page, use the
assetsByChunkNameproperty, which contains an object mappingchunkNameto asset name(s) (it's a string or an array of strings).
Returns a formatted string of the result.
options are the same asoptions intoJson.
options.colors With console colors
This is an example of howstats.toString can be used:
varwebpack=require("webpack");webpack({// configuration},function(err,stats){if(err){thrownewgutil.PluginError('webpack:build',err);}gutil.log('[webpack:build]',stats.toString({chunks:false,// Makes the build much quietercolors:true}));});
to handle all errors and warnings with the node.js API you need to testerr,stats.errors andstats.warnings:
varwebpack=require("webpack");webpack({// configuration},function(err,stats){if(err)returnhandleFatalError(err);varjsonStats=stats.toJson();if(jsonStats.errors.length>0)returnhandleSoftErrors(jsonStats.errors);if(jsonStats.warnings.length>0)handleWarnings(jsonStats.warnings);successfullyCompiled();});
varMemoryFS=require("memory-fs");varwebpack=require("webpack");varfs=newMemoryFS();varcompiler=webpack({ ...});compiler.outputFileSystem=fs;compiler.run(function(err,stats){// ...varfileContent=fs.readFileSync("...");});
webpack 👍