Giter Site home page Giter Site logo

dozoisch / primus-webpack-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kkemple/primus-webpack-plugin

0.0 0.0 0.0 5 KB

Build client side primus script and add to build assets ๐Ÿ“ฆโšก

License: MIT License

JavaScript 100.00%

primus-webpack-plugin's Introduction

PrimusWebpackPlugin

Build client side Primus script and add to build assets ๐Ÿ“ฆโšก

Introduction

Primus is a socket wrapper implementation that requires a built client library. The client library also needs to match the configuration of the server side implementation. Primus handles this by exposing primus.library() which returns the client library as a string.

This is problematic in applications that bundle and dynamically generate client assets. It is also problematic because unless you want to serve the client script directly from your websocket server (not a great approach IMO) you need to prebuild and package the client script anyway.

This plugin allows you to pass in your Primus options and then adds the client library to your Webpack build assets.

If HtmlWebpackPlugin is being used it will also add the asset to the output HTML ๐ŸŽ‰

Usage

Install PrimusWebpackPlugin:

npm install --save-dev primus-webpack-plugin

With Yarn:

yarn add -D primus-webpack-plugin

In webpack.config.js:

const PrimusWebpackPlugin = require('primus-webpack-plugin')

...

new PrimusWebpackPlugin({
  filename: 'primus-client.[hash].js',
  minify: true,
  primusOptions: {
    transformer: 'uws',
    parser: {
      encoder: function (data, fn) {
        fn(null, JSON.stringify(data))
      },
      decoder: function (data, fn) {
        fn(null, JSON.parse(data))
      }
    }
  }
})

Options:

Name Description Default
filename Name of generated file primus-client.js
minify Whether or not to minify the file false
primusOptions Options for the Primus Server {}

https://github.com/primus/primus#getting-started

Caveats

primus.library() generates a UMDish style file but it doesn't seem to work being bundled with Webpack, instead a global Primus constructor is added. If you want to require/import Primus you will need to shim it in your Webpack config.

primus-webpack-plugin's People

Contributors

kkemple avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.