Combine and minify templates with CoffeeScript / Cake - coffeescript

I have a src/templates/ directory full of mustache templates. How would I combine and minify the contents of those, so they're available for use in my CoffeeScript app?
I'm already following the directions at https://github.com/jashkenas/coffee-script/wiki/%5BHowTo%5D-Compiling-and-Setting-Up-Build-Tools for combining and minifying my CoffeeScript src into js.

First off, I'll assume that your templates are being exported to the global object (e.g. each one does window.userpane = rather than just userpane =). That's the most important thing. If you're doing that, and you're concatenating and compiling successfully, then the only thing left is to have automatic minification after each concatenation.
Short answer: There's no good tool for this yet. Your best option is to extend your existing Cakefile with a line like
fs.watchFile 'concatenated.js', ->
exec 'uglifyjs concatenated.js'
(To install UglifyJS, run npm install uglify-js.)
Now, this won't solve the problem of ensuring that your scripts are concatenated in a sensible order. (For instance, if you have window.templates = {} in file A and templates.userpane = in file B, then it's very important that file A be concatenated before file B.) For that, you should keep an eye on Sprockets, which lets you indicate at the top of each JS file what its dependencies are, then combine them in an order that respects those dependencies. The creator of Sprockets, Sam Stephenson, is an active member of the CoffeeScript community, and first-class support for CoffeeScript in Sprockets is coming in Sprockets 2 (repo here).
Update: Here's a Cake task to do the actual reading and concatenating of everything in the template directory:
templateJs = ''
files = fs.readdirSync 'template'
for file in files
contents = fs.readFileSync file, 'utf8'
name = file.replace /\..*/, '' # remove extension
templateJs += "window.#{name} = '#{contents}';"
Then prepend your concatenated JS with templateJs. Note that this assumes that there are no single quotes (') in the template. Either put backslashes in front of them or consistently use double quotes.

Related

Is there a way to use babel plugins relying ont he current filename with babel-loader?

I wrote a plugin for babel that relies on the opts.filename and opts.filenameRelative properties. It seems to be working within babel-loader for the purposes of analyzing the adjacent files, but the filename itself seems to be modified.
I'm wondering if theres a way, using babel-loader, to get access to the full source file path to use for generating a legible id and hash.
babel-loader does indeed pass the filename into the transform function. In my particular case, I was preprocessing typescript files with awesome-typescript-loader, and that was messing with the file path.

Duplicate Outputs in Doxygen

I'm generating developer documentation using Doxygen. It's parsing all of the files correctly, but the output is generating duplicate entries in the member function list and class diagram.
Any ideas?
I had this exact problem, and found that I had accidentally specified a build folder in the INPUT line due to RECURSIVE being on, e.g.,
Example file structure:
./
MyLibrarySources/
Libs/
build/
Doxyfile:
INPUT = ./ MyLibrarySources/ ...
RECURSIVE = YES
This caused Doxygen to parse the headers from two different locations: once from MyLibrarySources/, and once from build/, producing duplicate members and other odd results.
The easy solution is to add your build directory to the EXCLUDE line, e.g.:
EXCLUDE = "build"
This makes Doxygen not parse the same header files in two different locations. And yes, in-source build directories are usually a bad idea, place them elsewhere. In my case, command-line builds not issued from my IDE went there by default.
Edit note: I had incorrectly believed that the source files were being parsed twice because of the double-specification in the INPUT line. This is not the case. Doxygen is smart about this and will not parse the same physical file twice 👍.

Joining files in Coffescript

I am currently looking to use RequireJS to ensure modularity in the front end side of my project written in Coffeescript. Is there a way to use "import" directives in coffeescript so that you could recursive compile a large number of ".coffee" files into a single ".js" file - and potentially minify it too. I know there is a "join" argument you can pass to the coffeescript compiler, but it would be really useful to just reference files from one to another.
Secondly, it should be able to do this compilation really fast, so that the filewatcher can immediately change the entire output .js file every time any file (out of hundreds of files) was changed.
In addition, if it provided inbuilt minification/obfuscation that would be GREAT!

Is it possible to combine group files and group directories?

I am using StringTemplate to generate HTML and I would like to be able to combine STGroupFile and STGroupDir such that I have a directory of group files. This way I can include multiple templates in one file but also combine templates from multiple files. It doesn't appear that there is support for this but I thought I would see if maybe I'm overlooking something.
In case you are wondering why I would want to do this, lets look at an example from the StringTemplate docs:
test(name) ::= "$name:bracket()$" // apply bracket template to each name
bracket(x) ::= "[$x$]" // surround parameter with square brackets
Assume the above is all in one file (test.st) and we are using STGroupDir, it will be unable to resolve the "bracket(x)" template unless it is in a separate file (bracket.st). In this particular instance, bracket is like a local helper which is useful for cleaning up the main template. In this case, you probably don't want bracket to be visible outside of test.st.
However, if you wanted to create a library of simple helper templates, you might want to locate them all in one file and have each of them accessible to templates in other files.
Disclaimer: I'm using StringTemplate in C#
You need to use .stg files with groups of templates. StringTemplate groups can import other templates or groups of templates, and you can create those groups from a file or a directory.

ANTLR - specifying multiple directories to search in for included grammars

I am extending an existing open-source project, which already has a very advanced Lexer/Parser/TreeParser in ANTLR. I would like to adhere to the current directory structure, so I created my own directory where I would like to put my new (already written) grammar files. They are importing these three original grammar files and to compile the code, I can use java org.antlr.Tool, where I can specify one directory to search for imported grammars with -lib option argument.
My problem is that these three original imported grammar files have some imports themselves and again in different directory. To illustrate (inheritance/importing going to top):
Abstract syntax directory: lexer parser tree parser
Original syntax directory: lexer parser tree parser
My new syntax directory: lexer parser tree parser
This is the hierarchy. The trouble I am encountering is how to specify "Abstract syntax directory" and also "Original syntax directory" at once with the -lib option on the command line for the ANTLR tool (or any other solution which would allow me to compile my grammar importing the original one with the given directories structure).
I tried -lib directory1 directory2, that just appears to ignore the directory2 (and then tries to compile it, which it cannot, because it is a directory, not an ANTLR grammar). I tried specifying "-lib directory1 -lib directory2", the "-lib directory2" just overwrites the "-lib directory1" then.
It is what I believe a fairly basic need to specify multiple directories to search in, in any more complicated system of grammars, therefore I am sure I missed something. I just cannot google anything useful out.
IF you need any more details, I am happy to provide them, it is my master thesis extending an open source project, so I do not need to keep anything secret. :) If it is not possible, I can live without it, but would really like to keep the consistency of the original project.
Looking at the source of the org.antlr.Tool class:
else if (args[i].equals("-lib")) {
if (i + 1 >= args.length) {
System.err.println("missing library directory with -lib option; ignoring");
}
else {
i++;
libDirectory = args[i];
if (libDirectory.endsWith("/") || libDirectory.endsWith("\\")) {
libDirectory = libDirectory.substring(0,libDirectory.length()-1);
}
File outDir = new File(libDirectory);
if (!outDir.exists()) {
ErrorManager.error(ErrorManager.MSG_DIR_NOT_FOUND,libDirectory);
libDirectory = ".";
}
}
}
it appears just one -lib directory is being read.
I'm assuming your (implied) question is hereby answered (that it is not possible to point to more than 1 -lib directory).