webpack执行流程简析

从用户在终端输入了一行命令开始

image.png
上源代码
  1. package.json
"scripts": {
    "dev": "node build/dev-server.js --progress --colors -p",
    "build": "node build/build.js --progress --colors -p",
    "build:dll": "webpack --config build/webpack.dll.conf.js --progress --colors -p",
    "unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run",
    "e2e": "node test/e2e/runner.js",
    "test": "npm run unit && npm run e2e",
    "lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs",
    "analyz": "NODE_ENV=production npm_config_report=true npm run build"
  }
  1. node_modules/.2.7.0@webpack/bin/webpack.js
#!/usr/bin/env node

/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra
*/
var path = require("path");

// Local version replace global one
try {
    // 得到node_modules下webpack.js的绝对路径
    var localWebpack = require.resolve(path.join(process.cwd(), "node_modules", "webpack", "bin", "webpack.js"));
    if (__filename !== localWebpack) {
        return require(localWebpack);
    }
} catch (e) { }
// 获取命令行里的参数
var yargs = require("yargs")
    .usage("webpack " + require("../package.json").version + "\n" +
    "Usage: https://webpack.js.org/api/cli/\n" +
    "Usage without config file: webpack  [] \n" +
    "Usage with config file: webpack");

require("./config-yargs")(yargs);

var DISPLAY_GROUP = "Stats options:";
var BASIC_GROUP = "Basic options:";

yargs.options({
    "json": {
        type: "boolean",
        alias: "j",
        describe: "Prints the result as JSON."
    },
    "progress": {
        type: "boolean",
        describe: "Print compilation progress in percentage",
        group: BASIC_GROUP
    }....

var argv = yargs.argv;

if (argv.verbose) {
    argv["display-reasons"] = true;
    ....
}

var options = require("./convert-argv")(yargs, argv);

function ifArg(name, fn, init) {
    if (Array.isArray(argv[name])) {
        if (init) init();
        argv[name].forEach(fn);
    } else if (typeof argv[name] !== "undefined") {
        if (init) init();
        fn(argv[name], -1);
    }
}

function processOptions(options) {
    // process Promise
    if (typeof options.then === "function") {
        options.then(processOptions).catch(function (err) {
            console.error(err.stack || err);
            process.exit(1); // eslint-disable-line
        });
        return;
    }
    var firstOptions = [].concat(options)[0];
    var statsPresetToOptions = require("../lib/Stats.js").presetToOptions;

    function compilerCallback(err, stats) {
        if (!options.watch || err) {
            // Do not keep cache anymore
            compiler.purgeInputFileSystem();
        }
        if (outputOptions.json) {
            process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n");
        } else if (stats.hash !== lastHash) {
            lastHash = stats.hash;
            process.stdout.write(stats.toString(outputOptions) + "\n");
        }
        if (!options.watch && stats.hasErrors()) {
            process.on("exit", function () {
                process.exit(2); // eslint-disable-line
            });
        }
    }
    if (firstOptions.watch || options.watch) {
        var watchOptions = firstOptions.watchOptions || firstOptions.watch || options.watch || {};
        if (watchOptions.stdin) {
            process.stdin.on("end", function () {
                process.exit(0); // eslint-disable-line
            });
            process.stdin.resume();
        }
        compiler.watch(watchOptions, compilerCallback);
        console.log("\nWebpack is watching the files…\n");
    } else
        compiler.run(compilerCallback);

}

processOptions(options);

  1. config-yargs.js
var CONFIG_GROUP = "Config options:";
var BASIC_GROUP = "Basic options:";
var MODULE_GROUP = "Module options:";
var OUTPUT_GROUP = "Output options:";
var ADVANCED_GROUP = "Advanced options:";
var RESOLVE_GROUP = "Resolving options:";
var OPTIMIZE_GROUP = "Optimizing options:";

module.exports = function(yargs) {
    yargs
        .help("help")
        .alias("help", "h", "?")
        .version()
        .alias("version", "v")
        .options({
            "config": {
                type: "string",
                describe: "Path to the config file",
                group: CONFIG_GROUP,
                defaultDescription: "webpack.config.js or webpackfile.js",
                requiresArg: true
            },
            "env": {
                describe: "Environment passed to the config, when it is a function",
                group: CONFIG_GROUP
            },
            "hot": {
                type: "boolean",
                describe: "Enables Hot Module Replacement",
                group: ADVANCED_GROUP
            },
            "debug": {
                type: "boolean",
                describe: "Switch loaders to debug mode",
                group: BASIC_GROUP
            },
            "devtool": {
                type: "string",
                describe: "Enable devtool for better debugging experience (Example: --devtool eval-cheap-module-source-map)",
                group: BASIC_GROUP,
                requiresArg: true
            },
            
        }).strict();
};

你可能感兴趣的:(webpack执行流程简析)