webpack(2) -编译

2020-12-19  本文已影响0人  Lyan_2ab3

webpack 准备阶段

这个阶段的主要工作,是创建 CompilerCompilation 实例。

WebpackOptionsApply 这个模块主要是根据options选项的配置,设置compile的相应的插件,属性,里面写了大量的 apply(compiler); 使得模块的this指向compiler

class WebpackOptionsApply extends OptionsApply{
  constructor() {
     super();
  }
  process(options, compiler){
     compiler.outputPath = options.output.path;
     compiler.recordsInputPath = options.recordsInputPath || null;
     compiler.recordsOutputPath = options.recordsOutputPath || null;
     compiler.name = options.name;
    //...
        new JavascriptModulesPlugin().apply(compiler);
        new JsonModulesPlugin().apply(compiler);
        new AssetModulesPlugin().apply(compiler);
    //...
        new EntryOptionPlugin().apply(compiler);
     // 触发事件点entry-options并传入参数 context和entry 
        compiler.hooks.entryOption.call(options.context, options.entry);
 }  
}
class EntryOptionPlugin {
    /**
     * @param {Compiler} compiler the compiler instance one is tapping into
     * @returns {void}
     */
    apply(compiler) {
        compiler.hooks.entryOption.tap("EntryOptionPlugin", (context, entry) => {
            EntryOptionPlugin.applyEntryOption(compiler, context, entry);
            return true;
        });
    }

    /**
     * @param {Compiler} compiler the compiler
     * @param {string} context context directory
     * @param {Entry} entry request
     * @returns {void}
     */
    static applyEntryOption(compiler, context, entry) {
        if (typeof entry === "function") {
            const DynamicEntryPlugin = require("./DynamicEntryPlugin");
            new DynamicEntryPlugin(context, entry).apply(compiler);
        } else {
            const EntryPlugin = require("./EntryPlugin");
            for (const name of Object.keys(entry)) {
                const desc = entry[name];
                const options = EntryOptionPlugin.entryDescriptionToOptions(
                    compiler,
                    name,
                    desc
                );
                for (const entry of desc.import) {
                    new EntryPlugin(context, entry, options).apply(compiler);
                }
            }
        }
    }

    /**
     * @param {Compiler} compiler the compiler
     * @param {string} name entry name
     * @param {EntryDescription} desc entry description
     * @returns {EntryOptions} options for the entry
     */
    static entryDescriptionToOptions(compiler, name, desc) {
        /** @type {EntryOptions} */
        const options = {
            name,
            filename: desc.filename,
            runtime: desc.runtime,
            dependOn: desc.dependOn,
            chunkLoading: desc.chunkLoading,
            wasmLoading: desc.wasmLoading,
            library: desc.library
        };
        if (desc.chunkLoading) {
            const EnableChunkLoadingPlugin = require("./javascript/EnableChunkLoadingPlugin");
            EnableChunkLoadingPlugin.checkEnabled(compiler, desc.chunkLoading);
        }
        if (desc.wasmLoading) {
            const EnableWasmLoadingPlugin = require("./wasm/EnableWasmLoadingPlugin");
            EnableWasmLoadingPlugin.checkEnabled(compiler, desc.wasmLoading);
        }
        if (desc.library) {
            const EnableLibraryPlugin = require("./library/EnableLibraryPlugin");
            EnableLibraryPlugin.checkEnabled(compiler, desc.library.type);
        }
        return options;
    }
}

module.exports = EntryOptionPlugin;
//  EntryPlugin 

    compiler.hooks.compilation.tap(
            "EntryPlugin",
            (compilation, { normalModuleFactory }) => {
                compilation.dependencyFactories.set(
                    EntryDependency,
                    normalModuleFactory
                );
            }
        );

compiler.hooks.make.tapAsync("EntryPlugin", (compilation, callback) => {
            const { entry, options, context } = this;

            const dep = EntryPlugin.createDependency(entry, options);
            compilation.addEntry(context, dep, options, err => {
                callback(err);
            });
        });
    }

另外要注意的一点是,任务点 run 只有在 webpack 以正常模式运行的情况下会触发,如果我们以监听(watch)的模式运行 webpack,那么任务点 run 是不会触发的,但是会触发任务点 watch-run

// Compiler 中 createNormalModuleFactory
createNormalModuleFactory() {
    const normalModuleFactory = new NormalModuleFactory({
           context: this.options.context,
            fs: this.inputFileSystem,
            resolverFactory: this.resolverFactory,
            options: this.options.module || {},
            associatedObjectForCache: this.root
    });
    this.hooks.normalModuleFactory.call(normalModuleFactory);
    return normalModuleFactory;
    }

modules 和 chunks 的生成阶段

先解析项目依赖的所有 modules,再根据 modules 生成 chunks
module 解析,包含了三个主要步骤:创建实例、loaders应用以及依赖收集
chunks 生成,主要步骤是找到 chunk 所需要包含的 modules。

文件生成阶段

上一篇 下一篇

猜你喜欢

热点阅读