kkemple / primus-webpack-plugin

Build client side primus script and add to build assets 📦⚡
MIT License
7 stars 10 forks source link

Failed to send event #2

Open zhaoyi0113 opened 7 years ago

zhaoyi0113 commented 7 years ago

I setup primus-webpack-plugin in webpack configuration file as below.

new PrimusWebpackPlugin({
      filename: 'primus-client.js',
      minify: true,
      primusOptions: {
        transformer: 'websockets',
        timeout: false,
      }
    }),

In the js code, I import Primus and use it to send event to server as below:

import feathers from 'feathers-client';
import Primus from '../../../dist/primus-client';
const primus = new Primus(url);
const app =feathers().configure(feathers.hooks()).configure(feathers.primus(primus)); 
app.service('my service').create('...')

after doing this, I got below exception from the last line of above code:

connection failed  TypeError: Cannot read property 'apply' of undefined
    at client.js:104
    at Object.send (client.js:82)
    at Object.create (client.js:126)
    at hooks.js:101
    at hooks.js:85

It works fine if I serve the primus client from a static server. I only got this error when I use this webpack plugin.

kkemple commented 7 years ago

The UMD output for primus client doesn't work well with client-side bundling like browersify and webpack, the purpose of this module is to get the client script built and added to assets to include in your HTML doc you send to client. Primus is then available globally as window.Primus. If you want to require/import primus you will have to alias the global Primus as primus in your build config

zhaoyi0113 commented 7 years ago

Is there an example for that? I tried below method but doesn't work.

new webpack.ProvidePlugin({
       Primus: 'primus-client',
    }),
kkemple commented 7 years ago

once you add the provide plugin, how do you require primus in your app?

zhaoyi0113 commented 7 years ago

I just do import Primus from 'primes-client'. Is it correct way to do?