博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Bootstrap 之 Metronic 模板的学习之路 - (7)GULP 前端自动化工具
阅读量:6477 次
发布时间:2019-06-23

本文共 12498 字,大约阅读时间需要 41 分钟。

初步了解 Metronic 的结构和应用后,我们就可以在项目中应用起来了。考虑到实际项目应用时,会有很多文件需要修改调整并压缩处理,这时候我们就会用到我们的,鉴于 gulp 的内容比较多,为了不打乱 Metronic 系列,就单独列了一篇。本文不对 gulp 进行详细阐述,只保留和 Metronic 相关部分。

Gulp is a toolkit that will help you automate painful or time-consuming tasks in your development workflow. To get started first install Node.js as gulp require npm to run. It can be downloaded at We can now install Gulp using npm. To install Gulp just follow below steps:

Gulp 是一个在你开发流程中帮助你自动化一些费时费力的苦差事的工具箱。

部署 gulp

打开终端并进入到 Metronic 的 theme 根目录。请查看该目录下 gulpfile.js 文件是否存在。

若存在,
运行下面命令

// 安装 gulp 附属sudo npm install// 全局安装 gulp,这种模式安装后,gulp 可以应用到所有项目sudo npm install gulp -g

查看 gulpfile.js

'use strict';// sass compilevar gulp = require('gulp');var sass = require('gulp-sass');var prettify = require('gulp-prettify');var minifyCss = require("gulp-minify-css");var rename = require("gulp-rename");var uglify = require("gulp-uglify");var rtlcss = require("gulp-rtlcss");  var connect = require('gulp-connect');//*** Localhost server tastgulp.task('localhost', function() {  connect.server();});gulp.task('localhost-live', function() {  connect.server({    livereload: true  });});//*** SASS compiler taskgulp.task('sass', function () {  // bootstrap compilation    gulp.src('./sass/bootstrap.scss').pipe(sass()).pipe(gulp.dest('./assets/global/plugins/bootstrap/css/'));  // select2 compilation using bootstrap variables    gulp.src('./assets/global/plugins/select2/sass/select2-bootstrap.min.scss').pipe(sass({outputStyle: 'compressed'})).pipe(gulp.dest('./assets/global/plugins/select2/css/'));  // global theme stylesheet compilation    gulp.src('./sass/global/*.scss').pipe(sass()).pipe(gulp.dest('./assets/global/css'));    gulp.src('./sass/apps/*.scss').pipe(sass()).pipe(gulp.dest('./assets/apps/css'));    gulp.src('./sass/pages/*.scss').pipe(sass()).pipe(gulp.dest('./assets/pages/css'));  // theme layouts compilation    gulp.src('./sass/layouts/layout/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout/css'));  gulp.src('./sass/layouts/layout/themes/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout/css/themes'));  gulp.src('./sass/layouts/layout2/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout2/css'));  gulp.src('./sass/layouts/layout2/themes/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout2/css/themes'));  gulp.src('./sass/layouts/layout3/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout3/css'));  gulp.src('./sass/layouts/layout3/themes/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout3/css/themes'));  gulp.src('./sass/layouts/layout4/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout4/css'));  gulp.src('./sass/layouts/layout4/themes/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout4/css/themes'));  gulp.src('./sass/layouts/layout5/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout5/css'));  gulp.src('./sass/layouts/layout6/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout6/css'));  gulp.src('./sass/layouts/layout7/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout7/css'));});//*** SASS watch(realtime) compiler taskgulp.task('sass:watch', function () {    gulp.watch('./sass/**/*.scss', ['sass']);});//*** CSS & JS minify taskgulp.task('minify', function () {    // css minify     gulp.src(['./assets/apps/css/*.css', '!./assets/apps/css/*.min.css']).pipe(minifyCss()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/apps/css/'));    gulp.src(['./assets/global/css/*.css','!./assets/global/css/*.min.css']).pipe(minifyCss()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/global/css/'));    gulp.src(['./assets/pages/css/*.css','!./assets/pages/css/*.min.css']).pipe(minifyCss()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/pages/css/'));            gulp.src(['./assets/layouts/**/css/*.css','!./assets/layouts/**/css/*.min.css']).pipe(rename({suffix: '.min'})).pipe(minifyCss()).pipe(gulp.dest('./assets/layouts/'));    gulp.src(['./assets/layouts/**/css/**/*.css','!./assets/layouts/**/css/**/*.min.css']).pipe(rename({suffix: '.min'})).pipe(minifyCss()).pipe(gulp.dest('./assets/layouts/'));    gulp.src(['./assets/global/plugins/bootstrap/css/*.css','!./assets/global/plugins/bootstrap/css/*.min.css']).pipe(minifyCss()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/global/plugins/bootstrap/css/'));    //js minify    gulp.src(['./assets/apps/scripts/*.js','!./assets/apps/scripts/*.min.js']).pipe(uglify()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/apps/scripts/'));    gulp.src(['./assets/global/scripts/*.js','!./assets/global/scripts/*.min.js']).pipe(uglify()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/global/scripts'));    gulp.src(['./assets/pages/scripts/*.js','!./assets/pages/scripts/*.min.js']).pipe(uglify()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/pages/scripts'));    gulp.src(['./assets/layouts/**/scripts/*.js','!./assets/layouts/**/scripts/*.min.js']).pipe(uglify()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/layouts/'));});//*** RTL convertor taskgulp.task('rtlcss', function () {  gulp    .src(['./assets/apps/css/*.css', '!./assets/apps/css/*-rtl.min.css', '!./assets/apps/css/*-rtl.css', '!./assets/apps/css/*.min.css'])    .pipe(rtlcss())    .pipe(rename({suffix: '-rtl'}))    .pipe(gulp.dest('./assets/apps/css'));  gulp    .src(['./assets/pages/css/*.css', '!./assets/pages/css/*-rtl.min.css', '!./assets/pages/css/*-rtl.css', '!./assets/pages/css/*.min.css'])    .pipe(rtlcss())    .pipe(rename({suffix: '-rtl'}))    .pipe(gulp.dest('./assets/pages/css'));  gulp    .src(['./assets/global/css/*.css', '!./assets/global/css/*-rtl.min.css', '!./assets/global/css/*-rtl.css', '!./assets/global/css/*.min.css'])    .pipe(rtlcss())    .pipe(rename({suffix: '-rtl'}))    .pipe(gulp.dest('./assets/global/css'));  gulp    .src(['./assets/layouts/**/css/*.css', '!./assets/layouts/**/css/*-rtl.css', '!./assets/layouts/**/css/*-rtl.min.css', '!./assets/layouts/**/css/*.min.css'])    .pipe(rtlcss())    .pipe(rename({suffix: '-rtl'}))    .pipe(gulp.dest('./assets/layouts'));  gulp    .src(['./assets/layouts/**/css/**/*.css', '!./assets/layouts/**/css/**/*-rtl.css', '!./assets/layouts/**/css/**/*-rtl.min.css', '!./assets/layouts/**/css/**/*.min.css'])    .pipe(rtlcss())    .pipe(rename({suffix: '-rtl'}))    .pipe(gulp.dest('./assets/layouts'));  gulp    .src(['./assets/global/plugins/bootstrap/css/*.css', '!./assets/global/plugins/bootstrap/css/*-rtl.css', '!./assets/global/plugins/bootstrap/css/*.min.css'])    .pipe(rtlcss())    .pipe(rename({suffix: '-rtl'}))    .pipe(gulp.dest('./assets/global/plugins/bootstrap/css')); });//*** HTML formatter taskgulp.task('prettify', function() {            gulp.src('./**/*.html').            pipe(prettify({            indent_size: 4,             indent_inner_html: true,            unformatted: ['pre', 'code']           })).           pipe(gulp.dest('./'));});

