cloudchen / grunt-template-jasmine-requirejs

RequireJS template for grunt-contrib-jasmine task
111 stars 96 forks source link

Issue with Conditional Path #40

Closed paulfairless closed 11 years ago

paulfairless commented 11 years ago

Hi,

I'm having an issue trying to set up my jasmine tests due to a conditional requireJS path for JQuery. We do detection to load JQuery 2 or JQuery 1 depending on browser capabilities.

var pathToJQuery
if('querySelector' in document
    && 'localStorage' in window
    && 'addEventListener' in window) {
    pathToJQuery = '../lib/jquery/jquery-2.0.3.min'
} else {
    pathToJQuery = '../lib/jquery/jquery-1.10.2.min'
}

require.config({
    paths: {
        modernizr: '../lib/modernizr',
        jquery: pathToJQuery,
    }
});

This generates an error

Warning: pathToJQuery is not defined Use --force to continue.
cloudchen commented 11 years ago

This template reads requirejs configuration literally by specifying of requireConfigFile key name. Variable isn't supported by literal parsing. Is it an elegant way to load different jQuery by specifying different path? The question is worth discussing.

But, I show you my workaround as well.

requirejs.config()

require.config({
    "jquery1": "src/jquery1",
    "jquery2": "src/jquery2",
    "shim": {
        "jquery1": {
            exports: '$'
        },
        "jquery2": {
            exports: '$'
        }
    },
    "map": {
        "*": {
            "jquery": "src/jquery"
        },
    }
});

jquery.js (custom AMD module)

define('querySelector' in document
    && 'localStorage' in window
    && 'addEventListener' in window ? ['jquery1'] : ['jquery2'], function($) {
    return $;
});
akinnee commented 10 years ago

Is there any way around this? This is kind of a big problem for me. I use lots of variables in my config file for path prefixes, etc.