response.js icon indicating copy to clipboard operation
response.js copied to clipboard

Integrating Response with Webpack2

Open gonzaloplaza opened this issue 8 years ago • 11 comments

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

gonzaloplaza avatar Sep 09 '17 14:09 gonzaloplaza

@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 avatar Sep 10 '17 19:09 ryanve

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

ryanve avatar Sep 10 '17 19:09 ryanve

Hi Ryan,

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

Thanks, Sunny Dave

davesunny1985 avatar Sep 19 '17 04:09 davesunny1985

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.

kierzniak avatar Oct 23 '17 09:10 kierzniak

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

gonzaloplaza avatar Oct 30 '17 19:10 gonzaloplaza

@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 avatar Nov 04 '17 21:11 ryanve

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

ryanve avatar Nov 04 '17 22:11 ryanve

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.

kbherbert avatar Dec 20 '17 19:12 kbherbert

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

ryanve avatar Dec 31 '17 21:12 ryanve

@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 https://github.com/ryanve/response.js/blob/701e98d157b8f4cfca70984947750e88f6e6f952/response.js#L19 to , root = this || window

adrexia avatar Jul 24 '19 02:07 adrexia

Thanks @adrexia

Patched in v0.10.1

Available from npm like

npm install [email protected]

ryanve avatar Jul 24 '19 04:07 ryanve