javascript 是否可以阻止 requireJS 自动添加 .js 文件扩展名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10585099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to stop requireJS from adding the .js file extension automatically?
提问by Matt Andrews
I'm using requireJS to load scripts. It has this detail in the docs:
我正在使用 requireJS 加载脚本。它在文档中有这个细节:
The path that is used for a module name should not include the .js extension, since the path mapping could be for a directory.
用于模块名称的路径不应包含 .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).
在我的应用程序中,我将所有脚本文件映射到一个配置路径中,因为它们是在运行时动态生成的(我的脚本开始时就像这样,order.js
但变成了这样order.min.b25a571965d02d9c54871b7636ca1c5e.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.
在某些情况下,require 会在这些路径的末尾添加第二个 .js 扩展名。虽然我在服务器端生成动态路径然后填充配置路径,但我必须编写一些额外的 javascript 代码来.js
从有问题的文件中删除扩展名。
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.
阅读 requireJS 文档,我真的不明白为什么您希望将路径映射用于目录。这是否意味着可以在一次调用中以某种方式加载整个目录的文件?我不明白。
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?
有谁知道是否可以强制要求停止将 .js 添加到文件路径中,这样我就不必绕过它?
thanks.
谢谢。
UPDATE: added some code samples as requested.
更新:根据要求添加了一些代码示例。
This is inside my HTML file (it's a Scala project so we can't write these variables directly into a .js
file):
这是在我的 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")'
};
Then inside my main.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
});
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.
在上面的示例中,我必须使用字符串“bean”(指的是 require 路径)而不是我的直接对象(就像其他人使用的一样foo.js.modules.bar
),否则我会附加额外的对象.js
。
Hope this makes sense.
希望这是有道理的。
采纳答案by jaketrent
requirejs' noext plugin:
requirejs 的noext 插件:
Load scripts without appending ".js" extension, useful for dynamic scripts...
Documentation
check the
examples
folder. All the info you probably need will be inside comments or on the example code itself.Basic usage
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) } );
加载脚本而不附加“.js”扩展名,对动态脚本很有用...
文档
检查
examples
文件夹。您可能需要的所有信息都将包含在注释中或示例代码本身中。基本用法
将插件放入
baseUrl
文件夹(通常与 main.js 文件相同的文件夹)或为插件位置创建别名: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) } );
回答by Chris Eldredge
If you don't feel like adding a dependency on noext, you can also just append a dummy query string to the path to prevent the .js extension from being appended, as in:
如果您不想添加对 noext 的依赖,您也可以只在路径中附加一个虚拟查询字符串以防止附加 .js 扩展名,如下所示:
require.config({
paths: {
'signalr-hubs': '/signalr/hubs?noext'
}
});
This is what the noext plugin does.
这就是 noext 插件所做的。
回答by hud
I am using requirejs server side with node.js. The noext plugin does not work for me. I suspect this is because it tries to add ?noext to a url and we have filenames instead of urls serverside.
我在 node.js 中使用 requirejs 服务器端。noext 插件对我不起作用。我怀疑这是因为它试图将 ?noext 添加到 url 并且我们有文件名而不是服务器端的 url。
I need to name my files .njs or .model to separate them from static .js files. Hopefully the author will update requirejs to not force automatic .js file extension conventions on the users.
我需要将我的文件命名为 .njs 或 .model 以将它们与静态 .js 文件分开。希望作者将更新 requirejs 以不对用户强制使用自动 .js 文件扩展名约定。
Meanwhile here is a quick patch to disable this behavior.
同时,这里有一个快速补丁可以禁用此行为。
To apply this patch (against version 2.1.15 of node_modules/requirejs/bin/r.js) :
要应用此补丁(针对 node_modules/requirejs/bin/r.js 的 2.1.15 版):
- Save in a file called disableAutoExt.diff or whatever and open a terminal
- cd path/to/node_modules/
- patch -p1 < path/to/disableAutoExt.diff
- add disableAutoExt: true, to your requirejs.config: requirejs.config({disableAutoExt: true,});
- 保存在一个名为 disableAutoExt.diff 的文件中,然后打开一个终端
- cd 路径/到/node_modules/
- patch -p1 < path/to/disableAutoExt.diff
- 将 disableAutoExt: true, 添加到您的 requirejs.config: requirejs.config({disableAutoExt: true,});
Now we can do require(["test/index.njs", ...] ... and get back to work.
现在我们可以执行 require(["test/index.njs", ...] ... 并重新开始工作。
Save this patch in disableAutoExt.diff :
将此补丁保存在 disableAutoExt.diff 中:
--- mod/node_modules/requirejs/bin/r.js 2014-09-07 20:54:07.000000000 -0400
+++ node_modules/requirejs/bin/r.js 2014-12-11 09:33:21.000000000 -0500
@@ -1884,6 +1884,10 @@
//Delegates to req.load. Broken out as a separate function to
//allow overriding in the optimizer.
load: function (id, url) {
+ if (config.disableAutoExt && url.match(/\..*\.js$/)) {
+ url = url.replace(/\.js$/, '');
+ }
+
req.load(context, id, url);
},
The patch simply adds the following around line 1887 to node_modules/requirejs/bin/r.js:
该补丁只是在 node_modules/requirejs/bin/r.js 的第 1887 行附近添加以下内容:
if (config.disableAutoExt && url.match(/\..*\.js$/)) {
url = url.replace(/\.js$/, '');
}
UPDATE: Improved patch by moving url change deeper in the code so it no longer causes a hang after calling undef on a module. Needed undef because:
更新:通过在代码中更深地移动 url 更改来改进补丁,因此在模块上调用 undef 后不再导致挂起。需要 undef 因为:
To disable caching of modules when developing with node.js add this to your main app file:
要在使用 node.js 开发时禁用模块缓存,请将其添加到您的主应用程序文件中:
requirejs.onResourceLoad = function(context, map)
{
requirejs.undef(map.name);
};