Is it possible to stop requireJS from adding the .js extension automatically?

I use requireJS to load scripts. It has this detail in the docs :

The path used for the module name must not contain the .js extension, since path mapping may be for the directory.

In my application, I map all my script files in the configuration path because they are dynamically generated at runtime (my scripts start life as things like order.js , but become like order.min.b25a571965d02d9c54871b7636ca1c5e.js (this is a hash of the file’s contents , for caching purposes).

In some cases, require will add a second .js extension at the end of these paths. Although I generate dynamic paths on the server side and then fill in the config path, I must then write additional javascript code to remove the .js extension from the problem files.

Reading requireJS docs, I really don't understand why you ever wanted route matching to be used for a directory. Does this mean that you can somehow download the whole directory at the merits of files in one call? I do not understand.

Does anyone know if it’s possible to just force me to stop adding .js to the file paths, so I don’t have to hack them?

thank.

UPDATE: some code samples were added as requested.

This is inside my HTML file (this is a Scala project, so we cannot directly write these variables to 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")' }; 

Then inside my 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 above example, I need to use the string "bean" (which refers to the require path), and not on my direct object (for example, others use foo.js.modules.bar ), otherwise I get the added extra .js .

Hope this makes sense.

+47
javascript requirejs
May 14 '12 at 14:15
source share
3 answers

requirejs' noext plugin :

Downloading scripts without adding the .js extension, useful for dynamic scripts ...

Documentation

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

Main use

Place the plugins inside the baseUrl folder (usually the same folder as the main.js file), or create an alias for 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) } ); 
+13
Jun 28 2018-12-12T00:
source share

If you don't want to add a dependency on noext, you can also just add a dummy query string to the path to prevent the .js extension from being added, for example:

 require.config({ paths: { 'signalr-hubs': '/signalr/hubs?noext' } }); 

This is what the noext plugin does.

+106
Mar 13 '13 at 17:55
source share

I am using the server side of requirejs with node.js. The noext plugin does not work for me. I suspect that this is because he is trying to add? Noext to the url, and we have the file names, not the server urls.

I need to specify my .njs or .model files to separate them from static .js files. Hopefully the author will update requirejs so as not to force automatic conventions on the extension of the .js file for users.

Meanwhile, here is a quick patch to disable this behavior.

To apply this patch (versus version 2.1.15 from node_modules / requirejs / bin / r.js):

  • Save the file as 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,});

Now we can request (["test / index.njs", ...] ... and get back to work.

Save this patch to 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 lines from 1887 to node_modules / requirejs / bin / r.js:

 if (config.disableAutoExt && url.match(/\..*\.js$/)) { url = url.replace(/\.js$/, ''); } 

UPDATE: the improved patch, moving the url, changes the code deeper, so it no longer causes a hang after calling undef in the module. Undef is needed because:

To disable module caching during development using node.js, add this to your main application file:

 requirejs.onResourceLoad = function(context, map) { requirejs.undef(map.name); }; 
0
Nov 29 '14 at 14:50
source share



All Articles