可以看出,Metronic 的 gulp 主要有 localhostlocalhost-livesasssass:watchminifyrtlcssprettify 几个命令,分别实现不同的任务。

gulp 任务运用

首先我们在终端下用 gulp 命令测试,结果如下报错:

clipboard.png

按照提示,在执行npm install后需要执行 npm rebuild node-sass 重建 node-sass 来绑定当前环境。

执行 npm rebuild node-sass 后再执行 gulp,显示如下:

clipboard.png

说明 gulp 部署成功,只是默认任务还没有配置。我们先来看看gulpfile.js 列出的其他任务。

gulp localhost 任务和 gulp localhost-live 任务

在终端下执行:

gulp localhost

执行结果如下:

clipboard.png

可见本地 web 服务已启动,当我们想要查看单独的主题演示列表的时候,我们可以通过在浏览器输入http://localhost:8080进行访问。

gulp localhost-live 任务则可以运行实时更新支持的本地服务。

注意:一些基于 ajax 和数据库的演示无法通过 gulp localhost 服务正常展现. 要完全运行这些主题,需要一个支持 Apache 和 PHP 的环境.

SASS 编译

打开终端进入到 gulpfile.js 所在的 theme 目录。输入 gulp sass 命令执行手工构建任务.

输入 gulp sass:watch 运行 scss 实时监控 css 文件的编译.

clipboard.png

相关监控代码:

//*** SASS compiler taskgulp.task('sass', function () {  // bootstrap compilation    gulp.src('./sass/bootstrap.scss').pipe(sass()).pipe(gulp.dest('./assets/global/plugins/bootstrap/css/'));  // select2 compilation using bootstrap variables    gulp.src('./assets/global/plugins/select2/sass/select2-bootstrap.min.scss').pipe(sass({outputStyle: 'compressed'})).pipe(gulp.dest('./assets/global/plugins/select2/css/'));  // global theme stylesheet compilation    gulp.src('./sass/global/*.scss').pipe(sass()).pipe(gulp.dest('./assets/global/css'));    gulp.src('./sass/apps/*.scss').pipe(sass()).pipe(gulp.dest('./assets/apps/css'));    gulp.src('./sass/pages/*.scss').pipe(sass()).pipe(gulp.dest('./assets/pages/css'));  // theme layouts compilation    gulp.src('./sass/layouts/layout/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout/css'));  gulp.src('./sass/layouts/layout/themes/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout/css/themes'));  gulp.src('./sass/layouts/layout2/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout2/css'));  gulp.src('./sass/layouts/layout2/themes/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout2/css/themes'));  gulp.src('./sass/layouts/layout3/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout3/css'));  gulp.src('./sass/layouts/layout3/themes/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout3/css/themes'));  gulp.src('./sass/layouts/layout4/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout4/css'));  gulp.src('./sass/layouts/layout4/themes/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout4/css/themes'));  gulp.src('./sass/layouts/layout5/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout5/css'));  gulp.src('./sass/layouts/layout6/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout6/css'));  gulp.src('./sass/layouts/layout7/*.scss').pipe(sass()).pipe(gulp.dest('./assets/layouts/layout7/css'));});

编译后的 css 文件会输出到 assets 目录.

clipboard.png

gulpfile.js 中相关代码:

//*** SASS watch(realtime) compiler taskgulp.task('sass:watch', function () {    // 监控到 `*.scss` 变化执行 `sass` 任务    gulp.watch('./sass/**/*.scss', ['sass']);});

RTL SASS 编译

注意,因为 RTL 的主题文件和默认的主题文件不在同一个目录,一个在 theme_rtl ,一个在 theme,所以,在 theme_rtl 目录下,需要对 gulp 进行重新部署。

部署命令如下:

sudo cnpm installsudo cnpm install gulpgulp sass

关于 cnpm 见。

然后进入gulpfile.js 位于的 theme_rtl 目录。

确定最新修改的 SCSS 已通过 gulp sassgulp sass:watch 命令编译。
运行 gulp rtlcss 命令执行 css 文件的 RTL 版本修改任务。
编译后的 RTL css 会被输出到 assets 目录.

