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

Integrating Response with Webpack2 #63

Open
gonzaloplaza opened this issue Sep 9, 2017 · 11 comments
Open

Integrating Response with Webpack2 #63

gonzaloplaza opened this issue Sep 9, 2017 · 11 comments

Comments

@gonzaloplaza
Copy link

gonzaloplaza commented Sep 9, 2017

Hi Ryan

I'm developing a project using this theme from themeforest (https://themeforest.net/item/kosmo-multi-purpose-responsive-bootstrap-4-admin-template-ui-framework/19506620) wich uses Response.js.

I'm using Webpack2 to generate a unique bundle and can't load Response.js -> I'm getting this error after generating bundle, in console:

Uncaught TypeError: Cannot read property 'jQuery' of undefined

This is the config i've used for webpack2:

new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: 'jquery',
      "window.jQuery": 'jquery',
      "window.$" : "jquery",
      jquery: 'jquery',
      Response: 'response.js',
      "window.Response": "response.js",
      Popper: ['popper.js', 'default'],
      Tether: 'tether',
      'window.Tether': 'tether'
    })

Looks like when Response is being loaded, it doesn't recognize jQuery , this is the exactly line that fails. What is root?

Note: i'm using jQuery 3.2.1

var $ = root['jQuery'] || root['Zepto'] || root['ender'] || root['elo'];

Do you have any idea if something not compatible or i'm doing something wrong?. Thank you in advance

@ryanve
Copy link
Owner

ryanve commented Sep 10, 2017

@gonzaloplaza Thanks for the detailed report! Hmm. I wonder...does it work as root?

"root.jQuery": "jquery",

Do you know which version of response.js the theme uses? Is there a way to upgrade the version? If so I imagine that I could adjust that line to make it work with your original way and then do a release.

@ryanve
Copy link
Owner

ryanve commented Sep 10, 2017

In that pattern root equals the this context from the outer function.

If webpack somehow forces it to run in strict mode then this === undefined

@davesunny1985
Copy link

Hi Ryan,

Any updates on this? because I think it is quite similar to the issue I am facing....

Thanks,
Sunny Dave

@kierzniak
Copy link

This webpack configuration works for me:

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: ["babel-polyfill", './src/js/app.js'],
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: 'js/',
    filename: 'app.bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: require.resolve('response.js'),
        use: 'imports-loader?this=>window'
      }
    ]
  },
  plugins: [
    new webpack.ProvidePlugin({
      'window.jQuery': 'jquery',
    })
  ]
}

Sample js file can looks like this:

import Response from 'response.js';

console.log(Response);

This is is working because response.js file is imported with window as this context and jQuery is available in window object. This is not recommended way to import libraries and response.js should be rewrited to be fully compatible with webpack.

@gonzaloplaza
Copy link
Author

That configuration worked for me too. Thank you very much! .

@ryanve
Copy link
Owner

ryanve commented Nov 4, 2017

@kierzniak Thx for posting that solution! Do you think that changing line 9 to do this || window would make it work as you'd expect?

ryanve added a commit that referenced this issue Nov 4, 2017
@ryanve ryanve mentioned this issue Nov 4, 2017
ryanve added a commit that referenced this issue Nov 4, 2017
@ryanve
Copy link
Owner

ryanve commented Nov 4, 2017

I just released v0.10.0 with this || window included. Is that helpful? I have some other ideas too.

@kbherbert
Copy link

This update (v0.10.0) did not work for me in Webpack 3. I had to revert to v0.9.1 in combination with @kierzniak 's suggestion above.

@ryanve
Copy link
Owner

ryanve commented Dec 31, 2017

Ok @kbherbert thanks for reporting. FYI #76 is loosely related regarding the root export name.

@adrexia
Copy link

adrexia commented Jul 24, 2019

@ryanve we've been having similar issues with a gulp pipeline (the issue just started, so I'm unsure of the cause). Your solution went partway to fixing it, but we also needed to change this line

, root = this
to , root = this || window

@ryanve
Copy link
Owner

ryanve commented Jul 24, 2019

Thanks @adrexia

Patched in v0.10.1

Available from npm like

npm install response.js@v0.10

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

6 participants