Skip to content

Commit

Permalink
Adding quote_style option. Fixes #293.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ændrew Rininsland committed Apr 5, 2015
1 parent ee40525 commit 9704819
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 6 deletions.
24 changes: 24 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,30 @@ module.exports = function(grunt) {
mangleProperties: true,
nameCache: 'tmp/uglify_name_cache.json'
}
},
quotes_single: {
files: {
'tmp/quotes_single.js': ['test/fixtures/src/quotes.js']
},
options: {
quoteStyle: 1
}
},
quotes_double: {
files: {
'tmp/quotes_double.js': ['test/fixtures/src/quotes.js']
},
options: {
quoteStyle: 2
}
},
quotes_original: {
files: {
'tmp/quotes_original.js': ['test/fixtures/src/quotes.js']
},
options: {
quoteStyle: 3
}
}
},

Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,18 @@ Type: `Boolean`
Default: false

Pass this flag if you don't care about full compliance with Internet Explorer 6-8 quirks.


#### quoteStyle
Type: `Integer`
Default: `0`

Preserve or enforce quotation mark style.
- `0` will use single or double quotes such as to minimize the number of bytes (prefers double quotes when both will do)
- `1` will always use single quotes
- `2` will always use double quotes
- `3` will preserve original quotation marks


### Usage examples

#### Basic compression
Expand Down
11 changes: 11 additions & 0 deletions docs/uglify-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,14 @@ A string that is a path to a JSON cache file that uglify will create and use to
multiple runs of uglify. Note: this generated file uses the same JSON format as the `exceptionsFiles` files.



## quoteStyle
Type: `Integer`
Default: `0`

Preserve or enforce quotation mark style.

- `0` will use single or double quotes such as to minimize the number of bytes (prefers double quotes when both will do)
- `1` will always use single quotes
- `2` will always use double quotes
- `3` will preserve original quotation marks
1 change: 1 addition & 0 deletions docs/uglify-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ Version `3.x` introduced changes to configuring source maps. Accordingly, if you
`sourceMapName` - Accepts a string or function to change the location or name of your map
`sourceMapIncludeSources` - Embed the content of your source files directly into the map
`expression` - Accepts a `Boolean` value. Parse a single expression (JSON or single functions)
`quoteStyle` - Accepts integers `0` (default), `1`, `2`, `3`. Enforce or preserve quotation mark style.
10 changes: 7 additions & 3 deletions tasks/lib/uglify.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,22 @@ exports.init = function(grunt) {
}
}

if (options.indentLevel !== undefined) {
if (!_.isUndefined(options.indentLevel)) {
outputOptions.indent_level = options.indentLevel;
}

if (options.maxLineLen !== undefined) {
if (!_.isUndefined(options.maxLineLen)) {
outputOptions.max_line_len = options.maxLineLen;
}

if (options.ASCIIOnly !== undefined) {
if (!_.isUndefined(options.ASCIIOnly)) {
outputOptions.ascii_only = options.ASCIIOnly;
}

if (!_.isUndefined(options.quoteStyle)) {
outputOptions.quote_style = options.quoteStyle;
}

return outputOptions;
};

Expand Down
3 changes: 2 additions & 1 deletion tasks/uglify.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ module.exports = function(grunt) {
expression: false,
maxLineLen: 32000,
ASCIIOnly: false,
screwIE8: false
screwIE8: false,
quoteStyle: 0
});

// Process banner.
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/expected/quotes_double.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/expected/quotes_original.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/expected/quotes_single.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test/fixtures/src/quotes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function llama() {
var a = 'This is a single quote string';
var b = "This is a double quote string";
var c = {
"this": "is part of an Object",
'and': 'so is this',
'but': 'this one has "mixed" quotes: \''
};

return [a, b, c];
}
5 changes: 4 additions & 1 deletion test/uglify_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ exports.contrib_uglify = {
'mangleprops_withExceptAndExceptionsFiles.js',
'mangleprops_withNameCacheFile1.js',
'mangleprops_withNameCacheFile2.js',
'uglify_name_cache.json'
'uglify_name_cache.json',
'quotes_single.js',
'quotes_double.js',
'quotes_original.js'
];

test.expect(files.length);
Expand Down

0 comments on commit 9704819

Please sign in to comment.