是否可以阻止requireJS自动添加.js文件扩展名? [英] Is it possible to stop requireJS from adding the .js file extension automatically?

查看:187
本文介绍了是否可以阻止requireJS自动添加.js文件扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用requireJS加载脚本。它有文档中的详细信息

I'm using requireJS to load scripts. It has this detail in the docs:


用于模块名称的路径不应包含.js
扩展名,因为路径映射可能适用于目录。

The path that is used for a module name should not include the .js extension, since the path mapping could be for a directory.

在我的应用程序中,我将所有脚本文件映射到配置路径中,因为它们是在运行时动态生成的(我的脚本像<$ c一样开始生活$ c> order.js 但是会变成 order.min.b25a571965d02d9c54871b7636ca1c5e.js (这是文件内容的哈希值,用于缓存目的)。

In my app, I map all of my script files in a config path, because they're dynamically generated at runtime (my scripts start life as things like order.js but become things like order.min.b25a571965d02d9c54871b7636ca1c5e.js (this is a hash of the file contents, for cachebusting purposes).

在某些情况下,require会在这些路径的末尾添加第二个.js扩展名。虽然我在服务器端生成动态路径,然后填充配置路径,然后我必须写一些额外的JavaScript代码,从有问题的文件中删除 .js 扩展名。

In some cases, require will add a second .js extension to the end of these paths. Although I generate the dynamic paths on the server side and then populate the config path, I have to then write some extra javascript code to remove the .js extension from the problematic files.

阅读requireJS文档,我真的不喜欢不懂为什么你曾经想使用的路径映射为一个目录。这是否意味着可以在一次调用中以某种方式加载整个目录的文件?我不明白。

Reading the requireJS docs, I really don't understand why you'd ever want the path mapping to be used for a directory. Does this mean it's possible to somehow load an entire directory's worth of files in one call? I don't get it.

有没有人知道是否可以强制要求停止将.js添加到文件路径中,这样我就不必破解它了?

Does anybody know if it's possible to just force require to stop adding .js to file paths so I don't have to hack around it?

谢谢。

更新:根据要求添加了一些代码示例。

UPDATE: added some code samples as requested.

这是在我的HTML文件中(它是一个Scala项目,所以我们不能将这些变量直接写入 .js 文件):

This is inside my HTML file (it's a Scala project so we can't write these variables directly into a .js file):

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")'
};

然后在我的 main.js 中:

requirejs.config({
    paths: foo.js.modules
});

require([foo.js.modules.detect, foo.js.modules.images, "bean"], 
    function(detect, images, bean) {
        // do stuff
});

在上面的例子中,我必须使用字符串bean(指的是require路径) )而不是我的直接对象(像其他人一样使用 foo.js.modules.bar )否则我得到额外的 .js 追加。

In the example above, I have to use the string "bean" (which refers to the require path) rather than my direct object (like the others use foo.js.modules.bar) otherwise I get the extra .js appended.

希望这是有道理的。

推荐答案

requirejs ' noext插件

requirejs' noext plugin:


加载脚本而不附加.js扩展名,对动态脚本很有用...

Load scripts without appending ".js" extension, useful for dynamic scripts...

检查示例文件夹。您可能需要的所有信息都将在评论内或示例代码本身。

check the examples folder. All the info you probably need will be inside comments or on the example code itself.

Put baseUrl 文件夹中的插件(通常与main.js文件相同的文件夹)或创建插件位置的别名:

Put the plugins inside the baseUrl folder (usually same folder as the main.js file) or create an alias to the plugin location:

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)
    }
);


这篇关于是否可以阻止requireJS自动添加.js文件扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