Skip to content

Consider allowing map configs for loader plugin resource IDs #497

Closed

Description

@jrburke

Spawned by the post from Gavin Jackson in this thread:
http://groups.google.com/group/requirejs/browse_thread/thread/28d8fa2aba128a59

One solution would be to map 'cs!whatever' to 'whatever' via an injection of requirejs config after a build, since after a build it would not make sense to dynamically load 'cs!whatever' if it was a built JS layer.

Not sure this is the right solution yet. It may require:

  • a change to the optimizer to allow specifying "config injection", which would mean parsing for a requirejs.config() call.
  • Update almond if this change goes in.

I did a quick hack (really needs cleanup) in the require-cs project to do this in makeModuleMap:

if (name) {
    if (prefix) {
        if (pluginModule && pluginModule.normalize) {
            //Plugin is loaded, use its normalize method.
            normalizedName = pluginModule.normalize(name, function (name) {
                return normalize(name, parentName, applyMap);
            });
        } else {
            normalizedName = normalize(name, parentName, applyMap);
        }

        //THIS IS THE NEW PART
        if (config.map && config.map['*'] && config.map['*'][prefix + '!' + normalizedName]) {
            normalizedName = config.map['*'][prefix + '!' + normalizedName];
            prefix = undefined;
            if (normalizedName.indexOf('!') === -1) {
                url = context.nameToUrl(normalizedName);
            }
        }
    } else {
       //LEFT OUT FOR CLARITY
    }
}

So it would be limited to full module IDs, not the partial mapping done for JS module map config, and only for '*', so normalized modules. I think this is OK though -- it only makes sense on normalized IDs.

One question would be if this is "map" config or if it should be something like "pluginMap" to indicate the special rules around full IDs only. That probably makes more sense, avoids confusion about non-* values not working.

