require.js 文本插件添加“.js";到文件名 [英] require.js text plugin adds ".js" to the file name

查看:12
本文介绍了require.js 文本插件添加“.js";到文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 requirejs 和文本插件,但遇到了奇怪的问题.

I'm trying to work with requirejs and text plugin and I have weird problem.

我有两个网络服务器:

  1. localhost:3000 - 充当 CDN 并拥有所有静态文件:js、图像、css 和模板
  2. localhost:3001 - 服务器 - 充当 REST 服务器并只提供一个文件,即 main.html 文件
  1. localhost:3000 - act as CDN and has all the static files: js, images, css and templates
  2. localhost:3001 - server - act as REST server and serve only one file, the main.html file

main.html 文件使用以下行从第二个服务器加载所有 js 文件:

The main.html file loads all the js files from the second server using the following line:

<script data-main="http://localhost:3000/js/main" 
        src="http://localhost:3000/lib/require-jquery.js"></script>

出于某种原因,当使用requirejs文本插件时,他在导航到localhost:3001时添加到模板".js"后缀>

For some reason, when using the requirejs text plugin, he adds to the templates ".js" suffix when navigating to localhost:3001

我使用以下语法:

define ['jquery','backbone','underscore','models/model','text!templates/main.html', 
        'views/navigation', 'views/player', 'views/content', 'views/header']

当我导航到 localhost:3000 时,它工作正常.

when I navigate to localhost:3000 it works fine.

您能想到文本插件在从远程服务器(例如 CDN 服务器)提供文本文件时出现问题的任何原因吗?

Can you think of any reason that the text plugin would have problems serving text files from a remote server (for example, CDN server)?

推荐答案

我已经挖掘了文本插件的代码.

I've digged in the code of the text plugin.

我发现文本插件假定开发人员将文本模板转换为 html,因为它驻留在不同的域中.

I've found out that the text plugin assumes that the developer converted the text template to html since it resides on a different domain.

我已将文本插件的代码更改为不假设它.

I've change the code of the text plugin to not assume it.

有人认为我做错了什么?

Someone thinks that I'm doing something wrong?

插件原代码:

            //Load the text. Use XHR if possible and in a browser.
            if (!hasLocation || useXhr(url, defaultProtocol, defaultHostName, defaultPort)) {
                text.get(url, function (content) {
                    text.finishLoad(name, parsed.strip, content, onLoad, config);
                });
            } else {
                //Need to fetch the resource across domains. Assume
                //the resource has been optimized into a JS module. Fetch
                //by the module name + extension, but do not include the
                //!strip part to avoid file system issues.
                req([nonStripName], function (content) {
                    text.finishLoad(parsed.moduleName + '.' + parsed.ext,
                                    parsed.strip, content, onLoad, config);
                });
            }

这篇关于require.js 文本插件添加“.js";到文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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