Skip to content

Instantly share code, notes, and snippets.

@swapnilmishra
Last active December 20, 2015 17:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save swapnilmishra/6166366 to your computer and use it in GitHub Desktop.
Save swapnilmishra/6166366 to your computer and use it in GitHub Desktop.
Handle development/production HTML linkings of resources such as JS,CSS so that in development, you would have 2 or more link/script tags but in production you would just point to the minified versions.

Grunt plugins used

grunt-link-html grunt-contrib-copy

Instructions for running

  1. Modify path of js,css files according to your project structure.
  2. Modify path of header-template.html and header.html according to your project structure.
  3. Run
grunt build-prod // to build for production
grunt build-dev  // to build for development
module.exports = function(grunt) {
grunt.initConfig({
/*
copy header-template.html which will have placeholders for injecting css,js
*/
copy: {
main: {
files: [
{
src: ['header-template.html'],
dest: 'header.html'
}
]
}
},
link_html: {
// dev target to which will copy normal uncompressed files used for development
dev: {
jsFiles: ['public/default.js'], // path to dev js files
cssFiles: ['public/style.css'], // path to dev css files
targetHtml: ['header.html']
},
// prod target to which will copy normal uncompressed files used for production
prod: {
jsFiles: ['public/default.min.js'], // path to prod js files
cssFiles: ['public/style.min.css'], // path to prod css files
targetHtml: ['header.html']
}
},
}); // end of configuring the grunt task
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-link-html');
// this task can be run with "grunt build-prod"
grunt.registerTask('build-prod', 'Production', function() {
//copy template which has placeholders for js,css injection
grunt.task.run('copy');
// run link_html task which will replace our placeholders by js,css files supplied
grunt.task.run('link_html:prod');
});
// this task can be run with "grunt build-prod"
grunt.registerTask('build-dev', 'Development', function() {
grunt.task.run('copy');
grunt.task.run('link_html:dev');
});
};
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<!-- begin:css -->
<!-- end:css -->
</head>
<body>
<!-- begin:js -->
<!-- end:js -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment