Grunt Part 2
Posted 无处安放的灵魂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Grunt Part 2相关的知识,希望对你有一定的参考价值。
Objectives and Outcomes
In this exercise, you will continue to learn to use Grunt, the task runner. You will configure the Grunt file with a set of additional tasks to build your web project. At the end of this exercise, you will be able to:
- Configure a Grunt file with a set of tasks to build your web project from a source.
Copying the Files and Cleaning Up the Dist Folder
- Next you will install the Grunt modules to copy over files to a distribution folder named dist, and clean up the dist folder when needed. To do this, install the following Grunt modules:
- You will now add the code to perform the copying of files to the dist folder, and cleaning up the dist folder. To do this, add the following code to Gruntfile.js. This should be added right after the configuration of the SASS task.:
,
copy: {
html: {
files: [
{
//for html
expand: true,
dot: true,
cwd: ‘./‘,
src: [‘*.html‘],
dest: ‘dist‘
}]
},
fonts: {
files: [
{
//for font-awesome
expand: true,
dot: true,
cwd: ‘node_modules/font-awesome‘,
src: [‘fonts/*.*‘],
dest: ‘dist‘
}]
}
},
clean: {
build: {
src: [ ‘dist/‘]
}
}
- Remember to add the comma after the end of the SASS task.
Compressing and Minifying Images
- Next we install the grunt-contrib-imagemin module and use it to process the images. To install this module type at the prompt:
- Then, configure the imagemin task as shown below in the Gruntfile:
Preparing the Distribution Folder and Files
- We are now going to use the Grunt usemin module together with concat, cssmin, uglify and filerev to prepare the distribution folder. To do this, install the following Grunt modules:
- Next, update the task configuration within the Gruntfile.js with the following additional code to introduce the new tasks:
- Next, update the jit-grunt configuration as follows, to inform it that useminPrepare task depends on the usemin package:
- Next, update the Grunt build task as follows:
- Now if you run Grunt, it will create a dist folder with the files structured correctly to be distributed to a server to host your website. To do this, type the following at the prompt:
Conclusions
In this exercise you have learnt how to configure a Grunt file to perform several tasks. You were able to build a distribution folder for your web project.
以上是关于Grunt Part 2的主要内容,如果未能解决你的问题,请参考以下文章
我为何放弃Gulp与Grunt,转投npm scripts(上)
我为何放弃Gulp与Grunt,转投npm scripts(上)
我为何放弃Gulp与Grunt,转投npm scripts(中)