grunt-ts
grunt-ts copied to clipboard
Enhancement - Banner option
Hi basarat,
It would be super cool to have a kind of "banner" option which would combine with the "out" option of the grunt-ts task.
This way we wouldn't need to use another grunt task to insert a header after the concatenation. I suppose we could use the comment-are-kept feature in the _reference.ts but this doesn't integrate well with the need to keep our version centralized (package.json)
Cheers
I think there is the following feature request:
- Add a
banneroption to add a header to the merged javascript file.
One thing to be aware of is that typescript compiler generated sourcemaps may not work if you do any sort of manual modification of the generated javascript. That said I'd be happy to accept a pull request if you really want it :)
I did some looking into this. The spec for map files is linked from here as a Google doc: https://github.com/mozilla/source-map
Most of it is way over my head, but I was able to understand enough to see that it's based on line and character index in the unminified source. So if the requirement is simply to add some lines of arbitrary length at the start of the generated JS, it should be quite easy to hack the first entry in the .map file in an appropriate way.
To test this, I created files called noBanner.js, banner.js, and doubleBanner.js and minified them with map files using Uglify2.
Here is banner.js for example. noBanner.js didn't have a leading comment. doubleBanner.js had a two-line comment.
// THIS IS A BANNER
var x = 1;
x += 10;
console.log(x.toString());
This is the relevant line in each of the minified files:
//noBanner.js.map
"mappings":"AAAA,GAAIA,GAAI,CACRA,IAAK,EACLC,SAAQC,IAAIF,EAAEG"
//banner.js.map
"mappings":"AACA,GAAIA,GAAI,CACRA,IAAK,EACLC,SAAQC,IAAIF,EAAEG"
//doubleBanner.js.map
"mappings":"AAEA,GAAIA,GAAI,CACRA,IAAK,EACLC,SAAQC,IAAIF,EAAEG"
So you see, adding a line only incremented the third character of the first part "by two". I'll dig into the spec some more to figure out how it really works, but this particular requirement seems like it could actually be fairly easy to write - plus the fact that the .map file is JSON so it would even be straightforward to parse.
I'm actually thinking this could be a dual-purpose feature - for example if we implemented it as header and footer and provided some sugar for the metadata, we might even be able to support some core JS stuff that other TS workflow tools don't like Universal Modules (UMD) or even named AMD modules.