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

前端 未结 3 1108
梦谈多话
梦谈多话 2020-12-02 11:53

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

The path that is used for a module name should not include the .js extension,

相关标签:
3条回答
  • 2020-12-02 12:39

    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:

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

    This is what the noext plugin does.

    0 讨论(0)
  • 2020-12-02 12:54

    requirejs' noext plugin:

    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)
        }
    );
    
    0 讨论(0)
  • 2020-12-02 13:00

    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.

    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.

    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) :

    1. Save in a file called disableAutoExt.diff or whatever and open a terminal
    2. cd path/to/node_modules/
    3. patch -p1 < path/to/disableAutoExt.diff
    4. add disableAutoExt: true, to your requirejs.config: requirejs.config({disableAutoExt: true,});

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

    Save this patch in 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:

    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:

    To disable caching of modules when developing with node.js add this to your main app file:

    requirejs.onResourceLoad = function(context, map)
    {
        requirejs.undef(map.name);
    };
    
    0 讨论(0)
提交回复
热议问题