feat: add iesafe option

Fixes #134
This commit is contained in:
John Ericson 2017-02-25 22:57:31 -05:00 committed by Remy Sharp
parent ef5433ccc7
commit 83edb2bf54
10 changed files with 42 additions and 0 deletions

View File

@ -16,6 +16,7 @@ function options(args) {
'videos',
'inlinemin',
'preserve-comments',
'iesafe',
],
string: [ // options
'encoding',

View File

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

View File

@ -7,5 +7,6 @@ module.exports = function () {
nosvg: false, // by default, DO compress SVG with SVGO
skipAbsoluteUrls: false,
preserveComments: false,
iesafe: false,
};
};

View File

@ -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();
@ -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

5
test/fixtures/script-ie.js vendored Normal file
View File

@ -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
test/fixtures/script-iesafe.opts.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"iesafe": true
}

View File

@ -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
test/fixtures/script-iesafe.src.html vendored Normal file
View File

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

View File

@ -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
test/fixtures/script-ieunsafe.src.html vendored Normal file
View File

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