dojo / has

:rocket: Dojo 2 - feature detection library.
http://dojo.io
Other
3 stars 18 forks source link

Support Builder Plugin API #4

Closed kitsonk closed 8 years ago

kitsonk commented 8 years ago

We should attempt to support the RequireJS builder plugin API for has. In particular write() could be implemented, which would write out modules that meet the conditions expressed in the MID.

kitsonk commented 8 years ago

Actually, I tried this with r.js and the way the module behaves is as you would expect it, as r.js will invoke the module, which will resolve the module as per the feature flags, which then will get loaded and built into a layer.

There are two challenges, first r.js doesn't understand the UMD wrapping, so the modules have to be built as AMD for r.js to work. Second, it will of course detect the environment that it runs under (NodeJS) and the features there. Therefore the static features need to be supplied. One way of doing that is providing it in the r.js configuration:

(function () {
    /* grab reference to global scope */
    Function('return this')().DojoHasEnvironment = {
        staticFeatures: {
            'host-browser': true
        }
    };

    return {
        appDir: '../',
        baseUrl: '.',
        dir: '../../built',
        packages: [
            { name: 'dojo-has', location: 'node_modules/dojo-has/dist/umd' }
        ],
        optimize: 'none',
        modules: [
            { name: 'main' }
        ]
    };
}())

In this situation, main looks like the following:

define([ 'dojo-has/has!host-browser?./a:./b' ], function () {
    console.log('main');
});

With the static features provided, r.js will include module ./a in the bundle, without the static features ./b will be included in the bundle.