mathiasbynens / punycode.js

A robust Punycode converter that fully complies to RFC 3492 and RFC 5891.
https://mths.be/punycode
MIT License
1.6k stars 159 forks source link

RequireJS & punicode in browser leads to error #18

Closed compojoom closed 10 years ago

compojoom commented 10 years ago

Hey guys, I've setup a small example here: http://fiddle.jshell.net/bDpK3/show/

just look at the output of the console: Error: Mismatched anonymous define() module: function () { return punycode; } http://requirejs.org/docs/errors.html#mismatch

something is wrong with the code that deals with the loaders

pollen8 commented 10 years ago

I believe its because require.js needs a module name defined, so around line 492 where currently there is:

define(function() { return punycode; });

if you replace that with

define('punycode', function() { return punycode; });

The error goes away. However I am not sure of the wider consequences of this, seems to me that the current punycode is assuming loading via commonjs?

pollen8 commented 10 years ago

guess you are using Joomla right ;) reading the readme https://github.com/bestiejs/punycode.js/blob/master/README.md, Punycode suggest loading via requirejs, but I'm stuck with the fact that Joomla is just injecting the script into the doc head. Im going to investigate if I can get round this one, will post back if I find anything

compojoom commented 10 years ago

Yes, using joomla :) I already had tried the define('punicode', function.... The thing is - if you load the page without reuqire.js and you type punycode in the console you get an object. If we modify the define and try to use it with requirejs -> then punycode in the console doesn't give us anything...

pollen8 commented 10 years ago

if you load with requirejs then you need to load with the following:

requirejs.config({
    paths: {
        punycode : 'media/system/js/punycode'
    }
});
requirejs(['punycode'], function (punycode) {
console.log(punycode);

window.punycode = punycode;
})

currently im at the stage where that's working IF you comment out libraries/cms/html/behavior.php line 129

JHtml::_('script', 'system/punycode.js', false, true);
pollen8 commented 10 years ago

i ended up replacing my calls to JHtml::_('behavior.formvalidation'); with a call to a custom method in my own html helper class:

    public static function formvalidation()
    {
        // Only load once
        if (isset(static::$loaded[__METHOD__]))
        {
            return;
        }

        // Add validate.js language strings
        JText::script('JLIB_FORM_FIELD_INVALID');

        // Include MooTools More framework
        static::framework('more');

        $document = JFactory::getDocument();
        $debug = JFactory::getConfig()->get('debug');
        $file = $debug ? 'punycode-uncompressed' : 'punycode';
        $path = JURI::root(). 'media/system/js/' . $file;

        $js = array();
        $js[] = "requirejs({";
        $js[] = "   'paths': {";
        $js[] = "     'punycode': '" . $path . "'";
        $js[] = "   }";
        $js[] = " },";
        $js[] = "['punycode'], function (p) {";
        $js[] = "  window.punycode = p;";
        $js[] = "});";

        $document->addScriptDeclaration(implode("\n", $js));
        JHtml::_('script', 'system/validate.js', false, true);
        static::$loaded[__METHOD__] = true;
    }