• 简单的方式
  • 复杂的方式
  • Compiler
  • Watching
  • stats
    • stats.hasErrors()
    • stats.hasWarnings()
    • stats.toJson(options)
    • stats.toString(options)
  • error handling
  • compile to memory

    简单的方式

    1. var webpack = require("webpack");
    2. // returns a Compiler instance
    3. webpack({
    4. // configuration
    5. }, function(err, stats) {
    6. // ...
    7. });

    复杂的方式

    1. var webpack = require("webpack");
    2. // returns a Compiler instance
    3. var compiler = webpack({
    4. // configuration
    5. });
    6. compiler.run(function(err, stats) {
    7. // ...
    8. });
    9. // or
    10. compiler.watch({ // watch options:
    11. aggregateTimeout: 300, // wait so long for more changes
    12. poll: true // use polling instead of native watchers
    13. // pass a number to set the polling interval
    14. }, function(err, stats) {
    15. // ...
    16. });

    Compiler

    Compiler 引用有如下方法:

    compiler.run(callback) - Builds the bundle(s).

    • 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 a Watching instance. Note: since this will automatically run an initial build, so you only need to run watch (and not run).

    • watchOptions
      • watchOptions.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. true uses 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 that handler is called multiple times. It even can occur that handler is called for the same bundle multiple times. In this cases webpack is not sure about changes and rebuilds.)

    Watching

    An instance of Watching has the following method:

    watcher.close(callback) - stops the watcher.

    • callback - A function that’s called when the watcher has closed.

    stats

    The Stats object exposes these methods:

    stats.hasErrors()

    Returns true if there were errors while compiling.

    stats.hasWarnings()

    Returns true if there were warnings while compiling.

    stats.toJson(options)

    Return information as json object

    You can specify the information by the options argument: (Boolean)

    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

    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

    In toJson every flag defaults to true (except chunkModules). By default it’s not sorted.

    Here is an example of the resulting JSON.

    Note: If you want to extract the asset name for generating the HTML page, use the assetsByChunkName property, which contains an object mapping chunkName to asset name(s) (it’s a string or an array of strings).

    stats.toString(options)

    Returns a formatted string of the result.

    options are the same as options in toJson.

    options.colors With console colors

    error handling

    to handle all errors and warnings with the node.js API you need to test err, stats.errors and stats.warnings:

    1. var webpack = require("webpack");
    2. webpack({
    3. // configuration
    4. }, function(err, stats) {
    5. if(err)
    6. return handleFatalError(err);
    7. var jsonStats = stats.toJson();
    8. if(jsonStats.errors.length > 0)
    9. return handleSoftErrors(jsonStats.errors);
    10. if(jsonStats.warnings.length > 0)
    11. handleWarnings(jsonStats.warnings);
    12. successfullyCompiled();
    13. });

    compile to memory

    1. var MemoryFS = require("memory-fs");
    2. var webpack = require("webpack");
    3. var fs = new MemoryFS();
    4. var compiler = webpack({ ... });
    5. compiler.outputFileSystem = fs;
    6. compiler.run(function(err, stats) {
    7. // ...
    8. var fileContent = fs.readFileSync("...");
    9. });