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

jQuery dependency cause double require of module using webpack #1411

Closed
mrsombre opened this issue Jun 1, 2017 · 11 comments
Closed

jQuery dependency cause double require of module using webpack #1411

mrsombre opened this issue Jun 1, 2017 · 11 comments

Comments

@mrsombre
Copy link

mrsombre commented Jun 1, 2017

We are using webpack for building JS and bootstrap 3 (which has not support jquery 3 yet). In this case, tablesorter dependency install separate jquery 3 and using it in webpack, instead of main jquery 2 and we can not use tablesorter from global scope.

Consider change dependency to "jquery": "^2.4 | ^3.2" from "jquery": ">=3.2.1" to fix this issue.

@MartinMuzatko
Copy link

I also have trouble using this with webpack.

I have to manually assign $.fn.tablesorter = require('tablesorter'). But this does not work :/ @Mottie

@mrsombre
Copy link
Author

mrsombre commented Jun 1, 2017

@MartinMuzatko i downgraded to 2.28.8, because can't for 2.28.9 for some reason.
npm i tablesorter@2.28.8 or in packaje.json "tablesorter": "2.28.8"

@Mottie
Copy link
Owner

Mottie commented Jun 1, 2017

Hi @mrsombre!

Bootstrap 3.3.7 supports jQuery 3.0+ so a change to the dependency shouldn't be necessary. It looks like I forgot to publish v2.28.9 to npm. The current version is 2.28.12.

@MartinMuzatko

Did you use npm install tablesorter? Also you should be using const tablesorter = require('tablesorter'); and not assigning it to a jQuery function. It does that internally.

@MartinMuzatko
Copy link

Thank you for the feedback @Mottie.
When I include the script via script tag, there are no problems. Even though jquery is exposed via webpack.

{ test: require.resolve('jquery'), loader: 'expose?jQuery!expose?$'},

But when I import 'tablesorter', it is not added to the jquery scope ($.fn.tablesorter or $.tablesorter.

@Mottie
Copy link
Owner

Mottie commented Jun 1, 2017

I'm not sure what might be missing, but check out this StackOverflow answer: https://stackoverflow.com/a/28989476/145346

The way I found works is something like this:

module.exports = {
  entry: [
    'script!jquery/dist/jquery.min.js',
    'script!tablesorter/dist/js/jquery.tablesorter.min.js'
  ],
  externals: {
    jquery: 'jQuery'
  },
  plugins: [
    new webpack.ProvidePlugin({
      '$': 'jquery',
      'jQuery': 'jquery'
    })
  ],
  // ...
}

@mrsombre
Copy link
Author

mrsombre commented Jun 1, 2017

@Mottie we using jquery 2.4 for some reasons, i don't know, maybe our frontend guys knows, BTW what the problem with or in dependency. I'm explain how it works

Example webpack code

// jquery
window.jQuery = window.$ = require("jquery");
require("tablesorter");

Last version tablesorter with jquery 3 dependency
in my bundle.js

// jquery
window.jQuery = window.$ = __webpack_require__(0);
...
/* Includes widgets ( storage,uitheme,columns,filter,stickyHeaders,resizable,saveSort ) */
(function(factory) {
	if (true) {
		!(__WEBPACK_AMD_DEFINE_ARRAY__ = [__webpack_require__(3)]
...

Old version tablesorter with jquery >1 dependency

// jquery
window.jQuery = window.$ = __webpack_require__(0);
...
/* Includes widgets ( storage,uitheme,columns,filter,stickyHeaders,resizable,saveSort ) */
(function(factory) {
	if (true) {
		!(__WEBPACK_AMD_DEFINE_ARRAY__ = [__webpack_require__(0)]
...

Also, there are double jquery in my bundle.js and in this case tablesorted has not exposed to global scope, so i can't use it at site pages, only in my bundle.js

@Mottie
Copy link
Owner

Mottie commented Jun 1, 2017

@mrsombre so to solve this all I'd need to do is to set the jQuery dependency to "^2.4|^3.2"?

@mrsombre
Copy link
Author

mrsombre commented Jun 1, 2017

@Mottie i think so, would be great

@Herst
Copy link
Collaborator

Herst commented Jun 1, 2017

Never heard of jQuery 2.4, WP does not know it either: https://en.wikipedia.org/wiki/JQuery#Release_history

I guess you mean 2.2.4 (latest before 3).

@Mottie
Copy link
Owner

Mottie commented Jun 1, 2017

Actually, I'm going to change it back to ">=1.2.6" because that would allow pretty much any jQuery version.

@Mottie Mottie closed this as completed in cb420ef Jun 1, 2017
@Mottie
Copy link
Owner

Mottie commented Jun 1, 2017

This fix will be available with the next release, which will be likely be tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants