spmjs / grunt-cmd-transport

Transport everything into common module.
MIT License
70 stars 32 forks source link

seajs依赖层级超过4层,transport的时候会丢失第四层以后的依赖关系 #68

Open grassmu opened 10 years ago

grassmu commented 10 years ago

A依赖B,B依赖C,C依赖D,我打包A模块,打包的结果是D模块没有了

popomore commented 10 years ago

把你的邮件发这里吧

popomore commented 10 years ago
  1. 是否配了 alias
  2. 命名应该不支持 /
  3. 如果相对路径请使用 ./
grassmu commented 10 years ago

那如果不支持的话,是不是应该打包都会失败的呢,为何只有4层以上依赖会失败?

popomore commented 10 years ago

就是没有提取,直接用的 require 的

grassmu commented 10 years ago

我发给你的邮件内容有详细的代码,亲,是我哪里写的不对?

daddybh commented 9 years ago

我也有相同的问题,无法提取第四层的,请问楼主找到问题了吗?

amriogit commented 9 years ago

第四层依赖无法提取的原因是顶级 ID 不会递归查找依赖,而相对 ID 会。

伪代码

fetchModule(id)
  return module.factory + loadDepsModule(module.depIds)

loadDepsModule(depIds)
  result = ''
  forEach id in depIds
    if isRelativeId(id) 
      result += fetchModule(id)
    else 
      result += getModule(id).factory
  return result

所以想全部合并的话,只有两个办法:

  1. 全部使用相对 ID ,利用递归查找相对依赖的特性
  2. 先合并依赖到的顶级 ID 模块,再进行下一步合并工作
    基于以上的限制,本人写了一个会递归查找所有依赖的合并工具 amrio-seajs-builder 在本人目前的项目中已经替代了 SPM/grunt-cmd-* 构建方案
lealife commented 9 years ago

经分析, 其实不是层级的原因. 是绝对与相对路径的原因. 比如有以下目录

src/
  a.js
  b.js
  c.js
  d.js

依赖关系是a -> b -> c -> d

现在分析a的依赖关系. 如果在每个依赖都是用相对的路径来包含, 比如require('./b/'), './c', './d'. 分析a时就能把b, c, d都包含进来.

如果在a中require('/src/b') 是以绝对路径依赖b的, 此时只能分析到a的依赖关系为: 'b', 'c'. 表明, 绝对模块的依赖不会递归分析.

分析代码就可以看出. https://github.com/spmjs/grunt-cmd-transport/blob/master/tasks/lib/script.js#L160

return parsed.dependencies.map(function(id) {
        // 如果是相对模块
        if (id.charAt(0) === '.') {
          var origId = id;
          id = iduri.appendext(id);
          var depFilepath = path.join(path.dirname(filepath), id);
          var depFile = getFileInfo(depFilepath);
          if (!depFile) return;
          var obj = {
            id: origId,
            path: depFile.path,
            contents: depFile.contents,
            hash: depFile.hash,
            relative: true
          };
          if (depMap) depMap[origId] = obj;
          // 递归分析子模块依赖
          return [obj].concat(parseFileDependencies(depFilepath));
        } else {
          // 绝对模块不递归分析
          return parseModuleDependencies(id);
        }
      });

解决方法: http://life.leanote.com/post/grunt-cmd-transport-deps-bugs

nightost commented 8 years ago

问题依旧,难道transport不更新了么。。。