【问题标题】:Prevent gulp to minify code on local machine防止 gulp 缩小本地机器上的代码
【发布时间】:2015-10-29 08:42:28
【问题描述】:

我使用 gulp 与 [RDash angular dashboard][1] 开始了一个项目。 这是我第一次使用 gulp,问题是我在本地工作时无法调试,因为它会缩小 css/js/html 文件。

在本地工作时如何防止 gulp 缩小?

这是我在 gulpfile.js 上的内容:

var gulp = require('gulp'),
usemin = require('gulp-usemin'),
wrap = require('gulp-wrap'),
connect = require('gulp-connect'),
watch = require('gulp-watch'),
minifyCss = require('gulp-minify-css'),
minifyJs = require('gulp-uglify'),
concat = require('gulp-concat'),
less = require('gulp-less'),
rename = require('gulp-rename'),
minifyHTML = require('gulp-minify-html');

var paths = {
scripts: 'src/js/**/*.*',
styles: 'src/less/**/*.*',
images: 'src/img/**/*.*',
templates: 'src/templates/**/*.html',
index: 'src/index.html',
bower_fonts: 'src/components/**/*.{ttf,woff,eof,svg}',
};

/**
 * Handle bower components from index
   */
   gulp.task('usemin', function() {
return gulp.src(paths.index)
    .pipe(usemin({
        js: [minifyJs(), 'concat'],
        css: [minifyCss({keepSpecialComments: 0}), 'concat'],
    }))
    .pipe(gulp.dest('dist/'));
});

/**
 * Copy assets
   */
gulp.task('build-assets', ['copy-bower_fonts']);

gulp.task('copy-bower_fonts', function() {
return gulp.src(paths.bower_fonts)
    .pipe(rename({
        dirname: '/fonts'
    }))
    .pipe(gulp.dest('dist/lib'));
});

/**
 * Handle custom files
 */
gulp.task('build-custom', ['custom-images', 'custom-js', 'custom-less',         'custom-templates']);

gulp.task('custom-images', function() {
return gulp.src(paths.images)
    .pipe(gulp.dest('dist/img'));
});

gulp.task('custom-js', function() {
return gulp.src(paths.scripts)
    .pipe(minifyJs())
    .pipe(concat('dashboard.min.js'))
    .pipe(gulp.dest('dist/js'));
});

gulp.task('custom-less', function() {
return gulp.src(paths.styles)
    .pipe(less())
    .pipe(gulp.dest('dist/css'));
});

gulp.task('custom-templates', function() {
return gulp.src(paths.templates)
    .pipe(minifyHTML())
    .pipe(gulp.dest('dist/templates'));
});

/**
 * Watch custom files
 */
gulp.task('watch', function() {
gulp.watch([paths.images], ['custom-images']);
gulp.watch([paths.styles], ['custom-less']);
gulp.watch([paths.scripts], ['custom-js']);
gulp.watch([paths.templates], ['custom-templates']);
gulp.watch([paths.index], ['usemin']);
});

/**
 * Live reload server
 */
gulp.task('webserver', function() {
connect.server({
    root: 'dist',
    livereload: true,
    port: 8888
});
});

gulp.task('livereload', function() {
gulp.src(['dist/**/*.*'])
    .pipe(watch())
    .pipe(connect.reload());
});

/**
 * Gulp tasks
 */
gulp.task('build', ['usemin', 'build-assets', 'build-custom']);
gulp.task('default', ['build', 'webserver', 'livereload', 'watch']);

【问题讨论】:

标签: angularjs gulp gulp-minify-css gulp-clean-css


【解决方案1】:

你应该添加 gulp-minify 并按如下方式使用它:

var minify = require('gulp-minify');

gulp.task('compress', function() {
  gulp.src('lib/*.js')
    .pipe(minify({
        exclude: ['tasks'],
        ignoreFiles: ['.combo.js', '-min.js']
    }))
    .pipe(gulp.dest('dist'))
});

将您不想缩小的文件传递给exclude

在您的代码中,您已指定要缩小此处的所有文件:

  gulp.task('usemin', function() {
   return gulp.src(paths.index)
    .pipe(usemin({
        js: [minifyJs(), 'concat'],
        css: [minifyCss({keepSpecialComments: 0}), 'concat'],
    }))
    .pipe(gulp.dest('dist/'));
});

排队

.pipe(usemin({..}) 

只是不要使用这个usemin 任务,你应该没问题

【讨论】:

  • 我在 'var paths = ....' 之后添加了您的代码,将 exclude 更改为:['src/js/**/*.*'] 并且我找不到模块'gulp-minify'。我的代码已经缩小了文件 - 编辑我的代码不是更好吗?另外,如果我添加名为“压缩”的新任务,我不应该将它添加到默认任务或构建吗?
  • 那里,为你调整了答案:)
  • 非常感谢!我停止使用它,但现在看起来 index.html 没有用相关的 JS 文件更新。我认为将它们全部添加到 index.html 将解决问题。谢谢!
  • 很高兴我能帮上忙。如果您满意,您可以用答案标签标记它:)
猜你喜欢
  • 1970-01-01
  • 2019-04-27
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
相关资源
最近更新 更多