Search code examples
javascriptgulpuglifyjsgulp-uglify

How to prevent Uglify un-escaping "</script" in strings?


I've got code like this:

var string = '<script src="' + src + '">\x3c/script>';

I've used \x3c instead of < to avoid having a closing script tag (which would end the script early if used as an inline script in a web browser, breaking everything).

But uglify unfortunately converts it back into a < character, breaking my page.

It looks like Uglify has an option called inline-scripts intended to fix this, but there are no docs on using this option with the API (I'm using it via gulp-uglify so I need to be able to pass this option in an options object, not via the CLI).

How do I do it? None of the following work:

  • {'inline-script': true}
  • {inlineScript: true}
  • {beautify: {inlineScript: true}}
  • {beautify: {'inline-script': true}}

Solution

  • I realize this is an old question, however, the answer given is not correct. You need to use the output object in uglifyjs.

    .pipe(uglify({
        output: {
            'inline_script': true
        }
    }))