Created
November 25, 2017 14:14
-
-
Save bcswartz/0a28b5070c392b9c94d46ddfe447c259 to your computer and use it in GitHub Desktop.
Parses the index.html file to determine which JS and CSS files to concatenate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function( grunt ) | |
{ | |
grunt.initConfig({ | |
//Create jsResources and cssResources configuration properties | |
jsResources: [], | |
cssResources: [], | |
clean: { | |
build: { | |
dot: true, | |
src: [ '../build/**/*' ] | |
} | |
}, | |
copy: { | |
build: { | |
files: [ | |
{ | |
cwd: '../app/', | |
dest: '../build/', | |
expand: true, | |
//Copy over the index.html file and all the files in the "views" directory | |
src: [ 'index.html', 'views/**' ] | |
} | |
] | |
} | |
}, | |
replace: { | |
gather: { | |
files: [ | |
{ | |
cwd: '../app/', | |
dest: '../build/', | |
expand: true, | |
src: [ 'index.html' ] | |
} | |
], | |
options: { | |
patterns: [ | |
{ | |
//Grab the <!--build-js-start--> and <!--build-js-end--> comments and everything in-between | |
match: /\<\!\-\-build\-js\-start[\s\S]*build\-js\-end\-\-\>/, | |
replacement: function ( matchedString ) { | |
//Grab all of the src attributes from the <script> tags | |
var jsArray = matchedString.match( /(src\s?\=\s?[\'\"])([^\'\"]*)([\'\"])/g ); | |
jsArray.forEach( function( element ) { | |
//Get just the value of the src attribute (the file path to the JS file) | |
var resourceTarget = element.match( /(src\s?\=\s?[\'\"])([^\'\"]*)([\'\"])/ )[ 2 ]; | |
targetConfig = grunt.config( 'jsResources' ); | |
//Alter the path for use with the concat task | |
targetConfig.push( '../app/' + resourceTarget ); | |
//Add the path to the JS file to the jsResources configuration property | |
grunt.config( 'jsResources', targetConfig ); | |
}); | |
//Replace the entire build-js-start to build-js-end block with this <script> tag | |
return '<script type="text/javascript" src="combined.js"></script>'; | |
} | |
}, | |
{ | |
//Grab the <!--build-css-start--> and <!--build-css-end--> comments and everything in-between | |
match: /\<\!\-\-build\-css\-start[\s\S]*build\-css\-end\-\-\>/, | |
replacement: function ( matchedString ) { | |
//Grab all of the href attributes from the <href> tags | |
var cssArray = matchedString.match( /(href\s?\=\s?[\'\"])([^\'\"]*)([\'\"])/g ); | |
cssArray.forEach( function( element ) { | |
var resourceTarget = element.match( /(href\s?\=\s?[\'\"])([^\'\"]*)([\'\"])/ )[ 2 ]; | |
var targetConfig = grunt.config( 'cssResources' ); | |
//Alter the path for use with the concat task | |
targetConfig.push( '../app/' + resourceTarget ); | |
//Add the path to the CSS file to the cssResources configuration property | |
grunt.config( 'cssResources', targetConfig ); | |
}); | |
//Replace the entire build-css-start to build-css-end block with this <link> tag | |
return '<link rel="stylesheet" media="screen" href="combined.css"/>'; | |
} | |
} | |
] | |
} | |
} | |
}, | |
concat: { | |
js: { | |
//Concatenate all of the files in the jsResources configuration property | |
src: [ '<%= jsResources %>' ], | |
dest: '../build/combined.js', | |
options: { | |
separator: ';' | |
} | |
}, | |
css: { | |
//Concatenate all of the files in the cssResources configuration property | |
src: [ '<%= cssResources %>' ], | |
dest: '../build/combined.css' | |
} | |
} | |
}); | |
grunt.loadNpmTasks( 'grunt-contrib-clean' ); | |
grunt.loadNpmTasks( 'grunt-contrib-concat' ); | |
grunt.loadNpmTasks( 'grunt-contrib-copy' ); | |
grunt.loadNpmTasks( 'grunt-replace' ); | |
grunt.registerTask( 'build', 'Creates a build from the files in the app directory', function() { | |
//So the user doesn't have to add '--force' to the command to clean the build directory | |
grunt.option( 'force', true ); | |
grunt.task.run ( [ | |
'clean:build', | |
'copy:build', | |
'replace:gather', | |
'concat:css', | |
'concat:js' | |
]); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment