webpack执行机制流程是怎么样的 webpack执行机制流程是怎么样的
webpack\u6267\u884c\u673a\u5236\u6d41\u7a0b\u662f\u600e\u4e48\u6837\u7684Sims2PackInstaller_v1514.exe \u5982\u679c\u662f\u8fd9\u4e2a\u5c31\u662f\u7528\u6765\u5b89\u88c5Sims2Pack\u7684\u5de5\u5177\uff0c \u4e5f\u53ef\u4ee5\u7528\u6765\u6574\u7406\u5b89\u88c5\u540e\u7684\u4e1c\u897f\uff0c \u76f8\u5f53\u4e8e\u5b98\u65b9\u7684\u201c\u5185\u5bb9\u7ba1\u7406\u5668\u201d\uff0c
\u3000\u3000\u51e0\u4e4e\u6240\u6709\u4e1a\u52a1\u7684\u5f00\u53d1\u6784\u5efa\u90fd\u4f1a\u7528\u5230 webpack \u3002\u7684\u786e\uff0c\u4f5c\u4e3a\u6a21\u5757\u52a0\u8f7d\u548c\u6253\u5305\u795e\u5668\uff0c\u53ea\u9700\u914d\u7f6e\u51e0\u4e2a\u6587\u4ef6\uff0c\u52a0\u8f7d\u5404\u79cd loader \u5c31\u53ef\u4ee5\u4eab\u53d7\u65e0\u75db\u6d41\u7a0b\u5316\u5f00\u53d1\u3002\u4f46\u5bf9\u4e8e webpack \u8fd9\u6837\u4e00\u4e2a\u590d\u6742\u5ea6\u8f83\u9ad8\u7684\u63d2\u4ef6\u96c6\u5408\uff0c\u5b83\u7684\u6574\u4f53\u6d41\u7a0b\u53ca\u601d\u60f3\u5bf9\u6211\u4eec\u6765\u8bf4\u8fd8\u662f\u5f88\u900f\u660e\u7684\u3002\u90a3\u4e48\u63a5\u4e0b\u6765\u6211\u4f1a\u5e26\u4f60\u4e86\u89e3 webpack \u8fd9\u6837\u4e00\u4e2a\u6784\u5efa\u9ed1\u76d2\uff0c\u9996\u5148\u6765\u8c08\u8c08\u5b83\u7684\u6d41\u7a0b\u3002
\u3000\u3000\u51c6\u5907\u5de5\u4f5c
\u3000\u30001. webstorm \u4e2d\u914d\u7f6e webpack-webstorm-debugger-script
\u3000\u3000\u5728\u5f00\u59cb\u4e86\u89e3\u4e4b\u524d\uff0c\u5fc5\u987b\u8981\u80fd\u5bf9 webpack \u6574\u4e2a\u6d41\u7a0b\u8fdb\u884c debug \uff0c\u914d\u7f6e\u8fc7\u7a0b\u6bd4\u8f83\u7b80\u5355\u3002
\u3000\u3000\u5148\u5c06 webpack-webstorm-debugger-script \u4e2d\u7684\u8f6f\u4ef6\u5916\u5305\u4f01\u4e1a\u516c\u53f8http://www.yingtaow.com?webstorm-debugger.js \u7f6e\u4e8e webpack.config.js \u7684\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u642d\u5efa\u597d\u4f60\u7684\u811a\u624b\u67b6\u540e\u5c31\u53ef\u4ee5\u76f4\u63a5 Debug \u8fd9\u4e2a webstorm-debugger.js \u6587\u4ef6\u4e86\u3002
\u3000\u30002. webpack.config.js \u914d\u7f6e
\u3000\u3000\u4f30\u8ba1\u5927\u5bb6\u5bf9 webpack.config.js \u7684\u914d\u7f6e\u4e5f\u5c1d\u8bd5\u8fc7\u4e0d\u5c11\u6b21\u4e86\uff0c\u8fd9\u91cc\u5c31\u5927\u81f4\u5bf9\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u8fdb\u884c\u4e2a\u5206\u6790\u3002
\u3000\u3000var path = require('path');
\u3000\u3000var node_modules = path.resolve(__dirname, 'node_modules');
\u3000\u3000var pathToReact = path.resolve(node_modules, 'react/dist/react.min.js');
\u3000\u3000module.exports = {
\u3000\u3000// \u5165\u53e3\u6587\u4ef6\uff0c\u662f\u6a21\u5757\u6784\u5efa\u7684\u8d77\u70b9\uff0c\u540c\u65f6\u6bcf\u4e00\u4e2a\u5165\u53e3\u6587\u4ef6\u5bf9\u5e94\u6700\u540e\u751f\u6210\u7684\u4e00\u4e2a chunk\u3002
\u3000\u3000entry: {
\u3000\u3000bundle: [
\u3000\u3000'webpack/hot/dev-server',
\u3000\u3000'webpack-dev-server/client?http://localhost:8080',
\u3000\u3000path.resolve(__dirname, 'app/app.js')
\u3000\u3000],
\u3000\u3000},
\u3000\u3000// \u6587\u4ef6\u8def\u5f84\u6307\u5411(\u53ef\u52a0\u5feb\u6253\u5305\u8fc7\u7a0b)\u3002
\u3000\u3000resolve: {
\u3000\u3000alias: {
\u3000\u3000'react': pathToReact
\u3000\u3000}
\u3000\u3000},
\u3000\u3000// \u751f\u6210\u6587\u4ef6\uff0c\u662f\u6a21\u5757\u6784\u5efa\u7684\u7ec8\u70b9\uff0c\u5305\u62ec\u8f93\u51fa\u6587\u4ef6\u4e0e\u8f93\u51fa\u8def\u5f84\u3002
\u3000\u3000output: {
\u3000\u3000path: path.resolve(__dirname, 'build'),
\u3000\u3000filename: '[name].js',
\u3000\u3000},
\u3000\u3000// \u8fd9\u91cc\u914d\u7f6e\u4e86\u5904\u7406\u5404\u6a21\u5757\u7684 loader \uff0c\u5305\u62ec css \u9884\u5904\u7406 loader \uff0ces6 \u7f16\u8bd1 loader\uff0c\u56fe\u7247\u5904\u7406 loader\u3002
\u3000\u3000module: {
\u3000\u3000loaders: [
\u3000\u3000{
\u3000\u3000test: /\.js$/,
\u3000\u3000loader: 'babel',
\u3000\u3000query: {
\u3000\u3000presets: ['es2015', 'react']
\u3000\u3000}
\u3000\u3000}
\u3000\u3000],
\u3000\u3000noParse: [pathToReact]
\u3000\u3000},
\u3000\u3000// webpack \u5404\u63d2\u4ef6\u5bf9\u8c61\uff0c\u5728 webpack \u7684\u4e8b\u4ef6\u6d41\u4e2d\u6267\u884c\u5bf9\u5e94\u7684\u65b9\u6cd5\u3002
\u3000\u3000plugins: [
\u3000\u3000new webpack.HotModuleReplacementPlugin();
\u3000\u3000]
\u3000\u3000};
\u3000\u3000\u9664\u6b64\u4e4b\u5916\u518d\u5927\u81f4\u4ecb\u7ecd\u4e0b webpack \u7684\u4e00\u4e9b\u6838\u5fc3\u6982\u5ff5:
\u3000\u3000loader : \u80fd\u8f6c\u6362\u5404\u7c7b\u8d44\u6e90\uff0c\u5e76\u5904\u7406\u6210\u5bf9\u5e94\u6a21\u5757\u7684\u52a0\u8f7d\u5668\u3002loader \u95f4\u53ef\u4ee5\u4e32\u884c\u4f7f\u7528\u3002
\u3000\u3000chunk : code splitting\u540e\u7684\u4ea7\u7269\uff0c\u4e5f\u5c31\u662f\u6309\u9700\u52a0\u8f7d\u7684\u5206\u5757\uff0c\u88c5\u8f7d\u4e86\u4e0d\u540c\u7684module\u3002
\u3000\u3000\u5bf9\u4e8emodule\u548cchunk\u7684\u5173\u7cfb\u53ef\u4ee5\u53c2\u7167webpack\u5b98\u65b9\u7684\u8fd9\u5f20\u56fe:
\u3000\u3000plugin : webpack \u7684\u63d2\u4ef6\u5b9e\u4f53\uff0c\u8fd9\u91cc\u4ee5 UglifyJsPlugin \u4e3a\u4f8b\u3002
\u3000\u3000function UglifyJsPlugin(options) {
\u3000\u3000this.options = options;
\u3000\u3000}
\u3000\u3000module.exports = UglifyJsPlugin;
\u3000\u3000UglifyJsPlugin.prototype.apply = function(compiler) {
\u3000\u3000compiler.plugin("compilation", function(compilation) {
\u3000\u3000compilation.plugin("build-module", function(module) {
\u3000\u3000});
\u3000\u3000compilation.plugin("optimize-chunk-assets", function(chunks, callback) {
\u3000\u3000// Uglify \u903b\u8f91
\u3000\u3000});
\u3000\u3000compilation.plugin("normal-module-loader", function(context) {
\u3000\u3000});
\u3000\u3000});
\u3000\u3000};
\u3000\u3000\u5728 webpack \u4e2d\u4f60\u7ecf\u5e38\u53ef\u4ee5\u770b\u5230 compilation.plugin('xxx', callback) \uff0c\u4f60\u53ef\u4ee5\u628a\u5b83\u5f53\u4f5c\u662f\u4e00\u4e2a\u4e8b\u4ef6\u7684\u7ed1\u5b9a\uff0c\u8fd9\u4e9b\u4e8b\u4ef6\u5728\u6253\u5305\u65f6\u7531 webpack \u6765\u89e6\u53d1\u3002
\u3000\u30003. \u6d41\u7a0b\u603b\u89c8
\u3000\u3000\u5728\u5177\u4f53\u6d41\u7a0b\u5b66\u4e60\u524d\uff0c\u53ef\u4ee5\u5148\u901a\u8fc7\u8fd9\u5e45 webpack\u6574\u4f53\u6d41\u7a0b\u56fe \u4e86\u89e3\u4e00\u4e0b\u5927\u81f4\u6d41\u7a0b(\u5efa\u8bae\u4fdd\u5b58\u4e0b\u6765\u67e5\u770b)\u3002
\u3000\u3000shell \u4e0e config \u89e3\u6790
\u3000\u3000\u6bcf\u6b21\u5728\u547d\u4ee4\u884c\u8f93\u5165 webpack \u540e\uff0c\u64cd\u4f5c\u7cfb\u7edf\u90fd\u4f1a\u53bb\u8c03\u7528 ./node_modules/.bin/webpack \u8fd9\u4e2a shell \u811a\u672c\u3002\u8fd9\u4e2a\u811a\u672c\u4f1a\u53bb\u8c03\u7528./node_modules/webpack/bin/webpack.js \u5e76\u8ffd\u52a0\u8f93\u5165\u7684\u53c2\u6570\uff0c\u5982 -p , -w \u3002(\u56fe\u4e2d webpack.js \u662f webpack \u7684\u542f\u52a8\u6587\u4ef6\uff0c\u800c $@ \u662f\u540e\u7f00\u53c2\u6570)
\u3000\u3000\u5728 webpack.js \u8fd9\u4e2a\u6587\u4ef6\u4e2d webpack \u901a\u8fc7 optimist \u5c06\u7528\u6237\u914d\u7f6e\u7684 webpack.config.js \u548c shell \u811a\u672c\u4f20\u8fc7\u6765\u7684\u53c2\u6570\u6574\u5408\u6210 options \u5bf9\u8c61\u4f20\u5230\u4e86\u4e0b\u4e00\u4e2a\u6d41\u7a0b\u7684\u63a7\u5236\u5bf9\u8c61\u4e2d\u3002
\u3000\u30001. optimist
\u3000\u3000\u548c commander \u4e00\u6837\uff0coptimist \u5b9e\u73b0\u4e86 node \u547d\u4ee4\u884c\u7684\u89e3\u6790\uff0c\u5176 API \u8c03\u7528\u975e\u5e38\u65b9\u4fbf\u3002
\u3000\u3000var optimist = require("optimist");
\u3000\u3000optimist
\u3000\u3000.boolean("json").alias("json", "j").describe("json")
\u3000\u3000.boolean("colors").alias("colors", "c").describe("colors")
\u3000\u3000.boolean("watch").alias("watch", "w").describe("watch")
\u3000\u3000...
\u3000\u3000\u83b7\u53d6\u5230\u540e\u7f00\u53c2\u6570\u540e\uff0coptimist \u5206\u6790\u53c2\u6570\u5e76\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u628a\u53c2\u6570\u5bf9\u8c61\u4fdd\u5b58\u5728 optimist.argv \u4e2d\uff0c\u6765\u770b\u770b argv \u7a76\u7adf\u6709\u4ec0\u4e48\uff1f
\u3000\u3000// webpack --hot -w
\u3000\u3000{
\u3000\u3000hot: true,
\u3000\u3000profile: false,
\u3000\u3000watch: true,
\u3000\u3000...
\u3000\u3000}
\u3000\u30002. config \u5408\u5e76\u4e0e\u63d2\u4ef6\u52a0\u8f7d
\u3000\u3000\u5728\u52a0\u8f7d\u63d2\u4ef6\u4e4b\u524d\uff0cwebpack \u5c06 webpack.config.js \u4e2d\u7684\u5404\u4e2a\u914d\u7f6e\u9879\u62f7\u8d1d\u5230 options \u5bf9\u8c61\u4e2d\uff0c\u5e76\u52a0\u8f7d\u7528\u6237\u914d\u7f6e\u5728 webpack.config.js \u7684 plugins \u3002\u63a5\u7740 optimist.argv \u4f1a\u88ab\u4f20\u5165\u5230 ./node_modules/webpack/bin/convert-argv.js \u4e2d\uff0c\u901a\u8fc7\u5224\u65ad argv \u4e2d\u53c2\u6570\u7684\u503c\u51b3\u5b9a\u662f\u5426\u53bb\u52a0\u8f7d\u5bf9\u5e94\u63d2\u4ef6\u3002(\u81f3\u4e8e webpack \u63d2\u4ef6\u8fd0\u884c\u673a\u5236\uff0c\u5728\u4e4b\u540e\u7684\u8fd0\u884c\u673a\u5236\u7bc7\u4f1a\u63d0\u5230)
\u3000\u3000ifBooleanArg("hot", function() {
\u3000\u3000ensureArray(options, "plugins");
\u3000\u3000var HotModuleReplacementPlugin = require("../lib/HotModuleReplacementPlugin");
\u3000\u3000options.plugins.push(new HotModuleReplacementPlugin());
\u3000\u3000});
\u3000\u3000...
\u3000\u3000return options;
\u3000\u3000options \u4f5c\u4e3a\u6700\u540e\u8fd4\u56de\u7ed3\u679c\uff0c\u5305\u542b\u4e86\u4e4b\u540e\u6784\u5efa\u9636\u6bb5\u6240\u9700\u7684\u91cd\u8981\u4fe1\u606f\u3002
\u3000\u3000{
\u3000\u3000entry: {},//\u5165\u53e3\u914d\u7f6e
\u3000\u3000output: {}, //\u8f93\u51fa\u914d\u7f6e
\u3000\u3000plugins: [], //\u63d2\u4ef6\u96c6\u5408(\u914d\u7f6e\u6587\u4ef6 + shell\u6307\u4ee4)
\u3000\u3000module: { loaders: [ [Object] ] }, //\u6a21\u5757\u914d\u7f6e
\u3000\u3000context: //\u5de5\u7a0b\u8def\u5f84
\u3000\u3000...
\u3000\u3000}
\u3000\u3000\u8fd9\u548c webpack.config.js \u7684\u914d\u7f6e\u975e\u5e38\u76f8\u4f3c\uff0c\u53ea\u662f\u591a\u4e86\u4e00\u4e9b\u7ecf shell \u4f20\u5165\u7684\u63d2\u4ef6\u5bf9\u8c61\u3002\u63d2\u4ef6\u5bf9\u8c61\u4e00\u521d\u59cb\u5316\u5b8c\u6bd5\uff0c options \u4e5f\u5c31\u4f20\u5165\u5230\u4e86\u4e0b\u4e2a\u6d41\u7a0b\u4e2d\u3002
\u3000\u3000var webpack = require("../lib/webpack.js");
\u3000\u3000var compiler = webpack(options);
\u3000\u3000\u7f16\u8bd1\u4e0e\u6784\u5efa\u6d41\u7a0b
\u3000\u3000\u5728\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u548c shell \u540e\u7f00\u53c2\u6570\u7533\u660e\u7684\u63d2\u4ef6\uff0c\u5e76\u4f20\u5165\u6784\u5efa\u4fe1\u606f options \u5bf9\u8c61\u540e\uff0c\u5f00\u59cb\u6574\u4e2a webpack \u6253\u5305\u6700\u6f2b\u957f\u7684\u4e00\u6b65\u3002\u800c\u8fd9\u4e2a\u65f6\u5019\uff0c\u771f\u6b63\u7684 webpack \u5bf9\u8c61\u624d\u521a\u88ab\u521d\u59cb\u5316\uff0c\u5177\u4f53\u7684\u521d\u59cb\u5316\u903b\u8f91\u5728 lib/webpack.js \u4e2d\uff0c\u5982\u4e0b:
\u3000\u3000function webpack(options) {
\u3000\u3000var compiler = new Compiler();
\u3000\u3000...// \u68c0\u67e5options,\u82e5watch\u5b57\u6bb5\u4e3atrue,\u5219\u5f00\u542fwatch\u7ebf\u7a0b
\u3000\u3000return compiler;
\u3000\u3000}
\u3000\u3000...
\u3000\u3000webpack \u7684\u5b9e\u9645\u5165\u53e3\u662f Compiler \u4e2d\u7684 run \u65b9\u6cd5\uff0crun \u4e00\u65e6\u6267\u884c\u540e\uff0c\u5c31\u5f00\u59cb\u4e86\u7f16\u8bd1\u548c\u6784\u5efa\u6d41\u7a0b \uff0c\u5176\u4e2d\u6709\u51e0\u4e2a\u6bd4\u8f83\u5173\u952e\u7684 webpack \u4e8b\u4ef6\u8282\u70b9\u3002
\u3000\u3000compile \u5f00\u59cb\u7f16\u8bd1
\u3000\u3000make \u4ece\u5165\u53e3\u70b9\u5206\u6790\u6a21\u5757\u53ca\u5176\u4f9d\u8d56\u7684\u6a21\u5757\uff0c\u521b\u5efa\u8fd9\u4e9b\u6a21\u5757\u5bf9\u8c61
\u3000\u3000build-module \u6784\u5efa\u6a21\u5757
\u3000\u3000after-compile \u5b8c\u6210\u6784\u5efa
\u3000\u3000seal \u5c01\u88c5\u6784\u5efa\u7ed3\u679c
\u3000\u3000emit \u628a\u5404\u4e2achunk\u8f93\u51fa\u5230\u7ed3\u679c\u6587\u4ef6
\u3000\u3000after-emit \u5b8c\u6210\u8f93\u51fa
\u3000\u30001. \u6838\u5fc3\u5bf9\u8c61 Compilation
\u3000\u3000compiler.run \u540e\u9996\u5148\u4f1a\u89e6\u53d1 compile \uff0c\u8fd9\u4e00\u6b65\u4f1a\u6784\u5efa\u51fa Compilation \u5bf9\u8c61:
\u3000\u3000compilation\u7c7b\u56fe
\u3000\u3000\u8fd9\u4e2a\u5bf9\u8c61\u6709\u4e24\u4e2a\u4f5c\u7528\uff0c\u4e00\u662f\u8d1f\u8d23\u7ec4\u7ec7\u6574\u4e2a\u6253\u5305\u8fc7\u7a0b\uff0c\u5305\u542b\u4e86\u6bcf\u4e2a\u6784\u5efa\u73af\u8282\u53ca\u8f93\u51fa\u73af\u8282\u6240\u5bf9\u5e94\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4ece\u56fe\u4e2d\u770b\u5230\u6bd4\u8f83\u5173\u952e\u7684\u6b65\u9aa4\uff0c\u5982 addEntry() , _addModuleChain() , buildModule() , seal() , createChunkAssets() (\u5728\u6bcf\u4e00\u4e2a\u8282\u70b9\u90fd\u4f1a\u89e6\u53d1 webpack \u4e8b\u4ef6\u53bb\u8c03\u7528\u5404\u63d2\u4ef6)\u3002\u4e8c\u662f\u8be5\u5bf9\u8c61\u5185\u90e8\u5b58\u653e\u7740\u6240\u6709 module \uff0cchunk\uff0c\u751f\u6210\u7684 asset \u4ee5\u53ca\u7528\u6765\u751f\u6210\u6700\u540e\u6253\u5305\u6587\u4ef6\u7684 template \u7684\u4fe1\u606f\u3002
\u3000\u30002. \u7f16\u8bd1\u4e0e\u6784\u5efa\u4e3b\u6d41\u7a0b
\u3000\u3000\u5728\u521b\u5efa module \u4e4b\u524d\uff0cCompiler \u4f1a\u89e6\u53d1 make\uff0c\u5e76\u8c03\u7528 Compilation.addEntry \u65b9\u6cd5\uff0c\u901a\u8fc7 options \u5bf9\u8c61\u7684 entry \u5b57\u6bb5\u627e\u5230\u6211\u4eec\u7684\u5165\u53e3js\u6587\u4ef6\u3002\u4e4b\u540e\uff0c\u5728 addEntry \u4e2d\u8c03\u7528\u79c1\u6709\u65b9\u6cd5 _addModuleChain \uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4e3b\u8981\u505a\u4e86\u4e24\u4ef6\u4e8b\u60c5\u3002\u4e00\u662f\u6839\u636e\u6a21\u5757\u7684\u7c7b\u578b\u83b7\u53d6\u5bf9\u5e94\u7684\u6a21\u5757\u5de5\u5382\u5e76\u521b\u5efa\u6a21\u5757\uff0c\u4e8c\u662f\u6784\u5efa\u6a21\u5757\u3002
\u3000\u3000\u800c\u6784\u5efa\u6a21\u5757\u4f5c\u4e3a\u6700\u8017\u65f6\u7684\u4e00\u6b65\uff0c\u53c8\u53ef\u7ec6\u5316\u4e3a\u4e09\u6b65:
\u3000\u3000\u8c03\u7528\u5404 loader \u5904\u7406\u6a21\u5757\u4e4b\u95f4\u7684\u4f9d\u8d56
\u3000\u3000webpack \u63d0\u4f9b\u7684\u4e00\u4e2a\u5f88\u5927\u7684\u4fbf\u5229\u5c31\u662f\u80fd\u5c06\u6240\u6709\u8d44\u6e90\u90fd\u6574\u5408\u6210\u6a21\u5757\uff0c\u4e0d\u4ec5\u4ec5\u662f js \u6587\u4ef6\u3002\u6240\u4ee5\u9700\u8981\u4e00\u4e9b loader \uff0c\u6bd4\u5982 url-loader \uff0cjsx-loader \uff0c css-loader \u7b49\u7b49\u6765\u8ba9\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5728\u6e90\u6587\u4ef6\u4e2d\u5f15\u7528\u5404\u7c7b\u8d44\u6e90\u3002webpack \u8c03\u7528 doBuild() \uff0c\u5bf9\u6bcf\u4e00\u4e2a require() \u7528\u5bf9\u5e94\u7684 loader \u8fdb\u884c\u52a0\u5de5\uff0c\u6700\u540e\u751f\u6210\u4e00\u4e2a js module\u3002
\u3000\u3000Compilation.prototype._addModuleChain = function process(context, dependency, onModule, callback) {
\u3000\u3000var start = this.profile && +new Date();
\u3000\u3000...
\u3000\u3000// \u6839\u636e\u6a21\u5757\u7684\u7c7b\u578b\u83b7\u53d6\u5bf9\u5e94\u7684\u6a21\u5757\u5de5\u5382\u5e76\u521b\u5efa\u6a21\u5757
\u3000\u3000var moduleFactory = this.dependencyFactories.get(dependency.constructor);
\u3000\u3000...
\u3000\u3000moduleFactory.create(context, dependency, function(err, module) {
\u3000\u3000var result = this.addModule(module);
\u3000\u3000...
\u3000\u3000this.buildModule(module, function(err) {
\u3000\u3000...
\u3000\u3000// \u6784\u5efa\u6a21\u5757\uff0c\u6dfb\u52a0\u4f9d\u8d56\u6a21\u5757
\u3000\u3000}.bind(this));
\u3000\u3000}.bind(this));
\u3000\u3000};
\u3000\u3000\u8c03\u7528 acorn \u89e3\u6790\u7ecf loader \u5904\u7406\u540e\u7684\u6e90\u6587\u4ef6\u751f\u6210\u62bd\u8c61\u8bed\u6cd5\u6811 AST
\u3000\u3000Parser.prototype.parse = function parse(source, initialState) {
\u3000\u3000var ast;
\u3000\u3000if(!ast) {
\u3000\u3000// acorn\u4ee5es6\u7684\u8bed\u6cd5\u8fdb\u884c\u89e3\u6790
\u3000\u3000ast = acorn.parse(source, {
\u3000\u3000ranges: true,
\u3000\u3000locations: true,
\u3000\u3000ecmaVersion: 6,
\u3000\u3000sourceType: "module"
\u3000\u3000});
\u3000\u3000}
\u3000\u3000...
\u3000\u3000};
\u3000\u3000\u904d\u5386 AST\uff0c\u6784\u5efa\u8be5\u6a21\u5757\u6240\u4f9d\u8d56\u7684\u6a21\u5757
\u3000\u3000\u5bf9\u4e8e\u5f53\u524d\u6a21\u5757\uff0c\u6216\u8bb8\u5b58\u5728\u7740\u591a\u4e2a\u4f9d\u8d56\u6a21\u5757\u3002\u5f53\u524d\u6a21\u5757\u4f1a\u5f00\u8f9f\u4e00\u4e2a\u4f9d\u8d56\u6a21\u5757\u7684\u6570\u7ec4\uff0c\u5728\u904d\u5386 AST \u65f6\uff0c\u5c06 require() \u4e2d\u7684\u6a21\u5757\u901a\u8fc7addDependency() \u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d\u3002\u5f53\u524d\u6a21\u5757\u6784\u5efa\u5b8c\u6210\u540e\uff0cwebpack \u8c03\u7528 processModuleDependencies \u5f00\u59cb\u9012\u5f52\u5904\u7406\u4f9d\u8d56\u7684 module\uff0c\u63a5\u7740\u5c31\u4f1a\u91cd\u590d\u4e4b\u524d\u7684\u6784\u5efa\u6b65\u9aa4\u3002
\u3000\u3000Compilation.prototype.addModuleDependencies = function(module, dependencies, bail, cacheGroup, recursive, callback) {
\u3000\u3000// \u6839\u636e\u4f9d\u8d56\u6570\u7ec4(dependencies)\u521b\u5efa\u4f9d\u8d56\u6a21\u5757\u5bf9\u8c61
\u3000\u3000var factories = [];
\u3000\u3000for(var i = 0; i < dependencies.length; i++) {
\u3000\u3000var factory = _this.dependencyFactories.get(dependencies[i][0].constructor);
\u3000\u3000factories[i] = [factory, dependencies[i]];
\u3000\u3000}
\u3000\u3000...
\u3000\u3000// \u4e0e\u5f53\u524d\u6a21\u5757\u6784\u5efa\u6b65\u9aa4\u76f8\u540c
\u3000\u3000}
\u3000\u30003. \u6784\u5efa\u7ec6\u8282
\u3000\u3000module \u662f webpack \u6784\u5efa\u7684\u6838\u5fc3\u5b9e\u4f53\uff0c\u4e5f\u662f\u6240\u6709 module\u7684 \u7236\u7c7b\uff0c\u5b83\u6709\u51e0\u79cd\u4e0d\u540c\u5b50\u7c7b\uff1aNormalModule , MultiModule ,ContextModule , DelegatedModule \u7b49\u3002\u4f46\u8fd9\u4e9b\u6838\u5fc3\u5b9e\u4f53\u90fd\u662f\u5728\u6784\u5efa\u4e2d\u90fd\u4f1a\u53bb\u8c03\u7528\u5bf9\u5e94\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f build() \u3002\u6765\u770b\u770b\u5176\u4e2d\u5177\u4f53\u505a\u4e86\u4ec0\u4e48:
\u3000\u3000// \u521d\u59cb\u5316module\u4fe1\u606f\uff0c\u5982context,id,chunks,dependencies\u7b49\u3002
\u3000\u3000NormalModule.prototype.build = function build(options, compilation, resolver, fs, callback) {
\u3000\u3000this.buildTimestamp = new Date().getTime(); // \u6784\u5efa\u8ba1\u65f6
\u3000\u3000this.built = true;
\u3000\u3000return this.doBuild(options, compilation, resolver, fs, function(err) {
\u3000\u3000// \u6307\u5b9a\u6a21\u5757\u5f15\u7528\uff0c\u4e0d\u7ecfacorn\u89e3\u6790
\u3000\u3000if(options.module && options.module.noParse) {
\u3000\u3000if(Array.isArray(options.module.noParse)) {
\u3000\u3000if(options.module.noParse.some(function(regExp) {
\u3000\u3000return typeof regExp === "string" ?
\u3000\u3000this.request.indexOf(regExp) === 0 :
\u3000\u3000regExp.test(this.request);
\u3000\u3000}, this)) return callback();
\u3000\u3000} else if(typeof options.module.noParse === "string" ?
\u3000\u3000this.request.indexOf(options.module.noParse) === 0 :
\u3000\u3000options.module.noParse.test(this.request)) {
\u3000\u3000return callback();
\u3000\u3000}
\u3000\u3000}
\u3000\u3000// \u7531acorn\u89e3\u6790\u751f\u6210ast
\u3000\u3000try {
\u3000\u3000this.parser.parse(this._source.source(), {
\u3000\u3000current: this,
\u3000\u3000module: this,
\u3000\u3000compilation: compilation,
\u3000\u3000options: options
\u3000\u3000});
\u3000\u3000} catch(e) {
\u3000\u3000var source = this._source.source();
\u3000\u3000this._source = null;
\u3000\u3000return callback(new ModuleParseError(this, source, e));
\u3000\u3000}
\u3000\u3000return callback();
\u3000\u3000}.bind(this));
\u3000\u3000};
\u3000\u3000\u5bf9\u4e8e\u6bcf\u4e00\u4e2a module \uff0c\u5b83\u90fd\u4f1a\u6709\u8fd9\u6837\u4e00\u4e2a\u6784\u5efa\u65b9\u6cd5\u3002\u5f53\u7136\uff0c\u5b83\u8fd8\u5305\u62ec\u4e86\u4ece\u6784\u5efa\u5230\u8f93\u51fa\u7684\u4e00\u7cfb\u5217\u7684\u6709\u5173 module \u751f\u547d\u5468\u671f\u7684\u51fd\u6570
绛旓細Webpack 鐨杩愯娴佺▼鏄涓涓覆琛岀殑杩囩▼,浠庡惎鍔ㄥ埌缁撴潫浼氫緷娆鎵ц浠ヤ笅娴佺▼ :1.鍒濆鍖栧弬鏁帮細浠庨厤缃枃浠跺拰 Shell 璇彞涓鍙栦笌鍚堝苟鍙傛暟,寰楀嚭鏈缁堢殑鍙傛暟銆2.寮濮嬬紪璇戯細鐢ㄤ笂涓姝ュ緱鍒扮殑鍙傛暟鍒濆鍖 Compiler 瀵硅薄,鍔犺浇鎵鏈夐厤缃殑鎻掍欢,鎵ц瀵硅薄鐨 run 鏂规硶寮濮嬫墽琛岀紪璇戙3.纭畾鍏ュ彛锛氭牴鎹厤缃腑鐨 entry 鎵惧嚭鎵鏈夌殑...
绛旓細webstorm 涓厤缃 webpack-webstorm-debugger-script 鍦ㄥ紑濮嬩簡瑙d箣鍓,蹇呴』瑕佽兘瀵 webpack 鏁翠釜娴佺▼杩涜 debug ,閰嶇疆杩囩▼姣旇緝绠鍗曘 鍏堝皢 webpack-webstorm-debugger-script 涓殑杞欢澶栧寘浼佷笟鍏徃 缃簬 webpack.config.js 鐨勫悓涓鐩綍涓,鎼缓濂戒綘鐨勮剼鎵嬫灦鍚庡氨鍙互鐩存帴 Debug 杩欎釜 webstorm-debugger.js 鏂囦欢浜嗐 2. ...
绛旓細Webpack鐨勬梾绋嬩粠鍒涘缓涓涓疄渚嬪紑濮嬶紝閫氳繃webpack(options)锛屽畠鐏垫椿鍦版敮鎸侀潤鎬侀厤缃璞″拰鍔ㄦ佸嚱鏁板紡椤甸潰鏋勫缓銆傛瀯寤杩囩▼鍒嗕负涓ゅぇ鍏抽敭姝ラ锛歮ake闃舵璐熻矗鎵цcompilation鏋勫缓锛岃宻ear闃舵鍒欒仛鐒︿簬chunk鐨勪紭鍖栫敓鎴愩傚湪浜嬩欢椹卞姩鐨勪笘鐣岄噷锛宔ventemitter鍜孴apable鎼烘墜鍚堜綔锛屾彁渚涗簡涓板瘜鐨勬彃浠舵寕杞界偣銆俢hunk鏄ā鍧楀鐢ㄧ殑鍩虹煶锛...
绛旓細Webpack鐨鎵ц杩囩▼Webpack浠庢潯鐩紙entry锛夊紑濮嬪伐浣滐紝閫氬父杩欎簺鏄疛avaScript妯″潡锛屽叾涓webpack寮濮嬪叾閬嶅巻杩囩▼銆傚湪姝よ繃绋嬩腑锛寃ebpack浼氭牴鎹姞杞藉櫒閰嶇疆璇勪及鏉$洰锛坋ntry锛夊尮閰嶏紝杩欎簺閰嶇疆鍛婅瘔webpack濡備綍杞崲姣忎釜鍖归厤銆傝В鏋娴佺▼鏉$洰锛坋ntry锛夋湰韬氨鏄竴涓ā鍧椼傚綋webpack閬囧埌涓涓潯鐩椂锛寃ebpack浼氬皾璇曚娇鐢ㄦ潯鐩殑resolve...
绛旓細绱ф帴鐫锛岄亶鍘嗘瘡涓涓 assetNames锛屼竴涓釜浠诲姟琚 scheduleTask 鍑嗗灏辩华锛岀瓑寰呯潃鎵ц銆備换鍔″垎瑙</ 鑰屾瘡涓换鍔$殑鏍稿績 scheduleTask锛屽氨鍍忔媶瑙h皽棰樹竴鑸紝鍖呭惈鐫鑾峰彇 asset 淇℃伅銆佷唬鐮佹鏌ャ乵inify 鐨勯夋嫨锛圵orker 鎴栦富绾跨▼锛夈佹柊浠g爜鐢熸垚鍜岀紦瀛樻洿鏂帮紝浠ュ強瀵硅祫浜у唴瀹圭殑鍗虫椂鏇存柊銆傛暣涓杩囩▼绱у噾鑰屾湁搴忥紝浠ヨ祫婧愬鐞...
绛旓細loader鐢ㄦ硶閰嶇疆module: { rules: [ { test: /\.css$/, use: [ { loader: 'style-loader' }, { loader: 'css-loader' } ] } ] }鍐呰仈import Styles from 'style-loader!css-loader?modules!./styles.css';CLIwebpack --module-bind 'css=style-loader!
绛旓細webpack鎵撳寘鍘熺悊鏄牴鎹枃浠堕棿鐨勪緷璧栧叧绯诲鍏惰繘琛岄潤鎬佸垎鏋愶紝鐒跺悗灏嗚繖浜涙ā鍧楁寜鎸囧畾瑙勫垯鐢熸垚闈欐佽祫婧愶紝褰 webpack 澶勭悊绋嬪簭鏃讹紝瀹冧細閫掑綊鍦版瀯寤轰竴涓緷璧栧叧绯诲浘(dependency graph)锛屽叾涓寘鍚簲鐢ㄧ▼搴忛渶瑕佺殑姣忎釜妯″潡锛岀劧鍚庡皢鎵鏈夎繖浜涙ā鍧楁墦鍖呮垚涓涓垨澶氫釜 bundle銆倃ebpack鏈変袱绉嶇粍缁囨ā鍧楃殑渚濊禆鏂瑰紡锛屽悓姝ャ佸紓姝ャ傚紓姝...
绛旓細涓婂浘涓叆鍙e氨鏄乏涓婅鐨.js鏂囦欢銆傝繖閮ㄥ垎璇︾粏鍙互鍙傝Webpack瀹樼綉锛岃繖閲岃繖鏄瑪鑰呰瘯鐐兼椂鍋氱殑绗旇銆傚崢鍏ュ彛鏈変袱绉嶅啓娉曪細娉ㄦ剰锛屽鏋滄病鏈夐厤缃畂utput鐨刦ilename锛屼笂杈规瀯寤哄嚭鏉ョ殑dist涓枃浠跺悕鏄痠ndex.js銆傛敞鎰忥細涓婅堪澶氬叆鍙fā寮忎笅锛屾墦鍑烘潵鐨勫寘鏈変袱涓枃浠讹細index.js鍜宧ello.js锛屽氨鏄搴攅ntry瀵硅薄鐨刱ey銆傝屾墦鍖呭嚭鏉...
绛旓細浜屻webpack 鍚姩杩愯鐨 runtime 浠g爜:鐪嬭繃鎴戜箣鍓嶇殑鏂囩珷:娣卞叆鐞嗚В webpack 鏂囦欢鎵撳寘鏈哄埗 灏变細鐭ラ亾,鍦 webpack 鍚姩鐨勬椂鍊欓渶瑕鎵ц涓浜涘惎鍔ㄤ唬鐮併(function(modules) { window["webpackJsonp"] = function webpackJsonpCallback(chunkIds, moreModules) { // ... }; function __webpack_require__(moduleId)...
绛旓細鎻掍欢妯″紡鏄竴绉嶅簲鐢ㄩ潪甯稿箍娉涚殑妯″紡銆傛垜浠敤鐨勫緢澶氳蒋浠堕兘鎷ユ湁鑷韩鐨勬彃浠鏈哄埗锛岄氳繃鎻掍欢鍙互鎷撳睍杞欢鐨勫姛鑳姐傚彟澶栵紝鎻掍欢妯″紡涔熷箍娉涘簲鐢ㄤ簬 web 鏂归潰銆備緥濡 Webpack銆 Vue CLI銆乁MI銆丅abel绛夈傞偅涔堟彃浠剁郴缁熸槸濡備綍瀹炵幇鐨勫憿锛熷涓婂浘鎵绀猴紝鎻掍欢搴旂敤鐨娴佺▼寰堢畝鍗曪細鍏朵腑锛岀 3 姝ョ殑鏃跺欙紝鍥炲幓璋冪敤鎻掍欢锛岃皟鐢ㄦ彃浠舵椂...