I am using standard minify/uglify for css/js files and combine multiple files to main.min.css or app.min.js... However my .html file needs to be modified to point to these new file names too in <link>
or <script>
Is there a way to automate this? Or how to modify .html files automatically to rename the file names in there using gruntjs
?
You can do this with grunt-string-replace. Here's an example on how you could use it.
In my index.html you find the following import tags:
Notice the 'start imports' and 'end imports' comments. By default (in DEV) we comment out the PROD import.
In my grunt file I then add the following task:
Running the task (grunt string-replace) gives me:
Now the DEV imports have been commented out, while the PROD import is no longer commented out.
I am using Middleman App do distinguish between dev vs build in my html or haml file:
- if development?
and
- if build?
This is easily automated with grunt-processhtml. Here's an example from the docs:
Read more at https://www.npmjs.org/package/grunt-processhtml
You could use grunt preprocess to do this: https://github.com/jsoverson/grunt-preprocess
Basically, you need to set up a template and have preprocess replace the relevant parts.
The Gruntfile part will look something like this:
},
A very suitable grunt task for this is grunt-html-build
It can substitute some some parts of the HTML from dev to a production version. See examples there, it is easy to setup.
Now, using the standard configuration presented for grunt-html-build, if minified files are dynamically named during build process like:
some-file.js
->another-name.min.js
One can configure grunt-html-build with:
A HTML section like:
Would yield to something like:
That is what @hyprstack is asking for in comments.