Let me preface with two things. I am currently using grunt for these tasks, and I also know about Yeoman, which has what I ask for. I really like Yeomen, but he's too stubborn for this particular project I'm working on.
So, I have the following HTML file:
<html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title></title> <meta name="description" content=""> <meta name="viewport" content="width=device-width"> <link rel="stylesheet" href="css/bootstrap/bootstrap-2.1.1.css"> <link rel="stylesheet" href="css/normalize.css"> <link rel="stylesheet" href="css/boilerplate.css"> <script src="js/libraries/modernizr.js"></script> </head> <body> <p>Hello world! This is a basline HTML5 template (based on HTML5 Boilerplate).</p> <script src="js/libraries/underscore.js"></script> <script src="js/libraries/jquery/jquery.js"></script> </body> </html>
Now you can see CSS-MIN and JS-MIN comments. Right now I already have a custom grunt build task that correctly collects all of these files in comments (using htmlparser) and then minimizes and concatenates them as directly based on the comments. The final step in the build process is to create a new version of this HTML file (for use in production) that replaces comments with a new file. For example, the above code will be converted to this:
<html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title></title> <meta name="description" content=""> <meta name="viewport" content="width=device-width"> <link rel="stylesheet" href="css/build/min.css"> <script src="js/build/modernizr.js"></script> </head> <body> <p>Hello world! This is a basline HTML5 template (based on HTML5 Boilerplate).</p> <script src="js/build/libraries.js"></script> </body> </html>
I have a question, how can I do this in NodeJS? The htmlparser NPM module is great for parsing HTML, but now I need something where I modify the HTML (removing and adding certain elements in certain places). Are there any good packages / tutorials on how to do this in NodeJS code?
source share