Fun times with batch files

One to be put squarely into the head-meets-desk section. For a little while now, I’ve been meaning to update how I minify and compress the JavaScript files used by this website (and others that use JS to some lesser degree). For ages I’ve been using compressor code that runs as a Java app (I know, I know! Just step away from the pepper spray right now!) – first it was the YUI Compressor (last release: eight years ago), and then a couple or more years ago I moved to the Google Closure Compiler because I was now using the more modern ES5+ syntax that the YUI Compressor couldn’t parse.

This morning, I came across this repo on GitHub: JS minification benchmarks. It’s basically code that calculates performance benchmarks for a bunch of JavaScript minifiers. For me? “Oh, look, a list of minifiers, let’s try some out, and see which one I like best!”

I selected terser as the first one: I’m not particularly bothered about speed (I only minify occasionally, it’s not like I’m always altering the JS for this site, so speed is not a big worry) and so I went for the minifier that produced the smallest output. One quick call to npm install terser –g later, I was ready to go.

As a start, for experimentation, I quickly wrote a batch file that would minify and concatenate the JavaScript I use for this site. There are actually two output JS files: one that’s used for all my readers, and one that’s used for when I’m logged in as the Boyet God (and, no, I’m not telling you what that JS does, and the filename I’m using here is fake!), so the batch file looked like this:

rem minify Boyet script
terser [input files] -o js\minifiedBoyet.js -c
rem minify Boyet God script
terser [input files] -o js\minifiedBoyetGod.js -c

I ran the batch file and it just stopped after executing the first (successful) call to terser. The second call never happened. Wut? The first minified JS file had been created, and looked OK, but the second was nowhere to be seen. Double wut?

Time for a bit of googling. First hint was to put each of the calls to terser in their own batch files, so all the main batch file does is to call the two one-line batch files. Didn’t work. Turns out that when you call a batch file from another, the called batch file doesn’t return: some historical reason dating from MS-DOS days. You have to explicitly call the secondary batch file, like this: call DoThisOtherThing.bat. Well I never, and I’ve been programming PCs since those MS-DOS days.

OK, bingo, here we go! Except nope, still didn’t work: the batch files just stopped after that first call to terser.

This took another bit of googling. It turns out that terser is not a batch file, nor an exe file, but a cmd file: terser.cmd. To run a cmd file inside a batch file, you have to use … get ready for it … the cmd command. With the /c switch so that it can return.

Boom, this is the batch file that actually worked as I’d expected:

rem minify Boyet script
call /c terser [input files]] -o js\minifiedBoyet.js -c
rem minify Boyet God script
call /c terser [input files]] -o js\minifiedBoyetGod.js -c

Now, what was I doing all this for again? Oh yes, minification. Time to test these new minified files to see that all their functionality works as before…

WrongWayNotAnExit - banner

Loading similar posts...   Loading links to posts on similar topics...

3 Responses

 avatar
#1 Jürgen Krämer said...
07-Feb-21 11:58 PM

In your last example you mixed up two correct solutions, resulting in a wrong solution. To call a bat file or a cmd file from a batch file, you can either prefix it call or cmd /c but not with call /c. Thus both

call terser [input files]] -o js\minifiedBoyet.js -c

and

cmd /c terser [input files]] -o js\minifiedBoyet.js -c

would work.

julian m bucknall avatar
#2 julian m bucknall said...
08-Feb-21 6:59 AM

@Jürgen: You are utterly right, I blew it 🙄. Now corrected, and thanks!

Cheers, Julian

 avatar
#3 Stuart Anderson said...
08-Feb-21 10:47 PM

Great! Thanks for this. It worked for me.

Leave a response

Note: some MarkDown is allowed, but HTML is not. Expand to show what's available.

  •  Emphasize with italics: surround word with underscores _emphasis_
  •  Emphasize strongly: surround word with double-asterisks **strong**
  •  Link: surround text with square brackets, url with parentheses [text](url)
  •  Inline code: surround text with backticks `IEnumerable`
  •  Unordered list: start each line with an asterisk, space * an item
  •  Ordered list: start each line with a digit, period, space 1. an item
  •  Insert code block: start each line with four spaces
  •  Insert blockquote: start each line with right-angle-bracket, space > Now is the time...
Preview of response