keithamus / npm-scripts-example

An example of how to use NPM scripts over Grunt/Gulp & Friends. http://blog.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool
MIT License
841 stars 101 forks source link

Cache-busting filenames without using Jade #22

Closed caprica closed 9 years ago

caprica commented 9 years ago

I really like the simplicity of this build approach, and I've got it working just fine for my own needs with one exception...

Your example uses "jade" to process the "assets.json" to insert the cache-busting hashed filenames.

My projects don't use jade, so can you recommend a simple way to apply the hashed filenames to an already existing static HTML file? e.g. simple string replacement of "index.js" to "index-abcdef.js", but based on the assets.json? This is my first foray into npm so I'm not really sure what is supposed to be done here.

I already tried things like using map-replace, but that project's last update was two years ago and it seemed to not install properly and failed to run.

keithamus commented 9 years ago

I'd very much recommend using a templating language for this sort of thing. If you don't like Jade, try Handlebars or EJS which both allow you to write plain HTML.

Otherwise write a simple piece of JS to find/replace a string, e.g.:

<!--SCRIPTS-->
<script src="foo.js"/>
<script src="bar.js"/>
<!--/SCRIPTS-->
var html = fs.readFileSync('index.html');
html = html.replace('<!--SCRIPTS.*/SCRIPTS-->', '<script src="bundle.js">');
fs.writeFileSync('index.compiled.html', html);

Should be a relatively simple task to extend this with hashmark support.

caprica commented 9 years ago

Hmm... ok. I will try something like Jade in my workflow, it might just make life easier.

I only really need it for the index.html, everything else is done with HTML "partials" (I'm using AngularJS in a single-page application), that's why I was trying to avoid yet another technology.

Thanks for the advice.