Mat*_*ews 52 javascript requirejs
我正在使用requireJS来加载脚本.它在文档中有这个细节:
用于模块名称的路径不应包含.js扩展名,因为路径映射可以用于目录.
在我的应用程序中,我将所有脚本文件映射到配置路径中,因为它们是在运行时动态生成的(我的脚本开始像生活一样,order.js
但变成类似的东西order.min.b25a571965d02d9c54871b7636ca1c5e.js
(这是文件内容的哈希,用于缓存目的) .
在某些情况下,require会在这些路径的末尾添加第二个.js扩展名.虽然我在服务器端生成动态路径然后填充配置路径,但我必须编写一些额外的javascript代码以.js
从有问题的文件中删除扩展.
阅读requireJS文档,我真的不明白为什么你想要将路径映射用于目录.这是否意味着可以在一次调用中以某种方式加载整个目录的文件?我不明白.
有没有人知道是否可以强制要求停止将.js添加到文件路径,这样我就不必破解它了?
谢谢.
更新:根据请求添加了一些代码示例.
这是在我的HTML文件中(它是一个Scala项目,所以我们不能将这些变量直接写入.js
文件):
foo.js.modules = {
order : '@Static("javascripts/order.min.js")',
reqwest : 'http://5.foo.appspot.com/js/libs/reqwest',
bean : 'http://4.foo.appspot.com/js/libs/bean.min',
detect : 'order!http://4.foo.appspot.com/js/detect/detect.js',
images : 'order!http://4.foo.appspot.com/js/detect/images.js',
basicTemplate : '@Static("javascripts/libs/basicTemplate.min.js")',
trailExpander : '@Static("javascripts/libs/trailExpander.min.js")',
fetchDiscussion : '@Static("javascripts/libs/fetchDiscussion.min.js")'
mostPopular : '@Static("javascripts/libs/mostPopular.min.js")'
};
Run Code Online (Sandbox Code Playgroud)
然后在我的内心main.js
:
requirejs.config({
paths: foo.js.modules
});
require([foo.js.modules.detect, foo.js.modules.images, "bean"],
function(detect, images, bean) {
// do stuff
});
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我必须使用字符串"bean"(指的是require路径)而不是我的直接对象(就像其他人使用的那样foo.js.modules.bar
),否则我会得到额外的.js
附加.
希望这是有道理的.
Chr*_*dge 110
如果您不想在noext上添加依赖项,您还可以在路径中附加一个虚拟查询字符串,以防止附加.js扩展名,如下所示:
require.config({
paths: {
'signalr-hubs': '/signalr/hubs?noext'
}
});
Run Code Online (Sandbox Code Playgroud)
这就是noext插件的功能.
jak*_*ent 14
requirejs'noext plugin:
加载脚本而不附加".js"扩展名,对动态脚本很有用......
文档
检查
examples
文件夹.您可能需要的所有信息都将在注释内或示例代码本身上.基本用法
将插件放在
baseUrl
文件夹中(通常与main.js文件相同)或为插件位置创建别名:Run Code Online (Sandbox Code Playgroud)require.config({ paths : { //create alias to plugins (not needed if plugins are on the baseUrl) async: 'lib/require/async', font: 'lib/require/font', goog: 'lib/require/goog', image: 'lib/require/image', json: 'lib/require/json', noext: 'lib/require/noext', mdown: 'lib/require/mdown', propertyParser : 'lib/require/propertyParser', markdownConverter : 'lib/Markdown.Converter' } }); //use plugins as if they were at baseUrl define([ 'image!awsum.jpg', 'json!data/foo.json', 'noext!js/bar.php', 'mdown!data/lorem_ipsum.md', 'async!http://maps.google.com/maps/api/js?sensor=false', 'goog!visualization,1,packages:[corechart,geochart]', 'goog!search,1', 'font!google,families:[Tangerine,Cantarell]' ], function(awsum, foo, bar, loremIpsum){ //all dependencies are loaded (including gmaps and other google apis) } );