yui / yuglify Goto Github PK
View Code? Open in Web Editor NEWUglifyJS Wrapper with YUI default configs
License: Other
UglifyJS Wrapper with YUI default configs
License: Other
I'm the author of an asset pipeline for PHP (http://github.com/CHH/pipe). For tool support it would be nice if yuglify
would support piping JS/CSS into STDIN
, and then outputting the minified code to STDOUT
— just like the underlying uglifyjs
.
An asset pipeline, has a filter pipeline where the output from the last filter gets put into the next filter. When using yuglifyjs
in it's current state in a such filter pipeline, the tool needs to write the input data to a temporary file, and read the output back from another temporary file, which is cumbersome to use.
Switching between CSS and JS compression could be done via a flag, for example --css
and --js
.
Example:
% echo "foo" | yuglify --js
% echo "foo" | yuglify --css
I have a issue with split_lines
. After splitting (and before inserting comments) code became:
code,code,"yUglify: preserved comment block"
,code,code
I.e. code wraps right after comment. After than replacing comments replaces "preserved comment block" without comma. As a result code become:
code,code,
/* comment
*/
,code,code
I.e. two commas are following each other (with comment between). This causes the error.
You should swap split_lines
and code.replace(reTokens, …)
.
Given this Makefile w/ minify target:
JS = $(shell find . -type f ( -iname ".js" ! -name ".min.js" ) )
MINIFY = $(JS:.js=.min.js)
all: clean $(MINIFY)
clean:
rm -f $(MINIFY)
%.min.js: %.js
yuglify
.PHONY: clean minify
Execution against this file:
function encodeHTMLSource() {
var encodeHTMLRules = { "&": "&", "<": "<", ">": ">", '"': '"', "'": ''', "/": '/' };
var matchHTML = /&(?!#?\w+;)|<|>|"|'|//g;
return function() {
return this ? this.replace(matchHTML, function(m) {return encodeHTMLRules[m] || m;}) : this;
};
}
Results in:
function encodeHTMLSourSuccessfully generated JS file: �[32mtest.min.js�[0m
"","'":"'","/":"/"},t=/&(?!#?\w+;)|<|>|"|'|//g;return function(){return this?this.replace(t,function(t){return e[t]||t}):this}};
CentOS 6
Node 10.4
Yuglify 0.1.4 (installed via npm -g install yuglify)
We could have an option to customize the output file name. Likewise we have on uglify-js
:
yuglify ./main.js -o ./my.custom.main.js
We already have -o
being an alias of --output
. What happens is that if you run this, the program will properly log the path and the custom file name, but still generates the minified file by following the pattern [file name + -min + file extension].
Shifter expects only .js lang files. however, the content type of the lang files is json.
Proposition: Try to find .json if .js lang file has been not found.
Encountered following during ./maange.py colllectstatic
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf6 in position 207: invalid start byte
My Environment is:
Django: 1.11
Python: 3.6
The file encoding is:
$ file -I assets/js/cover.js
assets/js/cover.js: text/plain; charset=us-ascii
Detail traceback:
Post-processed 'js/cover.?.js' as 'js/cover.?.js'
Traceback (most recent call last):
File "/...path..../py3venv/lib/python3.6/site-packages/django/utils/encoding.py", line 74, in force_text
s = six.text_type(s, encoding, errors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf6 in position 207: invalid start byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/...path..../py3venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/...path..../py3venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/...path..../py3venv/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/...path..../py3venv/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/...path..../py3venv/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 199, in handle
collected = self.collect()
File "/...path..../py3venv/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 139, in collect
for original_path, processed_path, processed in processor:
File "/...path..../py3venv/lib/python3.6/site-packages/pipeline/storage.py", line 33, in post_process
packager.pack_javascripts(package)
File "/...path..../py3venv/lib/python3.6/site-packages/pipeline/packager.py", line 124, in pack_javascripts
return self.pack(package, self.compressor.compress_js, js_compressed, templates=package.templates, **kwargs)
File "/...path..../py3venv/lib/python3.6/site-packages/pipeline/packager.py", line 118, in pack
content = compress(paths, **kwargs)
File "/...path..../py3venv/lib/python3.6/site-packages/pipeline/compressors/__init__.py", line 60, in compress_js
js = self.concatenate(paths)
File "/...path..../py3venv/lib/python3.6/site-packages/pipeline/compressors/__init__.py", line 151, in concatenate
return "\n;".join([self.read_text(path) for path in paths])
File "/...path..../py3venv/lib/python3.6/site-packages/pipeline/compressors/__init__.py", line 151, in <listcomp>
return "\n;".join([self.read_text(path) for path in paths])
File "/...path..../py3venv/lib/python3.6/site-packages/pipeline/compressors/__init__.py", line 223, in read_text
return force_text(content)
File "/...path..../py3venv/lib/python3.6/site-packages/django/utils/encoding.py", line 88, in force_text
raise DjangoUnicodeDecodeError(s, *e.args)
django.utils.encoding.DjangoUnicodeDecodeError: 'utf-8' codec can't decode byte 0xf6 in position 207: invalid start byte. You passed in b'/*\n * String prototype extensions. Doesn\'t depend on any\n * other code. Doens\'t overwrite existing methods.\n *\n * Adds trim, camelize, startsWith, endsWith, truncate and stripTags.\n *\n * Copyright (c) 2006 J\xf6rn Zaefferer\n *\n * Dual licensed under the MIT and GPL licenses:\n * http://www.opensource.org/licenses/mit-license.php\n * http://www.gnu.org/licenses/gpl.html\n *\n */\n\n(function() {\n\n function add(name, method) {\n if( !String.prototype[name] ) {\n String.prototype[name] = method;\n }\n }\n\n /**\n * Returns a string with with leading and trailing whitespace removed.\n *\n * @example " Hello Boys and Girls! ".trim()\n * @result "Hello Boys and Girls!"\n *\n * @name trim\n * @type String\n * @cat Javascript/String\n */\n add("trim", function(){\n return this.replace(/(^\\s+|\\s+$)/g, "");\n });\n\n /**\n * Return a camelized String, removing all underscores and dashes\n * and replacing the next character with it\'s uppercase representation.\n *\n * @example "font-weight".camelize()\n * @result "fontWeight"\n *\n * @example "border_width_bottom".camelize()\n * @result "borderWidthBottom"\n *\n * @example "border_width-bottom".camelize()\n * @result "borderWidthBottom"\n *\n * @name camelize\n * @type String\n * @cat Javascript/String\n */\n add("camelize", function() {\n return this.replace( /[-_]([a-z])/ig, function(z,b){ return b.toUpperCase();} );\n });\n\n /**\n * Tests if this string starts with a prefix.\n *\n * An optional offset specifies where to start searching,\n * default is 0 (start of the string).\n *\n * Returns false if the offset is negative or greater then the length\n * of this string.\n *\n * @example "goldvein".startsWith("go")\n * @result true\n *\n * @example "goldvein".startsWith("god")\n * @result false\n *\n * @example "goldvein".startsWith("ld", 2)\n * @result true\n *\n * @example "goldvein".startsWith("old", 2)\n * @result false\n *\n * @name startsWith\n * @type Boolean\n * @param prefix The prefix to test\n * @param offset (optional) From where to start testing\n * @cat Javascript/String\n */\n\n add("startsWith", function(prefix, offset) {\n var offset = offset || 0;\n if(offset < 0 || offset > this.length) return false;\n return this.substring(offset, offset + prefix.length) == prefix;\n });\n\n /**\n * Tests if this string ends with the specified suffix.\n *\n * @example "goldvein".endsWith("ein")\n * @result true\n *\n * @example "goldvein".endsWith("vei")\n * @result false\n *\n * @name endsWith\n * @type Boolean\n * @param suffix The suffix to test\n * @cat Javascript/String\n */\n add("endsWith", function(suffix) {\n return this.substring(this.length - suffix.length) == suffix;\n });\n\n /**\n * Returns a new String that is no longer then a certain length.\n *\n * @example "thisistenc ".truncate(5);\n * @result "th..."\n *\n * @example "thisistenc ".truncate(5, "x")\n * @result "thisx"\n *\n * @name truncate\n * @type String\n * @param Number length (optional) The maximum length of the returned string, default is 30\n * @param String suffix (optional) The suffix to append to the truncated string, default is "..."\n * @cat Javascript/String\n */\n add("truncate", function(length, suffix) {\n length = length || 30;\n suffix = suffix === undefined ? "..." : suffix;\n return this.length > length ?\n this.slice(0, length - suffix.length) + suffix : this;\n });\n\n /**\n * Returns a new String with all tags stripped.\n *\n * @example "<div id=\'hi\'>Bla</div>".stripTags()\n * @result "Bla"\n *\n * @name stripTags\n * @type String\n * @cat Javascript/String\n */\n add("stripTags", function() {\n return this.replace(/<\\/?[^>]+>/gi, \'\');\n });\n\n})();\n' (<class 'bytes'>)
Hey, I’m using yuglify through django-pipeline and I’m getting the above error when compressing my JS. A naive attempt at running node with --max-old-spaace-size=8192
did not work (I tried editing the shebang line in /usr/bin/yuglify
).
Do you have any hints on how I would go about debugging/fixing this?
I'm using yuglify with --terminal.
Input:
@charset "UTF-8";
@media all {
DIV.test1 {
background: white;
}
}
@charset "UTF-8";
@media all {
DIV.test2 {
background: red;
}
}
@charset "UTF-8";
@media all {
DIV.test3 {
background: black;
}
}
Output:
@charset "UTF-8";@media all{DIV.test1{background:white}}@charset "UTF-8";@media all{DIV.test2{background:red}}@media all{DIV.test3{background:black}}
Only one @charset was removed...
It would be nice to be able to set a custom output path for the minified file. Today it's generated in the same path where the source is.
Steps to reproduce:
$ rm -rf bower_components && bower cache clean && bower install jquery#~1.11
bower deleted Cached package jquery: /home/jon/.cache/bower/packages/fe2fe255e91d251051d543998aa8327a/1.11.0
bower jquery#~1.11 not-cached git://github.com/jquery/jquery.git#~1.11
bower jquery#~1.11 resolve git://github.com/jquery/jquery.git#~1.11
bower jquery#~1.11 download https://github.com/jquery/jquery/archive/1.11.0.tar.gz
bower jquery#~1.11 extract archive.tar.gz
bower jquery#~1.11 resolved git://github.com/jquery/jquery.git#1.11.0
bower jquery#~1.11 install jquery#1.11.0
jquery#1.11.0 bower_components/jquery
$ yuglify bower_components/jquery/dist/jquery.js
Compressing bower_components/jquery/dist/jquery.js...
/home/jon/node_modules/yuglify/bin/yuglify:111
throw(err);
^
Error
at new JS_Parse_Error (/home/jon/node_modules/yuglify/node_modules/uglify-js/lib/parse-js.js:263:18)
at js_error (/home/jon/node_modules/yuglify/node_modules/uglify-js/lib/parse-js.js:271:11)
at croak (/home/jon/node_modules/yuglify/node_modules/uglify-js/lib/parse-js.js:733:9)
at token_error (/home/jon/node_modules/yuglify/node_modules/uglify-js/lib/parse-js.js:740:9)
at unexpected (/home/jon/node_modules/yuglify/node_modules/uglify-js/lib/parse-js.js:746:9)
at /home/jon/node_modules/yuglify/node_modules/uglify-js/lib/parse-js.js:1112:13
at maybe_unary (/home/jon/node_modules/yuglify/node_modules/uglify-js/lib/parse-js.js:1209:19)
at expr_ops (/home/jon/node_modules/yuglify/node_modules/uglify-js/lib/parse-js.js:1236:24)
at maybe_conditional (/home/jon/node_modules/yuglify/node_modules/uglify-js/lib/parse-js.js:1240:20)
at maybe_assign (/home/jon/node_modules/yuglify/node_modules/uglify-js/lib/parse-js.js:1264:20)
at /home/jon/node_modules/yuglify/node_modules/uglify-js/lib/parse-js.js:1278:20
at vardefs (/home/jon/node_modules/yuglify/node_modules/uglify-js/lib/parse-js.js:1065:32)
at var_ (/home/jon/node_modules/yuglify/node_modules/uglify-js/lib/parse-js.js:1077:26)
This failure to parse occurred some time between jQuery 1.10 and 1.11 as 1.10 parses fine:
$ rm -rf bower_components && bower cache clean && bower install jquery#~1.10
bower deleted Cached package jquery: /home/jon/.cache/bower/packages/fe2fe255e91d251051d543998aa8327a/1.10.2
bower jquery#~1.10 not-cached git://github.com/jquery/jquery.git#~1.10
bower jquery#~1.10 resolve git://github.com/jquery/jquery.git#~1.10
bower jquery#~1.10 download https://github.com/jquery/jquery/archive/1.10.2.tar.gz
bower jquery#~1.10 extract archive.tar.gz
bower jquery#~1.10 resolved git://github.com/jquery/jquery.git#1.10.2
bower jquery#~1.10 install jquery#1.10.2
jquery#1.10.2 bower_components/jquery
$ yuglify bower_components/jquery/jquery.js
Compressing bower_components/jquery/jquery.js...
Successfully generated JS file: bower_components/jquery/jquery.min.js
I submitted a bug to jQuery, but they believe this is a Yuglify bug. See: http://bugs.jquery.com/ticket/14998
UglifyJS also strips the parenthesis of an constructor with empty arguments.
Chrome cannot parse the minified file if:
(
example1.js:
// A lot of code...
var foo = new Foo(); // last line
example2.js
(function() { // first line
// A lot code...
example1.min.js'
(by UglifyJS)
var e=new Foo
example2.min.js
(by UglifyJS)
(function(){
example1.min.js
and example2.min.js
concatinated
var e=new Foo
(function(){
or (formated by chrome)
var e=new Foo(function(){
which is problematic.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.