GVKun编程网logo

gulp之JS、CSS、HTML、图片压缩以及版本更新(js 图片压缩库)

2

如果您想了解gulp之JS、CSS、HTML、图片压缩以及版本更新的相关知识,那么本文是一篇不可错过的文章,我们将对js图片压缩库进行全面详尽的解释,并且为您提供关于ASP.NETMVC中@Html.

如果您想了解gulp之JS、CSS、HTML、图片压缩以及版本更新的相关知识,那么本文是一篇不可错过的文章,我们将对js 图片压缩库进行全面详尽的解释,并且为您提供关于ASP.NET MVC 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction、asp.net – Html.Partial vs Html.RenderPartial&Html.Action vs Html.RenderAction.任何人都可以描述不同之处、asp.net 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction、CS50 HTML 和 CSS 基础 (介绍最简单的 HTML 和 CSS)的有价值的信息。

本文目录一览:

gulp之JS、CSS、HTML、图片压缩以及版本更新(js 图片压缩库)

gulp之JS、CSS、HTML、图片压缩以及版本更新(js 图片压缩库)

之前被grunt的各种配置搞得头大,后来发现居然有gulp这样的好东西,于是就入坑了

创建package.json

npm init

安装gulp

全局安装(因为要使用gulp命令)

npm i gulp -g

项目安装(当然是项目需要咯)

npm i gulp --save-dev

i:install简写
--save-dev:自动添加到devdependencies

安装gulp插件

常用插件

  • gulp-sequence 顺序执行

  • gulp-jshint js语法检测

  • gulp-imagemin 图片压缩

  • imagemin-pngquant 图片压缩(png)

  • gulp-clean-css css压缩

  • gulp-uglify js压缩

  • gulp-htmlmin html压缩(js、css压缩)

  • gulp-minify-html html压缩(js模板压缩)

  • gulp-rev MD5版本号

  • gulp-rev-collector 版本替换

  • gulp-cache 缓存

安装示例

npm i gulp-sequence --save-dev

安装gulp-jshint要注意需要额外安装下jshint

安装好后我们的package.json文件内容就如下所示啦:

{
  "name": "gulp-study",
  "version": "1.0.0",
  "description": "gulp study",
  "main": "gulpfile.js",
  "keywords": [
    "gulp"
  ],
  "author": "xiaomeng",
  "license": "ISC",
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-cache": "^0.4.5",
    "gulp-clean-css": "^2.0.11",
    "gulp-htmlmin": "^2.0.0",
    "gulp-imagemin": "^3.0.2",
    "gulp-jshint": "^2.0.1",
    "gulp-minify-html": "^1.0.6",
    "gulp-rev": "^7.1.0",
    "gulp-rev-collector": "^1.0.5",
    "gulp-sequence": "^0.4.5",
    "gulp-uglify": "^1.5.4",
    "imagemin-pngquant": "^5.0.0",
    "jshint": "^2.9.2"
  }
}

创建gulpfile.js

gulpfile.js文件内容

var gulp            = require("gulp"),
    sequence        = require("gulp-sequence"),//顺序执行
    jsHint          = require("gulp-jshint"),//js语法检测
    minImage        = require("gulp-imagemin"),//图片压缩
    minImageForPng  = require("imagemin-pngquant"),//图片压缩(png)
    minCss          = require("gulp-clean-css"),//css压缩
    minJs           = require("gulp-uglify"),//js压缩
    minHtml         = require("gulp-htmlmin"),//html压缩(js、css压缩)
    minHtmlForJT   = require("gulp-minify-html"),//html压缩(js模板压缩)
    rev             = require("gulp-rev"),//MD5版本号
    revCollector    = require("gulp-rev-collector"),//版本替换
    cache           = require("gulp-cache");//缓存

//配置
var config = {
    //资源文件
    source: {
        //源文件
        src: {
            font:   "src/font/*",
            css:    "src/css/*.css",
            ajaxJs: "src/ajaxJs/*.js",
            js:     "src/js/*.js",
            images: "src/images/*.{png,jpg,gif,ico}",
            html:   "src/html/*.html"
        },
        //MD5版本号文件
        rev: {
            font:   "rev/font/*.json",
            css:    "rev/css/*.json",
            ajaxJs: "rev/ajaxJs/*.json",
            js:     "rev/js/*.json"
        },
        //替换版本后的文件
        revCollector: {
            css:    "revCollector/css/*.css",
            html:   "revCollector/html/*.html"
        }
    },
    //目录
    dir: {
        //MD5版本号文件目录
        rev: {
            font:   "rev/font",
            css:    "rev/css",
            ajaxJs: "rev/ajaxJs",
            js:     "rev/js"
        },
        //替换版本后的文件目录
        revCollector: {
            css: "revCollector/css",
            html: "revCollector/html"
        },
        //正式文件目录
        dist: {
            css:    "dist/css",
            ajaxJs: "dist/ajaxJs",
            js:     "dist/js",
            images: "dist/images",
            html:   "dist/html"
        }
    }
};

//任务
var task = {
    jsHint: "jsHint",
    revFont: "revFont",
    revCss: "revCss",
    revAjaxJs: "revAjaxJs",
    revJs: "revJs",
    revCollectorCss: "revCollectorCss",
    revCollectorHtml: "revCollectorHtml",
    minCss: "minCss",
    minAjaxJs: "minAjaxJs",
    minJs: "minJs",
    minHtml: "minHtml",
    minImage: "minImage"
};


//js语法检测
gulp.task(task.jsHint, function () {
    gulp.src([config.source.src.ajaxJs])
        .pipe(jshint())
        .pipe(jshint.reporter());
});

//MD5版本号
gulp.task(task.revFont, function () {
    return gulp.src(config.source.src.font)
        .pipe(rev())
        .pipe(rev.manifest())
        .pipe(gulp.dest(config.dir.rev.font));
});
gulp.task(task.revCss, function () {
    return gulp.src(config.source.src.css)
        .pipe(rev())
        .pipe(rev.manifest())
        .pipe(gulp.dest(config.dir.rev.css));
});
gulp.task(task.revAjaxJs, function () {
    return gulp.src(config.source.src.ajaxJs)
        .pipe(rev())
        .pipe(rev.manifest())
        .pipe(gulp.dest(config.dir.rev.ajaxJs));
});
gulp.task(task.revJs, function () {
    return gulp.src(config.source.src.js)
        .pipe(rev())
        .pipe(rev.manifest())
        .pipe(gulp.dest(config.dir.rev.js));
});

//版本替换
/**
 *  对插件进行如下修改,使得引用资源文件的url得以如下变换:
 *  "/css/base-f7e3192318.css" >> "/css/base.css?v=f7e3192318"
 *
 *  gulp-rev 1.0.5
 *  node_modules\gulp-rev\index.js
 *  144 manifest[originalFile] = revisionedFile; => manifest[originalFile] = originalFile + ''?v='' + file.revHash;
 *
 *  gulp-rev 1.0.5
 *  node_modules\gulp-rev\node_modules\rev-path\index.js
 *  10 return filename + ''-'' + hash + ext; => return filename + ext;
 *
 *  gulp-rev-collector 7.1.0
 *  node_modules\gulp-rev-collector\index.js
 *  31 if ( !_.isString(json[key]) || path.basename(json[key]).replace(new RegExp( opts.revSuffix ), '''' ) !==  path.basename(key) ) { =>
 *  if ( path.basename(json[key]).split(''?'')[0] !== path.basename(key) ) {
 *
 */
gulp.task(task.revCollectorCss, function () {
    return gulp.src([config.source.rev.font, config.source.src.css])
        .pipe(revCollector())
        .pipe(gulp.dest(config.dir.revCollector.css));
});
gulp.task(task.revCollectorHtml, function () {
    return gulp.src([config.source.rev.css, config.source.rev.ajaxJs, config.source.rev.js, config.source.src.html])
        .pipe(revCollector())
        .pipe(gulp.dest(config.dir.revCollector.html));
});

//压缩文件
gulp.task(task.minCss, function () {
    return gulp.src(config.source.revCollector.css)
        .pipe(minCss())
        .pipe(gulp.dest(config.dir.dist.css));
});
gulp.task(task.minAjaxJs, function () {
    return gulp.src(config.source.src.ajaxJs)
        .pipe(minJs())
        .pipe(gulp.dest(config.dir.dist.ajaxJs));
});
gulp.task(task.minJs, function () {
    return gulp.src(config.source.src.js)
        .pipe(minJs())
        .pipe(gulp.dest(config.dir.dist.js));
});
gulp.task(task.minHtml, function () {
    return gulp.src(config.source.revCollector.html)
        .pipe(minHtmlForJT())//附带压缩页面上的js模板
        .pipe(minHtml({
            removeComments: true,
            collapseWhitespace: true,
            collapseBooleanAttributes: true,
            removeEmptyAttributes: true,
            removeScriptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true,
            minifyJS: true,
            minifyCSS: true
        }))//附带压缩页面上的css、js
        .pipe(gulp.dest(config.dir.dist.html));
});
gulp.task(task.minImage, function () {
    return gulp.src(config.source.src.images)
        .pipe(cache(minImage({
            progressive: true,
            use: [minImageForPng()]
        })))
        .pipe(gulp.dest(config.dir.dist.images));
});


//正式构建
gulp.task("build", sequence(
    //js语法检测
    //[task.jsHint],
    //MD5版本号
    [task.revFont, task.revCss, task.revAjaxJs, task.revJs],
    //版本替换
    [task.revCollectorCss, task.revCollectorHtml],
    //压缩文件
    [task.minCss, task.minAjaxJs, task.minJs, task.minHtml, task.minImage]
));


gulp.task("default", ["build"], function () {

});

运行

执行默认任务

gulp

执行指定任务

gulp 任务名称

其他

目录说明

  • dist

    • ajaxJs

    • css

    • html

    • images

    • js

  • node_modules

  • rev

    • ajaxJs

    • css

    • font

    • js

  • revCollector

    • css

    • html

  • src

    • ajaxJs

    • css

    • html

    • images

    • js

  • gulpfile.js

  • package.json

使用npm安装模块速度有些慢,可以使用淘宝的cnpm

npm install -g cnpm --registry=https://registry.npm.taobao.org

重要的在这里:
cnpm安装的模块用webstorm打开时会造成webstorm卡死(对,我的就是卡死了)
解决方法:百度的(-_-)

ASP.NET MVC 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction

ASP.NET MVC 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction

1.Action、RenderAction加载办法的视图,履行Controller → Model → View的次序,然后把产生的页面带回到本来的View中再回传。而Partial、RenderPartial直接加载视图文件内容

2.Html.Partial可以直接供给用户控件名作为参数,而Html.Action须要有对应的Action,在Action内部返回PartailResult(即retun PartialView())。

3.对于简单的没有任何逻辑的用户控件,推荐应用Html.Partial;对于须要设置一些Model的用户控件,推荐应用Html.Action。当然,有Model数据也是可以应用Html.Partial办法的,可以看办法的重载。

4.Html.Partial与Html.Action有啥区别呢?区别就是,Html.Partial只有一个视图,而Html.Action除了视图,还真的有个Action跟它对应,所以,Html.Action功能比Html.Partial要强。

 

如何调用这个Html.Partial

 //1、以视图名使用当前文件夹下的视图(如果没有找到,则搜索 Shared 文件夹)
@Html.Partial( "_test" //加载对应文件 /Views/Product/_test.cshtml
 
//2、依据应用根路径定位视图// 以 "/" 或 "~/" 开头的路径代表应用根路径
@Html.Partial( "~/Views/Product/_test.cshtml" )
@Html.Partial( "/Views/Product/_test2.cshtml" )
 
//3、加载其他目录的 视图文件
//注意:需要复制views中的web.config 到template目录,否则会提示  "/template/A.cshtml”处的视图必须派生自 WebViewPage 或 WebViewPage<TModel>"
@Html.Partial( "/template/A.cshtml" )

asp.net – Html.Partial vs Html.RenderPartial&Html.Action vs Html.RenderAction.任何人都可以描述不同之处

asp.net – Html.Partial vs Html.RenderPartial&Html.Action vs Html.RenderAction.任何人都可以描述不同之处

在ASP.NET MVC中,有什么区别:

Html.Partial and Html.RenderPartial
Html.Action and Html.RenderAction

解决方法

Html.Action调用控制器的动作,这意味着它实例化控制器实体,调用动作方法,构建模型并返回视图结果.

Html.Partial使用已创建的模型(或者可以在没有模型的情况下调用)来渲染指定的视图.

何时使用一个而不是另一个?如果您已有模型并且只想拥有可重复使用的视图,请选择Html.Partial.如果你看到某个部分值得拥有自己的模型和动作,那么使用Html.Action可能是有意义的.

这个问题在this article中有更详细的讨论,你在上面看到的基本上是它的摘录.

asp.net 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction

asp.net 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction

1、带有Render的方法返回值是void,在方法内部进行输出;不带的返回值类型为MvcHtmlString,所以只能这样使用:

     @Html.Partial 对应 @{Html.RenderPartial(....);}@Html.Action 对应 @{Html.RenderAction(....);}

2、Html.Partial可以直接提供用户控件名作为参数,

    而Html.Action需要有对应的Action,在Action内部返回PartailResult(即retun PartialView())。

3、对于简单的没有任何逻辑的用户控件,推荐使用Html.Partial;对于需要设置一些Model的用户控件,推荐使用Html.Action。当然,有         Model数据也是可以使用Html.Partial方法的,可以看方法的重载。

4、使用Html.Action有个好处,就是可以根据不同的场景选择不同的用户控件。比如:@Html.Action("UserInfoControl")在对应的    UserInfoControl这个Action中,在用户未登录的时候,可以retun PartialView("LogOnUserControl");登录后,可以retun  PartialView("UserInfoControl");

CS50 HTML 和 CSS 基础 (介绍最简单的 HTML 和 CSS)

CS50 HTML 和 CSS 基础 (介绍最简单的 HTML 和 CSS)

最近简单 HTML 

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Hello!</title>
    </head>
    <body>
        Hello, world!
    </body>
<html>

HTML 中 form 表单

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Forms</title>
</head>
<body>
    <form>
        <input type="text" placeholder="First Name" name="first">
        <input type="password" placeholder="Password" name="password">
        <div>
            Favorite Color:
            <input name="color" type="radio" value="blue"> Blue
            <input name="color" type="radio" value="green"> Green
            <input name="color" type="radio" value="yellow"> Yellow
            <input name="color" type="radio" value="red"> Red

        </div>
        <input type="submit">
    </form>
</body>
</html>

相对比较复杂的 HTML

超链接 href

有序列表和无序列表

图片

表格

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>HTML Elements</title>
    </head>
    <body>
        <!-- We can create headings using h1 through h6 as tags. -->
        <h1>A Large Heading</h1>
        <h2>A Smaller Heading</h2>
        <h6>The Smallest Heading</h6>

        <!-- The strong and i tags give us bold and italics respectively. -->
        A <strong>bold</strong> word and an <i>italicized</i> word!

        <!-- We can link to another page (such as cs50''s page) using a. -->
        View the <a href="https://cs50.harvard.edu/">CS50 Website</a>!

        <!-- We used ul for an unordered list and ol for an ordered one. both ordered and unordered lists contain li, or list items. -->
        An unordered list:
        <ul>
            <li>foo</li>
            <li>bar</li>
            <li>baz</li>
        </ul>
        An ordered list:
        <ol>
            <li>foo</li>
            <li>bar</li>
            <li>baz</li>
        </ol>

        <!-- Images require a src attribute, which can be either the path to a file on your computer or the link to an image online. It also includes an alt attribute, which gives a description in case the image can''t be loaded. -->
        An image:
        <img src="../../images/duck.jpeg" alt="Rubber Duck Picture">
        <!-- We can also see above that for some elements that don''t contain other ones, closing tags are not necessary. -->

        <!-- Here, we use a br tag to add white space to the page. -->
        <br/> <br/>

        <!-- A few different tags are necessary to create a table. -->
        <table>
            <thead>
                <th>Ocean</th>
                <th>Average Depth</th>
                <th>Maximum Depth</th>
            </thead>
            <tbody>
                <tr>
                    <td>Pacific</td>
                    <td>4280 m</td>
                    <td>10911 m</td>
                </tr>
                <tr>
                    <td>Atlantic</td>
                    <td>3646 m</td>
                    <td>8486 m</td>
                </tr>
            </tbody>
        </table>
    </body>
<html>

 CSS 简介

在 HTML 中嵌入 CSS

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Hello!</title>
    </head>
    <body>
        <h1 style="color: blue; text-align: center;">A Colorful Heading!</h1>
        Hello, world!
    </body>
<html>

指定整个 body 的样式

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Hello!</title>
    </head>
    <body style="color: blue; text-align: center;">
        <h1 >A Colorful Heading!</h1>
        Hello, world!
    </body>
<html>

将 CSS 放在前面

 <html lang="en">
  <!DOCTYPE html>
  <head>
      <title>Hello!</title>
      <style>
          h1 {
              color: blue;
              text-align: center;
          }
      </style>
  </head>
  <body>
      <h1 >A Colorful Heading!</h1>
      Hello, world!
  </body>
  </html>

单独的 CSS 文件

 h1 {
      color: blue;
      text-align: center;
  }
<html lang="en">
  <!DOCTYPE html>
  <head>
      <title>Hello!</title>
      <link rel="stylesheet" href="styles.css">
  </head>
  <body>
      <h1 >A Colorful Heading!</h1>
      Hello, world!
  </body>
  </html>

给表格添加 CSS

table {
    border: 1px solid black;
    border-collapse: collapse;
}

td {
    border: 1px solid black;
    padding: 2px;
}

th {
    border: 1px solid black;
    padding: 2px;
}
table {
    border: 1px solid black;
    border-collapse: collapse;
}

td, th {
    border: 1px solid black;
    padding: 2px;
}

更复杂的 CSS
 

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Pseudoclasses</title>
        <style>
            button {
                background-color: red;
                width: 200px;
                height: 50px;
                font-size: 24px;
            }

            button:hover {
                background-color: green;
            }
        </style>
    </head>
    <body>
        <button>Button 1</button>
        <button>Button 2</button>
        <button>Button 3</button>

    </body>
<html>

 

关于gulp之JS、CSS、HTML、图片压缩以及版本更新js 图片压缩库的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于ASP.NET MVC 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction、asp.net – Html.Partial vs Html.RenderPartial&Html.Action vs Html.RenderAction.任何人都可以描述不同之处、asp.net 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction、CS50 HTML 和 CSS 基础 (介绍最简单的 HTML 和 CSS)等相关内容,可以在本站寻找。

本文标签: