Home > Web Front-end > JS Tutorial > How to use gulp to automatically optimize requireJS projects

How to use gulp to automatically optimize requireJS projects

php中世界最好的语言
Release: 2018-03-09 16:07:27
Original
1762 people have browsed it

This time I will show you how to use gulp to automatically optimize requireJS projects, and what are the precautions for using gulp to automatically optimize requireJS projects. The following is a practical case. Get up and take a look.

{  
  "name": "gulp-requireDemo",  
  "version": "0.0.0",  
  "scripts": {  
    "test": "echo \"Error: no test specified\" && exit 1"  
  },  
  "author": "feier",  
  "license": "ISC",  
  "dependencies": {  
    "amd-optimize": "^0.4.3",                   //关键文件 与gulp与require集成  
    "gulp": "^3.8.10",      //gulp主文件  
    "gulp-concat": "^2.4.2",   //文件合并  
    "gulp-imagemin": "^2.0.0",                  //图片压缩  
    "gulp-jshint": "^1.9.0",//js检查  
    "gulp-less": "^2.0.1",               //less编译  
    "gulp-minify-css": "^0.3.11",//css压缩  
    "gulp-rename": "^1.2.0",//重命名  
    "gulp-uglify": "^1.0.2",//文件压缩  
    "imagemin-pngcrush": "^4.0.0"  
  },  
  "main": "index.js",  
  "directories": {  
    "test": "test"  
  },  
}
Copy after login

gulpfile.js file

//引入gulp  
var gulp = require('gulp');  
  
  
//引入组件  
var concat = require('gulp-concat');//合并  
var jshint = require('gulp-jshint'); //js规范验证  
var uglify = require('gulp-uglify');//压缩  
var rename = require('gulp-rename');  //文件名命名  
var amdOptimize = require("amd-optimize");  //require优化  
var watch = require('gulp-watch');  
//脚本检查  gulp.task('lint', function () {  
    gulp.src('./src/js/**/*.js')  
        .pipe(jshint())  
        .pipe(jshint.reporter('default'));  
});  
//require合并  
gulp.task('rjs', function () {  
    gulp.src('./src/js/**/*.js')  
        .pipe(amdOptimize("main", { //require config  
            paths: {  
                "jquery": "../../libs/jquery/dist/jquery.min",  
                "jquery.serializeJSON": "../../libs/jquery.serializeJSON/jquery.serializejson.min",  
                "sug": "src/js/suggestion/suggestion",  
                "validate": "../util/src/js/util/validate",  
                "urlParam": "../util/src/js/util/url.param"  
            },  
            shim: {  
                "jquery.serializeJSON": ['jquery']  
            }  
        }))  
        .pipe(concat("index.js"))           //合并  
        .pipe(gulp.dest("dist/js"))          //输出保存  
        .pipe(rename("index.min.js"))          //重命名  
        .pipe(uglify())                        //压缩  
        .pipe(gulp.dest("dist/js"));         //输出保存  });  
gulp.task('default', function () {  
    //监听js变化  
    gulp.watch('./src/js/**/*.js', function () {       //当js文件变化后,自动检验 压缩  
        //gulp.run('lint', 'scripts');  
        gulp.run('lint', 'rjs');  
    });  
});
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to the php Chinese website Other related articles!

Related reading:

How to set the 360 ​​speed mode to be turned on by default

How to make Vue Es6 parse to Es5 syntax

The above is the detailed content of How to use gulp to automatically optimize requireJS projects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template