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

Parsley is undefined when loading from minified file? #930

Closed
Download opened this issue Jun 22, 2015 · 6 comments
Closed

Parsley is undefined when loading from minified file? #930

Download opened this issue Jun 22, 2015 · 6 comments

Comments

@Download
Copy link
Contributor

I'm pretty new to AMD / require.js as well as to Parsley, so excuse me if this turns out to be a user error...

I'm loading Parsley through AMD using require.js, but for some reason I'm getting undefined as the Parsley object.

Here is the strange thing: If I load the source file of Parsley (I have to add some paths so it can find it's own sub modules) it does work!

I created a JS Fiddle that demonstrates my issue, but for some reason I could only get this to work using require. In my actual code I'm using define

Here is my code using the minified script:

require.config({
    paths: {
        'jquery': '//code.jquery.com/jquery-2.1.4.min',
        'parsley': '//cdn.rawgit.com/download/Parsley.js/2.1.2/dist/parsley.min',
    }
});
define(['parsley'], function(Parsley){
    alert('Parsley is ' + Parsley);   // ---> alerts: 'Parsley is undefined'
});

If I use the source version of Parsley, it does work:

require.config({
    paths: {
        'jquery': '//code.jquery.com/jquery-2.1.4.min',
        'parsley': '//cdn.rawgit.com/download/Parsley.js/2.1.2/src/parsley',
        'i18n': '//cdn.rawgit.com/download/Parsley.js/2.1.2/src/i18n',
        'validator': '//cdn.rawgit.com/guillaumepotier/validator.js/1.1.2/dist/validator.min'
    }
});

define(['jquery', 'parsley'], function($, Parsley){
    alert('Parsley is ' + Parsley);   // --->  alerts 'Parsley is [Object object]'
});

If I leave out the dependency on jQuery, like so:

define(['parsley'], function(Parsley){
    alert('Parsley is ' + Parsley);
});

I get two errors:

  • Uncaught ReferenceError: jQuery is not defined (anonymous function) @ en.js:6
  • Uncaught ReferenceError: $ is not defined (anonymous function) @ pubsub.js:7

I think this is a bug. en.js and pubsub.js should define themselves and declare the dependency on jQuery...

If I add the explicit dependency on jQuery to the define using the minified Parsley, it doesn't actually help. Neither does adding the paths for i18n and validator. Am I doing something wrong?

@Download
Copy link
Contributor Author

(I know see that the rawgit URLs are pointing to my fork but I didn't actually change anything and if I make them point to this repo there is no difference)

@Download
Copy link
Contributor Author

I also see that window.Parsley is defined. It's just the AMD function argument which is undefined...

@guillaumepotier
Copy link
Owner

Hi @Download,

Parsley minified version removes all AMD definitions and other stuff. AMD is only used in dev in src/ and dist/ versions currently does not support AMD loading.
While Parsley is always added to window, fully supporting AMD with dist files seemed not so necessary.

At @Wisembly we require parsley min file with AMD with requirejs shim and it does the job.

WDYT?

@Download
Copy link
Contributor Author

Hi @guillaumepotier , thanks for looking into this.

I searched for AMD support for Parsley and found this issue, so I thought support had been added:
Have parsley register as an AMD module #606

Also, I see that at least some support for AMD is still in the minified file. It starts with:
!function(a){"function"==typeof define&&define.amd?define(["jquery"],a)

To make Parsley behave as expected (and have the Parsley argument to the define callback be filled in) would just require a single return window.Parsley at the end so I can't see why you wouldn't support that.

I can make a PR if you think it would stand a chance of being accepted?

@Download
Copy link
Contributor Author

I tried it out and adding return window.Parsley; to src/wrap/append.js fixes this issue in my testing. So I just went ahead and made a PR of it.

@guillaumepotier
Copy link
Owner

Indeed. Thank you. Best

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

2 participants