darkwebdev / lasso-modernizr

Modernizr plugin for Lasso.js
1 stars 1 forks source link

Suggested improvements #1

Open patrick-steele-idem opened 8 years ago

patrick-steele-idem commented 8 years ago

I suggest updating this plugin to not write the modernizr-custom.min.js to disk. Instead store the output of calling modernizr in String and use a custom dependency type to add that string to the output JavaScript bundle:

'use strict';

var modernizr = require('customizr');
var extend = require('raptor-util').extend;

module.exports = function (lasso, config) {
    var defaults = {
        cache: true,
        devFile: undefined, /* Don't write the file to disk, just give us a string back */
        dest: null,
        options: [],
        uglify: false, /* Don't minify! Let Lasso.js do that! */
        tests: [],
        excludeTests: [],
        crawl: true,
        useBuffers: false,
        files: {
            src: [
                '*[^(g|G)runt(file)?].{js,css,scss}',
                '**[^node_modules]/**/*.{js,css,scss}',
                '!lib/**/*'
            ]
        },
        customTests: []
    };

    var src = modernizr(extend(defaults, config));
    lasso.registerJavaScriptType('lasso-modernizr-output', {
        properties: {
        },

        init: function(lassoContext, callback) {
            callback();
        },

        cacheConfig: {
            cacheable: true,
            static: true
        },

        getDir: function() {
            return null;
        },

        read: function(context, callback) {
            return src;
        },

        getSourceFile: function() {
            return null;
        },

        getLastModified: function(lassoContext, callback) {
            return callback(null, -1);
        }
    });
};

With that change, the developer only needs to add the following file to their browser.json file:

{
    "dependencies": [
        "lasso-modernizr-output"
    ]
}
patrick-steele-idem commented 8 years ago

Also, you might want to lazily run modernizer() since it is only needed if the lasso-modernizr-output dependency has been included.

darkwebdev commented 8 years ago

Agree with everything above and I think it makes sense to turn on autoload by default and in case when we don't need Modernizr on every page we should be able to disable autoload and put plugin explicit in browser.json.