0

There are many tools out there to "compress" more or less javascript files by mangling variable names, removing comments, etc...

But how to "tell" them to NOT process a part of a file, from this file. I need to "tell" them somehow, by even rewriting that part another way, or putting some tags in comments, ... to NOT process some parts of my files.

The easiest but very ugly solution I found was to put my code in a string and eval it. But if there is any other solution I'd love to know it because eval is UGLY!

UPDATE:

please due to my requirements, those are not possible answers:

  • the file HAVE to be minified
  • it needs to be done FROM WITHIN the source code
Huafu
  • 2,445
  • 1
  • 22
  • 26
  • Cut it out, and paste it back in after compressing? – Paradoxis Nov 10 '14 at 21:24
  • 1
    Why do you not want it minified? You could always just not minify the file in question. – Shriike Nov 10 '14 at 21:25
  • No, that is not an option, I need to do it **FROM** the source – Huafu Nov 10 '14 at 21:26
  • @Shriike because it get downloaded faster by browsers, but that is not the question – Huafu Nov 10 '14 at 21:26
  • 1
    @Huafu my question was actually the opposite. I was asking why you don't want part of your file minified. If we understand the motivation it can help everyone arrive at a solution. – Shriike Nov 10 '14 at 21:29
  • Maybe he does changes frequently to that part and wants it easy to read – Alexandru Severin Nov 10 '14 at 21:30
  • @Shriike my bad, I guess 4.30am isn't time to read answers lol. Well the thing is that I "play" with prototype, for example but that is not only it I am reading source of functions `someFunction.toString()` to use them as templates to build new functions, I detect presence of arguments in the signature, ... – Huafu Nov 10 '14 at 21:31

2 Answers2

1

if you are using gulp, there is a module called gulp-tap that you can use to grab the file, split it between some type of identifier, minify the parts you want, and then concatenate it back together. I'm sure that grunt or other task runners have similar capabilities

  • yes thank you but I need to do this from the source, whatever minifier I'd be using. I really need to have this done in the source, period ;-) – Huafu Nov 10 '14 at 21:41
  • "From the source"? It will very much depend on what minifier you're using of course. – Rudie Nov 10 '14 at 21:42
  • Well, even if dirty, `eval` is a good example of what I meant by `from the source`, since it's working with all minifiers, and it's in the sources. But yeah, eval is ugly – Huafu Nov 10 '14 at 22:02
  • when you say 'from the source' , do you mean at runtime? Do you not have control over the actual files? – Stephen Sylvester Nov 14 '14 at 16:00
  • I have control over the actual files, but I mean, I want to tell the minifier to not minify part of my file from the source, with some tags for example – Huafu Nov 16 '14 at 04:50
0

Edit, Updated

Tried utilizing comments at yuicompressor ?

/*! function someFunction(i) {i = "abc"; return i} */
// minified stuff 

See Comment Starting with /*!, gist , Online JavaScript/CSS Compressor


Try

/*! (function someFunction(i) {document.write(i); return i}(123)) */
    // minified stuff
    console.log("abc");
var scripts = document.scripts
, script = scripts[scripts.length - 1].innerText
.match(/\/\*!+.*\*\//g)[0].replace(/\/\*!|\*\//g, "").trim();
eval(script);

/*! (function someFunction(i) {document.write(i); return i}(123)) */
    // minified stuff
console.log("abc");
var scripts = document.scripts
, script = scripts[scripts.length - 1].innerText
.match(/\/\*!+.*\*\//g)[0].replace(/\/\*!|\*\//g, "").trim();
eval(script);

See Degrading Script Tags

Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177
  • That is the best answer so far, but it has the bad side of then having the functions not handled as source code in most IDEs if not all – Huafu Nov 16 '14 at 04:51