Karma plugin: Add files to watching for preprocessor?

136 views
Skip to first unread message

andyt...@gmail.com

unread,
Oct 17, 2013, 7:23:11 PM10/17/13
to karma...@googlegroups.com
Hi there,

I just created a preprocessor for a project, that turns '<%= inlineTemplate("mytpl.html") %>' into '<div>My Template</div>'.


However, I just noticed a problem.

The preprocessor will only refresh the templates in the javascript whenever the javascript files change, but not whenever the templates change.

Here's a run-through of the bug:

1. Create script.js with contents '<%= inlineTemplate("mytpl.html") %>'
2. Create mytpl.html with contents '<div>My Template</div>'
3. Start karma, mytpl.html is put into script.js, test runs successfully
4. Change and save mytpl.html, test re-runs ... but the inline-template preprocessor has not re-run.  The old contents of mytpl.html are still in script.js.  And the old contents will stay there until I refresh script.js.

So.. is there any easy way I could, from my plugin, make it so all the templates referenced by inlineTemplate() are also added to the watcher list to 're-run' the preprocessor each js file?

Hope my question makes sense!

Thanks :-)

Vojta Jína

unread,
Oct 18, 2013, 2:00:02 AM10/18/13
to karma...@googlegroups.com
Hi Andy, your question makes sense, but Karma does not support this.

The preprocessing is 1-1.

I would suggest using grunt for this (this is similar to compiling js into a single minified file, I don't wanna support this, because it makes the development slow).

A workaround how to do it with Karma:
You could have another preprocessor (for the included templates), which would (whenever some inlined template changes) do fileList.changeFile(<path to the parent>) and that would trigger re-processing the parent file.
You could even do it without the additional preprocessor. When preprocessing the parent file, watch the inlined templates on your own (fs.watchFile or chokidar) and when any of them changes, do the fileList.updateFile(...)

V.


--
You received this message because you are subscribed to the Google Groups "karma-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to karma-users...@googlegroups.com.
To post to this group, send email to karma...@googlegroups.com.
Visit this group at http://groups.google.com/group/karma-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/karma-users/a8cda47a-fcdd-400d-872e-7d89443a1c9c%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

andyt...@gmail.com

unread,
Oct 18, 2013, 12:46:58 PM10/18/13
to karma...@googlegroups.com, andyt...@gmail.com
Hi Vojta,

Thanks for the ideas.  I don't want to use Grunt for the development reasons you mentioned (bad line numbers, no access to private vars since grunt wraps things in a closure for me, etc)

I will try out my own watch approach within the preprocessor, thanks.  The fileList approach seems clean enough.

andyt...@gmail.com

unread,
Oct 18, 2013, 1:37:39 PM10/18/13
to karma...@googlegroups.com, andyt...@gmail.com
So I tried the watch thing, and I had one problem - changeFile wouldn't actually update the js file!

I looked in fileList.changeFile's source and noticed it only actually updates the file if the mtime has changed - so this doesn't actually work:


(there's also a memory leak with creating lots of chokidars in there atm, will fix)

Vojta Jína

unread,
Oct 26, 2013, 1:18:41 AM10/26/13
to karma...@googlegroups.com
Opps, you are right, I didn't realize this.

Honestly, what's the problem with grunt solution?
You have grunt watching your templates (and subtemplates) and anytime something changes, the grunt task rebuilds the templates.
Karma is watching the result templates.

Or you can "touch" the parent template file ;-)

V.


--
You received this message because you are subscribed to the Google Groups "karma-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to karma-users...@googlegroups.com.
To post to this group, send email to karma...@googlegroups.com.
Visit this group at http://groups.google.com/group/karma-users.
Reply all
Reply to author
Forward
0 new messages