UglifyJS icon indicating copy to clipboard operation
UglifyJS copied to clipboard

Single quotes being replaced with double quotes

Open haknick opened this issue 12 years ago • 25 comments

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)

haknick avatar Jun 18 '14 04:06 haknick

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.

avdg avatar Jun 28 '14 03:06 avdg

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

singh1469 avatar Sep 11 '14 10:09 singh1469

@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.

michaelficarra avatar Sep 11 '14 16:09 michaelficarra

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

rvanvelzen avatar Jan 04 '15 10:01 rvanvelzen

Closing until clarification is given.

rvanvelzen avatar Jan 13 '15 14:01 rvanvelzen

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.

haknick avatar Jan 14 '15 08:01 haknick

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.

rvanvelzen avatar Jan 14 '15 12:01 rvanvelzen

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 ?

haknick avatar Jan 14 '15 23:01 haknick

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?

rvanvelzen avatar Jan 15 '15 08:01 rvanvelzen

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

haknick avatar Jan 20 '15 00:01 haknick

@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 avatar Jan 20 '15 10:01 mishoo

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

haknick avatar Jan 21 '15 00:01 haknick

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

rvanvelzen avatar Jan 21 '15 08:01 rvanvelzen

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 avatar Feb 16 '15 23:02 jdalton

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

rvanvelzen avatar Feb 17 '15 09:02 rvanvelzen

@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.

jdalton avatar Feb 18 '15 00:02 jdalton

@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.)

shamasis avatar Sep 26 '15 16:09 shamasis

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"

ghost avatar Feb 12 '16 21:02 ghost

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

kzc avatar Feb 12 '16 21:02 kzc

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

ghost avatar Feb 12 '16 22:02 ghost