clipboard.png

CSS 和 JS 文件的压缩

在命令行下进入 theme 目录。运行 gulp minify 执行 css 和 js 文件压缩任务.

压缩后的文件会输出到 assets 目录.

clipboard.png

相关代码:

//*** CSS & JS minify taskgulp.task('minify', function () {    // css minify     gulp.src(['./assets/apps/css/*.css', '!./assets/apps/css/*.min.css']).pipe(minifyCss()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/apps/css/'));    gulp.src(['./assets/global/css/*.css','!./assets/global/css/*.min.css']).pipe(minifyCss()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/global/css/'));    gulp.src(['./assets/pages/css/*.css','!./assets/pages/css/*.min.css']).pipe(minifyCss()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/pages/css/'));            gulp.src(['./assets/layouts/**/css/*.css','!./assets/layouts/**/css/*.min.css']).pipe(rename({suffix: '.min'})).pipe(minifyCss()).pipe(gulp.dest('./assets/layouts/'));    gulp.src(['./assets/layouts/**/css/**/*.css','!./assets/layouts/**/css/**/*.min.css']).pipe(rename({suffix: '.min'})).pipe(minifyCss()).pipe(gulp.dest('./assets/layouts/'));    gulp.src(['./assets/global/plugins/bootstrap/css/*.css','!./assets/global/plugins/bootstrap/css/*.min.css']).pipe(minifyCss()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/global/plugins/bootstrap/css/'));    //js minify    gulp.src(['./assets/apps/scripts/*.js','!./assets/apps/scripts/*.min.js']).pipe(uglify()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/apps/scripts/'));    gulp.src(['./assets/global/scripts/*.js','!./assets/global/scripts/*.min.js']).pipe(uglify()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/global/scripts'));    gulp.src(['./assets/pages/scripts/*.js','!./assets/pages/scripts/*.min.js']).pipe(uglify()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/pages/scripts'));    gulp.src(['./assets/layouts/**/scripts/*.js','!./assets/layouts/**/scripts/*.min.js']).pipe(uglify()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./assets/layouts/'));});

HTML 格式化

该命令可以将 HTML 格式化缩进,对于 precode 两个标签内的内容不进行格式化。

代码如下:

//*** HTML formatter taskgulp.task('prettify', function() {            gulp.src('./**/*.html').            pipe(prettify({            indent_size: 4,             indent_inner_html: true,            unformatted: ['pre', 'code']           })).           pipe(gulp.dest('./'));});

上一篇:

本系列暂告一段落,Metronic 的部件详细分析和后台搭配等以后有机会再看咯。先留个坑,至于何年何月能填上,不晓得哦 :b

转载地址:http://aomko.baihongyu.com/

你可能感兴趣的文章
IOS 手机震动
查看>>
智能指针的实现
查看>>
《The Missing Manual Javascript 》 - 书摘精要
查看>>
C#和Modbus通信工程
查看>>
winedit 改字体大小
查看>>
如何成为“10倍效率的开发者”
查看>>
在JAVA中如何判断按下两个键
查看>>
无法安装程序包“MIcrosoft.Owin.Security 2.0.2”。您正在尝试将此程序包安装到某个将“.NETFramework,Version=v4.0”作为目标的项目中。...
查看>>
spark使用scala读取Avro数据(转)
查看>>
中文词频统计
查看>>
「苦练基本功」超级大佬推荐工程师必看的书感悟
查看>>
折腾笔记之wordpress安装出现错误---【wordpress点击文章找不到网页的解决办法】...
查看>>
387. 最小差
查看>>
LCS问题
查看>>
使用ReaderWriterLock类实现多用户读/单用户写同步
查看>>
第4次作业类测试代码+105032014158+余超勇
查看>>
有关于虚拟DOM
查看>>
codves 4927 线段树练习5
查看>>
幻方(zjut1005) 与 反幻方(hdu 3927)
查看>>
vue中nextTick的使用(转载)
查看>>