我的主目录中有一个require.config,如下所示。
require.config({ baseUrl:“脚本/”, 路径:{ jquery:'shell/lib/jquery/jquery-1.7.1‘//许多库和模块在这里别名},map:{ '*':{ 'underscore':'shell/lib/underscore/underscore’//其他几个模块映射在这里} });
之所以这样做,是因为映射中定义的文件使用的是使用相对路径的内部依赖关系(在它们各自的文件夹中)。现在,当我运行优化器时,在path中定义的模块被保存为模块ID,就像jquery保存为jquery一样,而那些在地图中的模块正在获得完整的路径,比如‘shell/lib/下划线/下划线’而不是‘下划线’。
这会导致问题,因为我在其他模块中使用“下划线”,优化后的文件也有‘下划线’而不是‘shell/lib/下划线/下划线’。
有什么特别的方法来优化当我们给地图吐露或什么东西我错过了吗?请告诉我怎么修。
谢谢
发布于 2013-09-14 19:04:18
我不太明白这个问题:
这会导致问题,因为我在其他模块中使用“下划线”,优化后的文件也有‘下划线’而不是‘shell/lib/下划线/下划线’。
这似乎是预期的行为,您将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;
}
}这个例子告诉r.js:当您处理somefile.js时,将src/underscore替换为underscore (这正是您使用地图时所做的)。但只是为了向您展示如何使用onBuildWrite来做一些讨厌的事情)。
https://stackoverflow.com/questions/12523795
复制相似问题