Uglifyjs: Single quotes being replaced with double quotes

Created on 18 Jun 2014  路  25Comments  路  Source: mishoo/UglifyJS

Doing this:

var parseObj = uglify.minify(file_content,
{fromString: true, warnings:true, mangle:false,
compress: {unused:false}});
return parseObj.code

And the javascript string is returned as expected except the single quotes. They're replaced by double quotes. Is this by design or an issue ? Are there any options I'm missing (went through the docs a few times, but couldn't find anything)

Most helpful comment

Read the documentation.

https://github.com/mishoo/UglifyJS2

  • quote_style (default 0) -- preferred quote style for strings (affects quoted property names and directives as well):

    • 0 -- prefers double quotes, switches to single quotes when there are more double quotes in the string itself.

    • 1 -- always use single quotes

    • 2 -- always use double quotes

    • 3 -- always use the original quotes

$ 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');

All 25 comments

I guess its because UglifyJS only cares about strings being a string, not how the string is being formatted. I am not sure if UglifyJS actually tracks the way strings are written, but it probably only cares about the content, not the format.

This issue is causing me problems, will it be resolved via pull request from @btd ?

@singh1469: I would _love_ to hear what you're doing that can possibly be causing an issue. Let me just warn you first that whatever it is, it is a bad idea.

This is expected behaviour. Could you clarify what the actual problem is?

Closing until clarification is given.

The problem was when minifying show functions for CouchDB. The functions would be assigned as strings to JSON properties. By changing single quotes to double quotes the JSON parsing inside Couch would break. Had a workaround in place a bit after posting this issue, which was not as clean as if this did not happen.
Thanks for the effort as well.

UglifyJS does not compress code for use as JSON. I also don't see how that is directly useful, since most code _isn't_ valid JSON to begin with.

You can compress most JSON by simply parsing and re-stringifying it. If you're including functions you're not actually working with JSON and changing quote style should not matter.

The 'show' functions get stringified and then get assigned to JSON properties (as a string). It is, valid JSON. The obvious thing to do here is to escape the double quotes so that they're a valid JSON string. But since there were only single quotes inside the 'show' functions the question is; Why the need to convert single quotes to double quotes in UglifyJS ?

The 'show' functions get stringified and then get assigned to JSON properties (as a string).

Am I misunderstanding? Are you talking about JSON (the data format) or JavaScript objects?

JSON the data format. As shown here: http://guide.couchdb.org/draft/show.html#design

@rvanvelzen I think he is doing something like this:

var json = '{"code":"' + minified_code + '"}';

@haknick if you doing the above, then UglifyJS actually saved you from some pain you'd get later on (although, technically, you deserve it.)

@mishoo No, It's assigned like so:
var json = {code: minified_code}

@haknick I don't see a problem there. However, track #514 for progress.

Users are reporting issues with lodash because '"' is getting turned into """. This code is in Underscore too. Would defaulting to 3 for quote_style cause significantly larger fiiles?

@jdalton I'm unable to reproduce that particular issue.

@rvanvelzen

I'm unable to reproduce that particular issue.

Cool. We haven't had an issue in our official minified builds either (uses a hybrid of the Closure Compiler and Uglify). We've only received reports of issues from a few minifying lodash on their own with Uglify.

@haknick - I am facing same issue too. I needed a minified code (which would never contain double quote) to be transported as JSON.

@mishoo - I don't understand your statement "you deserve it". I thought uglify was to help minify the code and not impose any coding standards! :-)

However, is there a plan to make uglify stick to original string quotes? It seems uglify does cool string escape optimisation and decides the quote style (default is double; if \" is found then single and if \' is found then double.)

I am having an issue with this as well. For example, in my Angular app, templates, are surround by single quotes, eg.

templateUrl: '/path/to/some/template.html'

I have a post build step where i want to prefix these paths for uploading to a CDN. Because of the single quotes being turned into double quotes, now my minified source looks like

templateUrl:'//xxxx.cloudfront.net/"path/to/some/template.html"

Read the documentation.

https://github.com/mishoo/UglifyJS2

  • quote_style (default 0) -- preferred quote style for strings (affects quoted property names and directives as well):

    • 0 -- prefers double quotes, switches to single quotes when there are more double quotes in the string itself.

    • 1 -- always use single quotes

    • 2 -- always use double quotes

    • 3 -- always use the original quotes

$ 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');

Thanks! Although my bad, I just realized this is not for the gulp plugin. Apologies on that mixup.

@kzc - thanks.

@obuckley-kenzan gulp uglify apparently uses uglify.minify() which can use the output option:

{ output: { quote_style: 3 } }

great, thanks @kzc . Will give that a try!

that worked, thanks so much!

It seems like the only way to specify quote_style on the command-line is to pass it through the --beautify [options] command, but disabling beautification immediately, so that you don't get a beautified document, and instead just affect the quote-style.

This command worked for me and successfully turned all quotes from double to single, without affecting anything else):

cat videotoolbar.js | uglifyjs --compress --mangle --beautify beautify=false,quote_style=3

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JoeUX picture JoeUX  路  3Comments

Havunen picture Havunen  路  5Comments

buu700 picture buu700  路  5Comments

kzc picture kzc  路  5Comments

lhtdesignde picture lhtdesignde  路  3Comments