Skip to content

Commit

Permalink
feat: add iesafe option
Browse files Browse the repository at this point in the history
Fixes #134
  • Loading branch information
john-ericson-tr authored and remy committed Apr 5, 2017
1 parent ef5433c commit 83edb2b
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function options(args) {
'videos',
'inlinemin',
'preserve-comments',
'iesafe',
],
string: [ // options
'encoding',
Expand Down
1 change: 1 addition & 0 deletions docs/usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
-n, --nocompress don't compress CSS or HTML - useful for debugging
-i, --noimages don't encode images - keeps files size small, but more requests
-m, --inlinemin inline minified files
--iesafe safe JS compression for older IE 6/7/8
--videos encode videos (and their poster image) - disabled by default
--nosvg don't compress SVG (through SVGO)
--skip-absolute-urls don't inline links with absolute URLs
Expand Down
1 change: 1 addition & 0 deletions lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ module.exports = function () {
nosvg: false, // by default, DO compress SVG with SVGO
skipAbsoluteUrls: false,
preserveComments: false,
iesafe: false,
};
};
9 changes: 9 additions & 0 deletions lib/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var debug = require('debug')('inliner');
var UglifyJS = require('uglify-js');

function uglify(source) {
var notIESafe = !this.options.iesafe;

this.emit('progress', 'compressing javascript');

source = source.trim();
Expand All @@ -20,6 +22,13 @@ function uglify(source) {
try {
result = UglifyJS.minify(source, {
fromString: true,
// must set screw_ie8 for each option group
// https://github.com/mishoo/UglifyJS2/issues/1204#issuecomment-234714094
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
compress: { screw_ie8: notIESafe },
mangle: { screw_ie8: notIESafe },
output: { screw_ie8: notIESafe },
// jscs:enable requireCamelCaseOrUpperCaseIdentifiers
}).code;
} catch (e) {
// failed to uglify, just return it plain
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/script-ie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function doit() {
var foo = {default:'bar'};
// default is a reserved word which breaks parsing in IE<=8
return foo.default;
}
3 changes: 3 additions & 0 deletions test/fixtures/script-iesafe.opts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"iesafe": true
}
1 change: 1 addition & 0 deletions test/fixtures/script-iesafe.result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>iesafe</title> </head> <body> <script>function doit(){return{"default":"bar"}["default"]}</script> </body> </html>
10 changes: 10 additions & 0 deletions test/fixtures/script-iesafe.src.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>iesafe</title>
</head>
<body>
<script src="script-ie.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions test/fixtures/script-ieunsafe.result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>ieunsafe</title> </head> <body> <script>function doit(){return{default:"bar"}.default}</script> </body> </html>
10 changes: 10 additions & 0 deletions test/fixtures/script-ieunsafe.src.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ieunsafe</title>
</head>
<body>
<script src="script-ie.js"></script>
</body>
</html>

0 comments on commit 83edb2b

Please sign in to comment.