Search code examples
javascriptjsonquotesdouble-quotesuglifyjs

Don't escape double quotes


as I found out uglifyJS ignores an escaped double quotes in a single quote string.

'test \" + foo + \"'

results in

'test " + foo + "'

This is just a wrong behaviour.

I have a .js that I want to minify and this data have to go into a JSON. But the JSON API have to be like this.

{
    string: "test \" + foo + \""
}

not

{
    string: "test " + foo + ""
}

Is there a way to configure uglifyJS that it don't ignore \"

At the moment I uglify my javascript and replace all my \" with placeholders. After this, I do a string replace with all placeholders to get it working. But then my tests don't work.

EDIT: The uglify version of my Javascript has to be valid JSON and valid Javascript as well.

EDIT: As requested, a part of the real example. This code has to be uglified and then put into a JSON.

var privacylink = '#privacyButtonURL#';
link = '<a href=\"' + privacylink + '\" target=\"_blank\" style=\"color:#4398b5; text-decoration:underline;\">hear</a>';

The uglify version would be

var a="#privacyButtonURL#", b='<a href="'+a+'" target="_blank" style="color:#4398b5; text-decoration:underline;">hear</a>'

This would not work.

JSON.stringify('var a="#privacyButtonURL#", b='<a href="'+a+'" target="_blank" style="color:#4398b5; text-decoration:underline;">hear</a>'')

Solution

  • There's a github issue about quote format.

    The solution given in the link above is to tell uglify to keep the original quote format:

    $ echo "console.log(\"foo\",'bar');" | uglifyjs --quotes=1
    console.log('foo','bar');
    
    $ echo "console.log(\"foo\",'bar');" | uglifyjs --quotes=2
    console.log("foo","bar");
    
    $ echo "console.log(\"foo\",'bar');" | uglifyjs --quotes=3
    console.log("foo",'bar');
    

    Or with gulp, give the following as parameter to the uglify.minify() function:

    { output: { quote_style: 3 } }
    

    For your specific problem, as I'm not sure it will keep the unnecessary escape characters (in javascript), a solution would be:

    • remove all single quotes from the javascript by switching everything to double quotes and escape them where necessary
    • uglify the code with the above option
    • do {string: JSON.stringify(code)}

    If your javascript code has to have single quotes for some reason, instead you can replace the double quotes in the generated html in the javascript code by &#34; or &quot;.

    Note that I don't feel that something like var a = 'abc \" def'; is valid javascript in the first place.

    Another thing to look into would be how you include file (it's not mentioned), maybe there's a better way that directly loads the file into a string, on which you can then call JSON.stringify().

    Edit

    If you use a recent javascript engine, you can also use backquotes (`) in your code and replace double or single quotes by them.

    Or, if there's no $ nor backquotes in your javascript code, you can simply do:

    {
        string: JSON.stringify(`uglified javascript code`)
    }