4

我的 main 中有一个 require.config,如下所示。

require.config({ baseUrl
:'scripts/',

 paths:{  
        jquery:'shell/lib/jquery/jquery-1.7.1'
     // many libraries and modules are aliased here

 },
 map:{
  '*':{
      'underscore':'shell/lib/underscore/underscore'
            // a few other modules are mapped here
  }
 }     

});

我这样做是因为 map 中定义的文件使用相对路径使用内部依赖项(在它们各自的文件夹中)。现在,当我运行优化器时,路径中定义的模块保存为模块 ID,例如 jquery 保存为jquery,而 map 中的模块正在获取完整路径,例如“下划线”为“shell/lib/下划线/下划线”而不是“下划线” .

这会导致问题,因为我也在其他模块中使用“下划线”,并且优化的文件有“下划线”而不是“shell/lib/underscore/underscore”。

当我们提供地图配置或我缺少的东西时,是否有一些特定的优化方法?请告诉我如何解决它。

谢谢

4

1 回答 1

1

我不确定是否理解这个问题:

这会导致问题,因为我也在其他模块中使用“下划线”,并且优化的文件有“下划线”而不是“shell/lib/underscore/underscore”。

这似乎是预期的行为,您映射underscore到所有模块的该路径。所以基本上你告诉r.js:每次你发现underscore依赖重写它shell/lib/underscore/underscore。如果您的模块使用“内部路径”并且您想做相反的事情(使它们成为参考underscore),您需要做相反的映射:

 'some/path/underscore': 'underscore'

在这种情况下,所有模块都将指向同一个下划线模块。即使是那些使用一些奇怪的下划线路径的人。

在极端情况下,您需要控制如何r.js在磁盘上写入模块。您可以使用该onBuildWrite属性(请参阅https://github.com/jrburke/r.js/blob/master/build/example.build.js#L517)。

例如:

onBuildWrite: function ( moduleName, path, contents ) {
    if ( path === './src/somefile.js' ) {
          return contents.replace(/^define\('src\/underscore'/, "define('underscore'");
    } else {
          return contents;
    }
}

这个例子是一个“hack”,它告诉r.js:当你处理时somefile.js,替换src/underscoreunderscore(这正是你对 map 所做的......但只是为了向你展示如何使用onBuildWrite它来做讨厌的事情)。

于 2013-09-14T19:04:18.697 回答