Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

合并文件时,优先从内存中读取(匿名模块),否则才同步IO操作(命名模块) #101

Open
wants to merge 1 commit into
base: 1.0/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/processor/module-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ ModuleCompiler.prototype.isExclude = function( file ) {
* @param {Function} callback 处理完成回调函数
*/
ModuleCompiler.prototype.process = function ( file, processContext, callback ) {

var configFile = edp.path.resolve(
processContext.baseDir, this.configFile );

Expand All @@ -107,16 +108,18 @@ ModuleCompiler.prototype.process = function ( file, processContext, callback ) {
* 编译模块
*
* @inner
* @param {ProcessContext} processContext 构建环境对象
* @param {Object} fileInfo 文件信息对象
* @param {string} modId 模块id
* @param {boolean|Object} combine 合并选项,用于编译其依赖模块
*/
function compileModule( fileInfo, modId, combine ) {
function compileModule(processContext, fileInfo, modId, combine ) {
if ( !fileInfo || file.get( 'module-combined' ) ) {
return;
}

var moduleCode = require( '../util/compile-module' )(
processContext,
fileInfo.data,
modId,
configFile,
Expand Down Expand Up @@ -146,6 +149,7 @@ ModuleCompiler.prototype.process = function ( file, processContext, callback ) {
isCombined = isCombined || !!isModuleCombined;
if ( index === 0 ) {
compiledCodes = compileModule(
processContext,
file,
moduleId,
isModuleCombined
Expand Down
47 changes: 25 additions & 22 deletions lib/util/compile-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ function getPackageMainCode( packageInfo ) {

/**
* 合并模块的代码
*
*
* @param {ProcessContext} processContext 构建环境对象
* @param {ModuleInfo} moduleInfo 入口模块的信息.
* @param {string} moduleConfig module.conf的路径.
* @param {Object} excludeModules 需要排除的模块Id集合.
*/
function combineModuleCode( moduleInfo, moduleConfig, excludeModules ) {
function combineModuleCode(processContext, moduleInfo, moduleConfig, excludeModules ) {
var combineModules = moduleInfo.combineModules;

var codes = [];
Expand All @@ -47,24 +48,25 @@ function combineModuleCode( moduleInfo, moduleConfig, excludeModules ) {

if ( fs.existsSync( depFile ) ) {
excludeModules[ depId ] = 1;

var code = compileModule(
fs.readFileSync( depFile, 'UTF-8' ),
depId,
moduleConfig,
// 我们不传递include下去,因为第一个入口文件能看到include的内容,所以相关的代码已经包含进来了,
// 后续的文件看不到也没关系,另外传递下去可能导致无限递归
//
// 我们也不传递exclude下去,因为第一次的时候,已经把exclude中的pattern全部展开了,所以
// 此时excludeModules里面是没有pattern的,都是具体的moduleId,因此不需要传递最原始的combine.exclude了
//
// 综上所述,递归调用的时候,我们只需要传递{},表明需要combine即可.
//
// @see edp-build#19
{},
excludeModules
);

var depFileHash = require('path').relative(processContext.baseDir, depFile);
var code = compileModule(
processContext,
// 优先从内存中读取文件内容(针对匿名模块,按照路径查找),否则同步读取文件(针对命名模块)
processContext.files[depFileHash] ? (processContext.files[depFileHash].data) : (fs.readFileSync( depFile, 'UTF-8' )),
depId,
moduleConfig,
// 我们不传递include下去,因为第一个入口文件能看到include的内容,所以相关的代码已经包含进来了,
// 后续的文件看不到也没关系,另外传递下去可能导致无限递归
//
// 我们也不传递exclude下去,因为第一次的时候,已经把exclude中的pattern全部展开了,所以
// 此时excludeModules里面是没有pattern的,都是具体的moduleId,因此不需要传递最原始的combine.exclude了
//
// 综上所述,递归调用的时候,我们只需要传递{},表明需要combine即可.
//
// @see edp-build#19
{},
excludeModules
);
if ( !code ) {
edp.log.warn(
'Combine module code failed, can\'t combine [%s] to [%s]',
Expand Down Expand Up @@ -161,14 +163,15 @@ function getAllModules( moduleConfig ) {
/**
* 编译模块
*
* @param {ProcessContext} processContext 构建环境对象
* @param {string} code 模块代码
* @param {string} moduleId 模块id
* @param {string} moduleConfig 模块配置文件
* @param {boolean|Object=} combine 合并依赖编译选项
* @param {Object=} excludeModules 如果合并依赖,需要一个对象指定不需要合并的模块
* @return {string}
*/
function compileModule( code, moduleId, moduleConfig, combine, excludeModules ) {
function compileModule(processContext, code, moduleId, moduleConfig, combine, excludeModules ) {
//{{{ 根据语法树分析模块
var ast = edp.amd.getAst( code );
if ( !ast ) {
Expand Down Expand Up @@ -266,7 +269,7 @@ function compileModule( code, moduleId, moduleConfig, combine, excludeModules )
exclude: explicitExcludeModules,
include: explicitIncludeModules
} );
var depCodes = combineModuleCode( moduleInfo, moduleConfig, excludeModules );
var depCodes = combineModuleCode(processContext, moduleInfo, moduleConfig, excludeModules );
codes.push.apply( codes, depCodes );

if ( moduleId !== moduleInfo.id ) {
Expand Down