Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin causes errors during r.js optimization #2

Open
yanfali opened this issue Jan 30, 2014 · 0 comments
Open

Plugin causes errors during r.js optimization #2

yanfali opened this issue Jan 30, 2014 · 0 comments

Comments

@yanfali
Copy link

yanfali commented Jan 30, 2014

r.js bails as soon as it tries to integrate l20n.js because l20n.js makes direct references to browser constructs like window, document and location, which don't exist in node.js.

Using this post requirejs/r.js#443

I modified a copy of the plugin to patch around r.js and make the plugin dynamically load l20n and the locale files:

/* global define, alert */
/* jshint camelcase: false, curly: false */
/**
 * l20n
 * The l20n module.
 * @author @fernandogmar
 */
define(['require', 'module'], function(require, module) {
    'use strict';
    var config = module.config ? module.config() : {};

    var defaults = {
        name: 'l20nCtx',
        extension: 'l20n',
        prefix: '{{',
        suffix: '}}',
        locale: 'en'
    };

    var l20nPlugin = {};

    l20nPlugin.version = '0.0.3';

    l20nPlugin.load = function(resourceName, parentRequire, onload, masterConfig) {
        config = helper.extend(config, defaults, helper.getConfig(masterConfig));
        if (masterConfig.isBuild) {
            return onload();
        }
        require(['l20n'], function(L20n) {
            var full_name = helper.getFullName(resourceName, config.extension);
            var resource_id = parentRequire.toUrl(full_name);

            var ctx = L20n.getContext(resource_id);
            ctx.linkResource(helper.parseName(resource_id, config));
            ctx.requestLocales();

            var addEventListeners = function() {
                ctx.addEventListener('ready', onReady);
                ctx.addEventListener('error', onError);
            };

            var removeEventListeners = function() {
                ctx.removeEventListener('ready', onReady);
                ctx.removeEventListener('error', onError);
            };

            var onReady = function() {
                removeEventListeners();
                onload(ctx);
            };

            var onError = function(error) {
                removeEventListeners();
                if (config.debug) { //XXX it is very quiet :(
                    alert(error);
                }
                onload.error(error);
            };
            addEventListeners();
        });

    };

    var helper = {
        extend: function() {
            for (var i = 1; i < arguments.length; i++)
                for (var key in arguments[i])
                    if (arguments[i].hasOwnProperty(key))
                        arguments[0][key] = arguments[i][key];
            return arguments[0];
        },
        getConfig: function(obj) {
            var config = {};
            var l20nCtx = defaults.name;

            if (obj.config) {
                config = obj.config[l20nCtx] || config;
            } else {
                config = obj[l20nCtx] || config;
            }

            return config;
        },
        getFullName: function(file_name, default_extension) {
            //it doesn't have an extesion
            if (file_name && !file_name.match(/\.[0-9a-z]+$/i)) {
                return [file_name, default_extension].join('.');
            } else {
                return file_name;
            }
        },
        parseName: function(file_name, config) {
            var to_replace = config.prefix + 'locale' + config.suffix;
            return file_name.replace(to_replace, config.locale);
        }
    };

    return l20nPlugin;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant