• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LDLX@Y火星
博客园    首页    新随笔    联系   管理    订阅  订阅

grunt的用法一

grunt也是工程化管理工具之一

首先你需要全局安装grunt,打开cmd命令

cnpm install -g grunt-cli

然后在你项目目录下执行

cnpm install --save grunt grunt-contrib-uglify

然后在项目下初始化一下

cnpm init

接着下载几个压缩插件 例如:htmlmin任务模块的/cssmin任务模块的

cnpm install grunt-contrib-htmlmin --save-dev

cnpm install grunt-contrib-cssmin --save-dev

 最后在根目录上建立Gruntfile.js这个js,里面添加如下配置代码

module.exports = function(grunt){
    //1.引入
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-htmlmin'); //2.设置任务:
    grunt.initConfig({
//        //压缩CSS
        cssmin:{
            yasuo:{
                options:{
                    mangle:false
                },
                expand: true,
                cwd: 'css',//压缩那个文件夹里的文件
                src:'*.css',//压缩那个文件
                dest:'yscss',//放压缩后文件的文件夹
                ext:'.min.css'//压缩后文件的的名字
            }
        },
//        //压缩HTML
        htmlmin:{
            options: {
                    removeComments: true, //移除注释
                    removeCommentsFromCDATA: true,//移除来自字符数据的注释
                    collapseWhitespace: true,//无用空格
                    collapseBooleanAttributes: true,//失败的布尔属性
                    removeAttributeQuotes: true,//移除属性引号      有些属性不可移走引号
                    removeRedundantAttributes: true,//移除多余的属性
                    useShortDoctype: true,//使用短的跟元素
                    removeEmptyAttributes: true,//移除空的属性
                    removeOptionalTags: true//移除可选附加标签
                  },
            yasuo:{
                expand: true,
                cwd: 'index', 
                src: ['*.html'],
                dest: 'yshtml'
            }
        }


    });
    //设置默认任务
    grunt.registerTask('default',['cssmin','htmlmin']);
}

  当然,你也可以在这个js中添加其他的配置

最后,直接在目录下的cmd中输入grunt,即可,你会发现目录中多了几个文件夹,css代码也被压缩了,注意的是,压缩html的是index文件夹不是文件哦

 

posted @ 2018-05-15 21:22  火星黑洞  阅读(289)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3