Minimize Javascript programmatically in memory

I am creating a great “resource pipeline” for an express.js application, but I have a problem with the compression step for javascript files.

scripts = (fs.readFileSync(file) for file in filelist) result = scripts.join("\n\n") # concat 

so far, everything is working as expected (the logic itself is written in coffeescript). The next step after merging JS files is to minimize them. But here is my problem: I want to make it “hot” when I run my express application in production mode, from within the middleware I wrote.

I need a solution that can minimize a given piece of javascript without writing the result to disk (!), In other words: a function that performs minimization and returns the result directly as the result value. (No, no web services either.) It should be used as follows:

 minified_result = awesomeMinifyFunction( result ) 

The raw processing performance is not so important to me, nor the level of compression, I just need something that works this way without the hassle.

Does anyone know a suitable solution? Thanks in advance!

+4
source share
3 answers

I would suggest you look at one of the JavaScript-based minifiers, like UglifyJS2 .

 npm install uglify-js 

It can be used in the Node.JS application programmatically:

 var UglifyJS = require("uglify-js"); // you could pass multiple files (rather than reading them as strings) var result = UglifyJS.minify([ "file1.js", "file2.js", "file3.js" ]); console.log(result.code); 

Or you could

 var result = scripts.join("\n\n"); # concat result = UglifyJS.minify(result, {fromString: true}); console.log(result.code); 
+7
source

You can write your own function that removes all comments / spaces / blank lines, etc.

You can use a regex that uses rJSmin , for example:

 function awesomeMinifyFunction(result) { pattern = ( r'([^\047"/\000-\040]+)|((?:(?:\047[^\047\\\r\n]*(?:\\(?:[^\r\n]|\r?' r'\n|\r)[^\047\\\r\n]*)*\047)|(?:"[^"\\\r\n]*(?:\\(?:[^\r\n]|\r?\n|' r'\r)[^"\\\r\n]*)*"))[^\047"/\000-\040]*)|(?<=[(,=:\[!&|?{};\r\n])(?' r':[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*' r'(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*' r'[^*]*\*+(?:[^/*][^*]*\*+)*/))*)*((?:/(?![\r\n/*])[^/\\\[\r\n]*(?:(' r'?:\\[^\r\n]|(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]\r\n]*)*\]))[^/\\\[' r'\r\n]*)*/)[^\047"/\000-\040]*)|(?<=[\000-#%-,./: -@ \[-^`{-~-]return' r')(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/' r'))*(?:(?:(?://[^\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:' r'/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))*((?:/(?![\r\n/*])[^/\\\[\r\n]*(?' r':(?:\\[^\r\n]|(?:\[[^\\\]\r\n]*(?:\\[^\r\n][^\\\]\r\n]*)*\]))[^/' r'\\\[\r\n]*)*/)[^\047"/\000-\040]*)|(?<=[^\000-!#%&(*,./: -@ \[\\^`{|' r'~])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)' r'*/))*(?:((?:(?://[^\r\n]*)?[\r\n]))(?:[\000-\011\013\014\016-\040]' r'|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)+(?=[^\000-\040"#%-\047)*,./' r': -@ \\-^`|-~])|(?<=[^\000-#%-,./: -@ \[-^`{-~-])((?:[\000-\011\013\01' r'4\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=[^\000-#%-,./:' r' -@ \[-^`{-~-])|(?<=\+)((?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*' r'\*+(?:[^/*][^*]*\*+)*/)))+(?=\+)|(?<=-)((?:[\000-\011\013\014\016-' r'\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)))+(?=-)|(?:[\000-\011\013' r'\014\016-\040]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+|(?:(?:(?://[^' r'\r\n]*)?[\r\n])(?:[\000-\011\013\014\016-\040]|(?:/\*[^*]*\*+(?:[^' r'/*][^*]*\*+)*/))*)+' ) return result.match(pattern); } 
0
source

I would recommend taking a look at Asset Rack, which already implements what you are building.

0
source

Source: https://habr.com/ru/post/1502867/


All Articles