UglifyJS icon indicating copy to clipboard operation
UglifyJS copied to clipboard

[feature] Introduce magic comments for partial exclusion from transformations

Open gcnm-pgeil opened this issue 10 years ago • 11 comments

I want to suggest the introduction of what I call magic comments which may be contained inside the source code fed to uglifyjs and induce certain processing steps to change their default behaviour for according code ranges which are enclosed by magic comments.

Please let me explain a concrete use-case:

I want to use UglifyJS to process (minify/munge) several strings of JS code which are fed to it. I do this because my build pipeline operates on raw source code instead on a per file basis. However, these strings may simply contain the contents a some JS files read in a previous step. Now I want UglifyJS to minify or munge all the strings together before I write the result into a combined bundle file. Because my build pipeline spans multiple independent processing steps, I have no control/knowledge about whether the source code that is piped into the current (UglifyJS-) step is already minified/munged or not. But I want to prevent at any price that code breaks or the build step takes exorbitantly long because it is transforming and already transformed piece of code again.

Therefore came up with the idea of magic comments. They are 100% valid JavaScript style comments, do not influence any part of the application and are only understood by UglifyJS. They can be written right inside or wrapped to the input source either manually by the developer or simply prefixed/appended programmatically. And they actually allow for selectively skipping code range from certain transformations. When UglifyJS is used to minify/munge a raw source code string it does so but SKIPS code sections which are enclosed by the aforementioned magic comment.

The magic comments may be formed like JSHint directories, for example

/*uglifyjs:ignoreTransform:begin*/

var someJsCode = ...;

/*uglifyjs:ignoreTransform:end*/

Support for magic comments would offer a whole new level of granular control over UglifyJS to all developers/tools. What do you think?

Cheers

gcnm-pgeil avatar Jan 15 '16 11:01 gcnm-pgeil

There are a lot of issues with implementing such a feature.

A simple example would be variables declared outside of the block of code, but used inside of it. Would it still be available for mangling, or not? How about blocks that don't span complete statements? Where would you draw the line?

In the end, the real solution is to uglify only the resulting source that you're generating.

rvanvelzen avatar Jan 15 '16 11:01 rvanvelzen

Just keep the code away from uglifyJS before it can play with it. Once uglify-js has a representation of a file (say, the ast) you're already losing formatting data.

Adding features to prevent that would only make uglify-js more complex than needed. Sadly to say, but uglify-js isn't your do-it-all in one command thingy ;-)

avdg avatar Jan 15 '16 11:01 avdg

I see your point. Uglifying only the result source I am generating doesn't work in the scenario I've outlined. Because the source (=final combined output) I am generating may already contain code that has been minified/mangled in a previous processing step and right now I have no insight to this since I for exmaple have no file name I can match against a *.min.js pattern or so.

gcnm-pgeil avatar Jan 15 '16 11:01 gcnm-pgeil

Thanks for you honest words. :) Do you have an idea how I could cope with this issue else? My actual problem is, that I want to prevent re-transformations of already transformed code but all I have is the source code itself without any additional infos.

gcnm-pgeil avatar Jan 15 '16 11:01 gcnm-pgeil

Compressing isn't the problem as it's a separate phase. Just the internal representation, which is just the code (formatted as instructions in trees) without the formatting.

avdg avatar Jan 15 '16 11:01 avdg

The solution is to separate "sensitive" code from safe to minify code, or not uglify at all I'm afraid. In case of builds, you might want to search for ways to do things synchronized.

avdg avatar Jan 15 '16 11:01 avdg

@gcnm-pgeil Can this issue be closed? I'll let this decide on your side.

avdg avatar Jan 21 '16 16:01 avdg

(Hmm we might need a "magic" comments filter option actually in some other way, but pls, no macro's/macro-like things, they only make it worse for parsers)

avdg avatar Jan 21 '16 16:01 avdg

@avdg Yes, can be closed. ;)

gcnm-pgeil avatar Jan 22 '16 15:01 gcnm-pgeil

I think I found a way to emulate this behaviour but it's macro based and still outside the scope of uglify.

You need a preparser and a postparser to convert commented code to eval expressions and back after minifying. Just adding a comment in front of the code in eval like 'eval ("/macro/...code")' would give these evals a signature so it's easy to find them back.

Again, not on uglify side. But I'm sure there are great tools to handle them anyway.

avdg avatar Jan 25 '16 00:01 avdg

Clever idea! I will keep it in mind. ;)

gcnm-pgeil avatar Aug 07 '16 20:08 gcnm-pgeil