const EventEmitter = require('events');
const Primus = require('primus');
const uglify = require('uglify-js');

class PrimusWebpackPlugin {
  constructor(options) {
    this.options = Object.assign(
      {},
      {
        filename: 'primus-client.js',
        minify: false,
        primusOptions: {},
      },
      options,
    );
  }

  apply(compiler) {
    compiler.plugin('emit', (compilation, cb) => {
      const primus = new Primus(new EventEmitter(), this.options.primusOptions);
      const clientLib = primus.library();
      const filename = this.options.filename.replace(
        '[hash]',
        compilation.hash,
      );
      const source = this.options.minify
        ? uglify.minify(clientLib, { fromString: true })
        : { code: clientLib };

      compilation.assets[filename] = {
        source() {
          return source.code;
        },
        size() {
          return source.code.length;
        },
      };

      cb(null);
    });

    // if HtmlWebpackPlugin is being utilized, add our script to file
    compiler.plugin('compilation', compilation => {
      compilation.plugin(
        'html-webpack-plugin-before-html-processing',
        (htmlPluginData, cb) => {
          const filename = this.options.filename.replace(
            '[hash]',
            compilation.hash,
          );
          const scriptTag = `<script type="text/javascript" src="/${filename}"></script>`;

          if (
            !htmlPluginData.plugin.options.inject ||
            htmlPluginData.plugin.options.inject === 'head'
          ) {
            htmlPluginData.html = htmlPluginData.html.replace(
              '</head>',
              scriptTag + '</head>',
            );
          } else {
            htmlPluginData.html = htmlPluginData.html.replace(
              '</body>',
              scriptTag + '</body>',
            );
          }

          cb(null, htmlPluginData);
        },
      );
    });
  }
}

module.exports = PrimusWebpackPlugin;

@@ -296,18 +296,17 @@
 ions: %7B%7D
-,
 %0A
+
       %7D,
@@ -319,17 +319,16 @@
  options
-,
 %0A    );%0A
@@ -618,25 +618,24 @@
 ilation.hash
-,
 %0A      );%0A  
@@ -1305,17 +1305,16 @@
 ion.hash
-,
 %0A       
@@ -1668,33 +1668,32 @@
 tTag + '%3C/head%3E'
-,
 %0A            );%0A
@@ -1826,33 +1826,32 @@
 tTag + '%3C/body%3E'
-,
 %0A            );%0A
@@ -1899,16 +1899,16 @@
 nData);%0A
+
         
@@ -1904,25 +1904,24 @@
 );%0A        %7D
-,
 %0A      );%0A  

fix linting issue