1.module #

2.build流程 #

modulebuild3

3.编译队列控制 #

semaphore

4.entry配置 #

4.1 字符串 #

entry: './index.js',

4.2 字符串数组 #

entry: ['./index1.js', './index2.js']

4.3 对象 #

 entry: {
    main: './main.js'
 }

4.4 函数 #

entry: () => './index.js'
entry: () => new Promise((resolve) => resolve('./index.js'))

5.SingleEntryPlugin #

EntryOptionPlugin

5.1 注册模块工厂 #

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

5.2 make #

6.dependency #

module: {
  dependencies: [
    dependency: {
      module
    }
  ]
}

7.文件转 module #

7.1 create #

NormalModuleFactory.create

7.2 addModule #

addModule(module) {
  const identifier = module.identifier();
  this._modules.set(identifier, module);
  this.modules.push(module);
  this.entries.push(module);
  module.reasons.push(new ModuleReason(module, dependency, explanation));
}

7.3 buildModule #

build(options, compilation, resolver, fs, callback) {
  return this.doBuild(options, compilation, resolver, fs, err => {
        // 处理 source 这里会将 source 转为 AST,分析出所有的依赖
        const result = this.parser.parse(this._ast || this._source.source());
        handleParseResult(result);
  })
}

//获取 source
doBuild(options, compilation, resolver, fs, callback) {
    runLoaders(
        {
            resource: this.resource,
            loaders: this.loaders,
            context: loaderContext,
            readResource: fs.readFile.bind(fs)
        },
        (err, result) => {
            // createSource 会将 runLoader 得到的结果转为字符串以便后续处理
            this._source = this.createSource(
                this.binary ? asBuffer(source) : asString(source),
                resourceBuffer,
                sourceMap
            );
              return callback();
        }
    );
}

7.4 parse #

title.js

export const message = 'zhufeng';

lazy.js

export const message = 'zhufeng';

index.js

import { message } from './title.js';
console.log(message);
import('./lazy.js').then(result => {
    console.log(result);
})
console.log(__resourceQuery);
prewalkStatements(statements) {
    for (let index = 0, len = statements.length; index < len; index++) {
        const statement = statements[index];
        this.prewalkStatement(statement);
    }
}
prewalkImportDeclaration(statement) {
        const source = statement.source.value;//./title.js
        this.hooks.import.call(statement, source);
        for (const specifier of statement.specifiers) {
            const name = specifier.local.name;//name
            this.scope.renames.set(name, null);
            this.scope.definitions.add(name);
            switch (specifier.type) {
                case "ImportDefaultSpecifier":
                    this.hooks.importSpecifier.call(statement, source, "default", name);
                    break;
                case "ImportSpecifier":
                    this.hooks.importSpecifier.call(
                        statement,
                        source,
                        specifier.imported.name,
                        name
                    );
                    break;
            }
        }
}

HarmonyImportDependencyParserPlugin

const sideEffectDep = new HarmonyImportSideEffectDependency(
    source,
    parser.state.module,
    parser.state.lastHarmonyImportOrder,
    parser.state.harmonyParserScope
);
sideEffectDep.loc = statement.loc;
parser.state.module.addDependency(sideEffectDep);

7.5 依赖处理 #

_preparedEntrypoints

const afterBuild = () => {
    if (addModuleResult.dependencies) {
        this.processModuleDependencies(module, err => {
            if (err) return callback(err);
            callback(null, module);
        });
    } else {
        return callback(null, module);
    }
};
dependencies={
    NormalModuleFactory: {
    "module./title.js": [
       HarmonyImportSideEffectDependency,
       HarmonyImportSpecifierDependency
    ],
    "module./lazy.js": [
       HarmonyImportSideEffectDependency,
       HarmonyImportSpecifierDependency
    ]
  }
}
sortedDependencies = [
  {
    factory: NormalModuleFactory,
    dependencies: [
      HarmonyImportSideEffectDependency,
      HarmonyImportSpecifierDependency
    ]
  },
   {
    factory: NormalModuleFactory,
    dependencies: [
      HarmonyImportSideEffectDependency,
      HarmonyImportSpecifierDependency
    ]
  }
]
addModuleDependencies(
  module,
  dependencies,
  bail,
  cacheGroup,
  recursive,
  callback
) {
  asyncLib.forEach(
    dependencies,
    (item, callback) => {
      //获取依赖
      const dependencies = item.dependencies;
      //获取工厂
       const factory = item.factory;
      //创建模块 
      factory.create(
          (err, dependentModule) => {
            const addModuleResult = this.addModule(dependentModule);
            dependentModule = addModuleResult.module;
            // 将 module 信息写入依赖中
            iterationDependencies(dependencies);
            // build 阶段
            const afterBuild = () => {
              // build 阶段结束后有依赖的话继续处理依赖
              this.processModuleDependencies(dependentModule, callback);
            };
            this.buildModule(afterBuild);
          }
        );
    }
  );
}
dependencies{
    HarmonyImportSideEffectDependency(request:"./title.js")
}
blocks{
    ImportDependenciesBlock(request:"./lazy.js")
}

7.6 流程 #

moduleflow