Activity

  1. jrburke commented on Oct 14, 2012

    @jrburke
    MemberAuthor

    pluginMap seems better. Putting this in the 2.1.2 bucket as 2.1.1 is almost out the door. This may get pushed to a 2.2.0 if further implementation investigation makes this awkward. In particular, doing config auto-injection.

    Maybe this is just mainConfigFile -- using that to know where to inject.

  2. jrburke commented on Nov 20, 2012

    @jrburke
    MemberAuthor

    The config auto injection is the big blocker for this. Running out of time for 2.1.2, pushing forward, may still end up in 2.2.0.

  3. guybedford commented on Nov 21, 2012

    @guybedford
    Contributor

    Firstly, am I correct in assuming this applies to all plugins, not just cs!? So the same would happen for say, require-css!css-file being path mapped to a layer? If so, perhaps a solution could double up with paths config using the following rule:

    1. When reading paths configs, extract out any paths configs containing a '!' indicating that they are plugin path maps and not standard paths configs. This is ok since paths maps with a '!' make no sense otherwise.
    2. When processing a require first check if the moduleId matches one of these 'plugin maps' exactly and if so apply the mapping.
    3. All other path rules apply normally after this.

    That's the best I can come up with. I have a custom script populating all the build paths to the layers (as you previously suggested). This script would work directly with such a method above as it wouldn't need to distinguish between plugins and normal paths separating the configs.

  4. jrburke commented on Nov 30, 2012

    @jrburke
    MemberAuthor

    @guybedford yes it would be generic for all plugins. I'm favoring "pluginMap" for the config value and it would work like "map", except it would work on full plugin IDs, not partial module ID prefixes as "map" does today.

    The other part of this is that the optimizer would inject this config directly into probably the mainConfigFile's config section. This last part is what causes this bug to be pushed forward to a later release.

  5. guybedford commented on Dec 1, 2012

    @guybedford
    Contributor

    @jrburke the pluginMap support sounds great - it is much needed.

    I'm still not entirely sure though why this needs a configuration injection. If a layer is loaded and defines an absolute moduleId, 'cs!my/module', then surely a subsequent request to 'cs!my/module' will load directly from the defined version without needing access to the plugin?

  6. jrburke commented on Dec 3, 2012

    @jrburke
    MemberAuthor

    The problem is initial load. If all is built, there is no need to load the plugin, the built .js file is the thing that should get loaded.

  7. jrburke commented on Dec 3, 2012

    @jrburke
    MemberAuthor

    As @guybedford mentions in requirejs/r.js#327, this same sort of map would be useful for non-plugin resources.

    So, a way to inject a paths config that points loading of certain modules IDs to the same built file.

    Since that requires the same requirejs config modification as this ticket, I'll track it in this ticket for now.

  8. guybedford commented on Dec 7, 2012

    @guybedford
    Contributor

    @jrburke I think I finally understand what you mean - are you indicating that the injection of this new configuration property would happen into the main config file?

    So if this was main.js, then the config at the top of main.js would be ammended with this new layer map property?

    One thing to bear in mind is also allowing this file to be created itself, so that in my case, where I'm running a multi-page app where HTML pages are dynamically generated, the app can load this created JSON file and then use this to do the injection itself for all pages. This would cover #327.

  9. jrburke commented on Dec 10, 2012

    @jrburke
    MemberAuthor

    Yes, the config block found via mainConfigFile would be modified. The first pass of this in the optimizer will target the mainConfigFile. Once that gets sorted out, maybe we can do another pass that allows mainConfigFile to just be a JSON file.

  10. guybedford commented on Dec 12, 2012

    @guybedford
    Contributor

    Thanks, I'm happy to help out along these lines once the initial code is up. It's specifically getting the config injection data within in the NodeJS optimization that I am targeting here. When running an optimization as part of a larger build / app, getting this info is quite important for page generation when the framework handles the injection itself.

  11. jrburke commented on Jan 18, 2013

    @jrburke
    MemberAuthor

    This is partly done, in that r.js can find a config, then inject a modified config in a file. Tests here.

    However, I am waiting on the result of the "precise ID config" amd-implement discussion to do the final config stamping work.

    So pushing the rest of this until 2.1.4.

  12. guybedford commented on Jan 26, 2013

    @guybedford
    Contributor

    Having finally sorted out the build, this is now the final roadblock (I hope) for my production environment.

    So if there's anything I can do to help, just let me know - happy to get involved.

    Note - specifically the issue is ensuring that requires to plugin resource module IDs can load from built layers through a pluginMap config or paths variation.

  13. guybedford commented on Jan 26, 2013

    @guybedford
    Contributor

    Although this isn't too critical, because in most scenarios another resource hits the layer first stopping this from being a major issue just yet.

  14. jrburke commented on Feb 19, 2013

    @jrburke
    MemberAuthor

    Going to see about bundling this with #638, and given the config rewriting, best to do in a .0 release. I do hope that 2.2.0 is the next release after 2.1.5 though. Based on the amd discussion list, a layers property might make sense. Although for #638, making sure map configs for loader plugin resource IDs work would be good. Since loader plugin IDs, they would be exact match hits since the ID is opaque to the loader.

  15. jrburke commented on Dec 2, 2013

    @jrburke
    MemberAuthor

    Consider for 2.1.10: has come up in case for dojo i18n bundling, where it would want to load a layer like this:

    define('dojo/i18n', function(){});
    
    define('dojo/i18n!myapp/nls/greetings', {});
    
    define('dojo/i18n!myapp/nls/fr/greetings', {});
    
    define('dojo/i18n!myapp/nls/fr-fr/greetings', {});

    with a module doing this:

    define(['dojo/i18n!myapp/nls/greetings'], function(b) {
    
    });

    Ideally this sort of config would work:

    require.config({
        //pluginMap, or some other 
        exactMap: {        
            'dojo/i18n!myapp/nls/greetings': 'bundle'
        },
    })

    See if it makes sense to just put this in 2.1.10 instead of 2.2.

  16. jrburke commented on Jan 6, 2014

    @jrburke
    MemberAuthor

    Just committed support for a "bundles" config, will be part of 2.1.10:

    requirejs.config({
        bundles: {
            'primary': ['main', 'util', 'text', 'text!template.html'],
            'secondary': ['text!secondary.html']
        }
    });

    Keys and values are full module IDs, not module ID prefixes, and not path segments. API doc will be updated once 2.1.10 is pushed.

  17. guybedford commented on Jan 6, 2014

    @guybedford
    Contributor

    Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions