Gulp混淆压缩代码
来源:互联网 发布:云数据一体机能贷款吗 编辑:程序博客网 时间:2024/05/20 19:31
这是一个使用Ionic创建的AngularJS单页面应用的代码混淆压缩, 仅供参考
包含以下功能:
1. js压缩
2. html压缩
3. 图片压缩
4. css, sass文件压缩及监听
完整的gulpfile.js如下
var gulp = require('gulp');var gutil = require('gulp-util');var sass = require('gulp-sass');var minifyCss = require('gulp-minify-css');var rename = require('gulp-rename');var sh = require('shelljs');var jshint = require('gulp-jshint');var del = require('del');var uglify = require('gulp-uglify');var imagemin = require('gulp-imagemin');var templateCache = require('gulp-angular-templatecache');var ngAnnotate = require('gulp-ng-annotate');var useref = require('gulp-useref');var rev = require('gulp-rev');var revReplace = require('gulp-rev-replace');var gulpif = require('gulp-if');var paths = { clean: ['build', '.tmp'], sass: ['./scss/**/*.scss'], templatecache: ['./www/{templates,scripts}/**/*.html'], ng_annotate: ['./www/{js,scripts}/**/*.js'], useref: ['./www/*.html'], rev: ['./build/**/*.css', './build/**/*.js'], copy: ['./www/**/*.{eot,ico,ttf,woff,woff2,webp,svg}']};//main taskgulp.task('default', ['clean'], function(){ gulp.start('imagemin', 'revreplace', 'copy');});//Clean targetgulp.task('clean', function(){ del(paths.clean).then(function(){ console.log('Deleted files and folders:\n', paths.join('\n')); });});//compress PNG/SVG/JPEG/GIFgulp.task('imagemin', function() { return gulp.src('www/**/*.{png,jpg,gif,jpeg}') .pipe(imagemin({ optimizationLevel: 5, //类型:Number 默认:3 取值范围:0-7(优化等级) progressive: true, //类型:Boolean 默认:false 无损压缩jpg图片 interlaced: true, //类型:Boolean 默认:false 隔行扫描gif进行渲染 multipass: true //类型:Boolean 默认:false 多次优化svg直到完全优化 })) .pipe(gulp.dest('build'));});//将html压缩成js文件gulp.task('templatecache', function() { return gulp.src(paths.templatecache) .pipe(templateCache({standalone:true})) .pipe(gulp.dest('.tmp/'));});//使用ng-annotate进行angular模块依赖自动注入, 引入模块自动加[],防止被混淆gulp.task('ng_annotate', ['templatecache'], function() { return gulp.src(paths.ng_annotate) .pipe(ngAnnotate({single_quotes: true})) .pipe(gulp.dest('.tmp/'));});//replace index block, searchPath指定搜索压缩后的引用文件地址gulp.task('useref', ['ng_annotate'], function(){ return gulp.src(paths.useref) .pipe(useref({ searchPath: ['./www', './tmp'] })) .pipe(gulpif('*.js', uglify())) .pipe(gulpif('*.css', minifyCss())) .pipe(gulp.dest('./build'));});//Static asset revisioning by appending content hash to filenames: unicorn.css => unicorn-d41d8cd98f.cssgulp.task('revision', ['useref'], function(){ return gulp.src(paths.rev) .pipe(rev()) .pipe(gulp.dest('./build')) .pipe(rev.manifest()) .pipe(gulp.dest('./build'));});//replace indexgulp.task('revreplace', ['revision'], function(){ var manifest = gulp.src('build/rev-manifest.json'); return gulp.src('build/index.html') .pipe(revReplace({manifest: manifest})) .pipe(gulp.dest('build/'));});//copy othersgulp.task('copy', function () { return gulp.src(paths.copy) .pipe(gulp.dest('./build'));});gulp.task('sass', function(done) { gulp.src('./scss/ionic.app.scss') .pipe(sass({ errLogToConsole: true })) .pipe(gulp.dest('./www/css/')) .pipe(minifyCss({ keepSpecialComments: 0 })) .pipe(rename({ extname: '.min.css' })) .pipe(gulp.dest('./www/css/')) .on('end', done);});gulp.task('watch', function() { gulp.watch(paths.sass, ['sass']);});gulp.task('install', ['git-check'], function() { return bower.commands.install() .on('log', function(data) { gutil.log('bower', gutil.colors.cyan(data.id), data.message); });});gulp.task('git-check', function(done) { if (!sh.which('git')) { console.log( ' ' + gutil.colors.red('Git is not installed.'), '\n Git, the version control system, is required to download Ionic.', '\n Download git here:', gutil.colors.cyan('http://git-scm.com/downloads') + '.', '\n Once git is installed, run \'' + gutil.colors.cyan('gulp install') + '\' again.' ); process.exit(1); } done();});//Grammer checkgulp.task('jshint', function(){ return gulp.src(['www/js/*.js']) .pipe(jshint()) .pipe(jshint.reporter('fail'));});
依赖包配置文件package.json如下:
{ "name": "myapp", "version": "1.0.0", "description": "myApp: An Ionic project", "dependencies": { "gulp": "^3.5.6", "gulp-sass": "^1.3.3", "gulp-concat": "^2.2.0", "gulp-minify-css": "^0.3.0", "gulp-rename": "^1.2.0" }, "devDependencies": { "bower": "^1.3.3", "gulp-util": "^2.2.14", "shelljs": "^0.3.0", "gulp-angular-templatecache": "^1.8.0", "gulp-ng-annotate": "^1.1.0", "gulp-useref": "^3.0.2", "gulp-rev": "^6.0.1", "gulp-rev-replace": "^0.4.2", "gulp-if": "^2.0.0", "del": "^2.1.0" }, "cordovaPlugins": [ "cordova-plugin-device", "cordova-plugin-console", "cordova-plugin-whitelist", "cordova-plugin-splashscreen", "com.ionic.keyboard" ], "cordovaPlatforms": []}
https://www.npmjs.com/
npm包黑名单:
https://github.com/gulpjs/plugins/blob/master/src/blackList.json#L4
0 0
- Gulp混淆压缩代码
- gulp 压缩合并Css 混淆压缩js
- gulp代码压缩管理工具
- gulp压缩js代码
- android代码压缩混淆
- 使用gulp压缩合并代码
- 【gulp】JS文件的压缩混淆与雪碧图
- uglifyjs混淆压缩js代码
- 使用gulp压缩并合并AngularJS代码
- gulp代码压缩和制作雪碧图
- gulp 整合、压缩js、css代码
- ionic代码压缩与代码混淆
- 压缩代码和资源-ProGuard,代码混淆
- ionic利用gulp混淆代码与编译热更新
- gulp的实时刷新、代码压缩、代码排错
- 用UglifyJS2合并压缩混淆JS代码
- 用UglifyJS2合并压缩混淆JS代码
- node.js 压缩混淆js代码
- HDU2047阿牛的EOF牛肉串
- ERP与MES
- 装饰者模式
- 二进制位运算
- 解决Wget下载时的乱码问题
- Gulp混淆压缩代码
- 服务器端极光推送2种方式
- ios开发如何封装AFN第三方类库中的http请求
- 仿照微信首页下拉拍照的demo
- java 读写文件
- Android 基础UI Demo小合集
- 前言
- iOS开发-使用Storyboard进行界面跳转及传值
- DLL生成与使用