• Resolved shaolinshaosson

    (@shaolinshaosson)


    Anyone else has problems if js-files included in theme/plugins is written without using semicolons fully?

    Like with this code for example:

    (function(factory) {
    
      if (typeof exports == 'object') {
        // CommonJS
        factory(require('jquery'), require('spin'))
      }
      else if (typeof define == 'function' && define.amd) {
        // AMD, register as anonymous module
        define(['jquery', 'spin'], factory)
      }
      else {
        // Browser globals
        if (!window.Spinner) throw new Error('Spin.js not present')
        factory(window.jQuery, window.Spinner)
      }
    
    }(function($, Spinner) {
    
      $.fn.spin = function(opts, color) {
    
        return this.each(function() {
          var $this = $(this),
            data = $this.data();
    
          if (data.spinner) {
            data.spinner.stop();
            delete data.spinner;
          }
          if (opts !== false) {
            opts = $.extend(
              { color: color || $this.css('color') },
              $.fn.spin.presets[opts] || opts
            )
            data.spinner = new Spinner(opts).spin(this)
          }
        })
      }
    
      $.fn.spin.presets = {
        tiny: { lines: 8, length: 2, width: 2, radius: 3 },
        small: { lines: 8, length: 4, width: 3, radius: 5 },
        large: { lines: 10, length: 8, width: 4, radius: 8 }
      }
    
    }));

    Since it minifies:

    
    if (!window.Spinner) throw new Error('Spin.js not present')
        factory(window.jQuery, window.Spinner)
    

    to:

    
    if (!window.Spinner) throw new Error('Spin.js not present') factory(window.jQuery, window.Spinner)
    

    I get ‘SyntaxError: missing ; before statement’ that makes this plugin unusable.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Philip K Meadows

    (@philmeadows)

    There’s three options to get around this:

    1. Terminate your javascript command lines correctly with semicolons;
    2. Use a pre-minified version of the javascript, if one is available
    3. Or, rename the javascript file to contain the string min in the file name – WP Roids should then ignore minifying it

    Phil

    Plugin Author Philip K Meadows

    (@philmeadows)

    Also, wrap statements in curly braces {}

    Your code above should be:

    if(!window.Spinner) {
        throw new Error('Spin.js not present');
        factory(window.jQuery,window.Spinner);
    }
    Thread Starter shaolinshaosson

    (@shaolinshaosson)

    The problem is that the lack of semicolons exist in a lot of different plugins resources, so I would need to dig deep in all those js files and find out which ones are written that way, and either unload the included file and replace with pre-minified version, or unload the file and add “.min” tot he filename in my own version = pain in the ass when updating plugins.
    I was hoping that there would be a better way since others must also have this problem, I know it’s the plugins creators that should have used a pre-min version of its resources or take into account that people might want to minfy/combine all files.
    Would be nice if there would be a feature to setup what files should be minified or ignored for your plugin.

    Plugin Author Philip K Meadows

    (@philmeadows)

    That’s not a bad idea

    I’ll work on an option(s) to disable minification on a per plugin basis

    Would be handy for debugging!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘JS error with code written without ;’ is closed to new replies.