简体   繁体   English

脚本文件未加载到 rev-manifest.json (gulp)

[英]script files not loading in rev-manifest.json (gulp)

I have this gulpfile.js where I'm minifying my js files the problem is that when I'm doing gulp build this task is creating the minified js files but not entering the key-value pairs in the rev-manifest.json.我有这个 gulpfile.js,我正在缩小我的 js 文件,问题是当我在做 gulp 构建时,这个任务是创建缩小的 js 文件,但没有在 rev-manifest.json 中输入键值对。

gulp.task('js', function (done) {
console.log('minifying js...');
gulp.src('./assets/**/*.js')
    .pipe(uglify())
    .pipe(rev())
    .pipe(gulp.dest('./public/assets'))
    .pipe(rev.manifest({
        cwd: 'public',
        merge: true
    }))
    .pipe(gulp.dest('./public/assets'));
done()

}); });

I have a similar task for my scss files which converts scss to CSS and then minifies it.我的 scss 文件有一个类似的任务,它将 scss 转换为 CSS 然后将其缩小。 this is working absolutely fine adding proper key-value pairs in the rev-manifest.json这在 rev-manifest.json 中添加正确的键值对绝对正常

gulp.task('css', function (done) {
console.log('minifying css...');
gulp.src('./assets/sass/**/*.scss')
    .pipe(sass())
    .pipe(cssnano())
    .pipe(gulp.dest('./assets.css'));

gulp.src('./assets/**/*.css')
    .pipe(rev())
    .pipe(gulp.dest('./public/assets'))
    .pipe(rev.manifest({
        cwd: 'public',
        merge: true
    }))
    .pipe(gulp.dest('./public/assets'));
done();

}); });

this is what rev-manifest.json looks like这就是 rev-manifest.json 的样子

See it's only adding the css files here but not js files.看到这里只添加 css 文件,而不是 js 文件。 my rev-manifest.json is present inside public/assets/我的 rev-manifest.json 存在于 public/assets/

I just deleted the rev-manifest.js file and build it again and it worked.我刚刚删除了 rev-manifest.js 文件并再次构建它并且它工作。 Took me a day to do this.我花了一天时间来做这件事。 Why God Why.上帝啊,这到底是为什么。

In my case, manifests were not merging, they were getting overwritten.在我的情况下,清单没有合并,它们被覆盖了。 gulp.dest() causes the file to be overwritten. gulp.dest() 导致文件被覆盖。 We indeed have to pass the path of the manifest as parameter before the options if we want the merge to work, here is the working code:如果我们希望合并工作,我们确实必须在选项之前将清单的路径作为参数传递,这是工作代码:

const gulp = require('gulp');

const sass = require('gulp-sass')(require('sass'));
const cssnano = require('gulp-cssnano');
const rev = require('gulp-rev');
const uglify = require('gulp-uglify-es').default;
const imagemin = require('gulp-imagemin');
const del = require('del');

gulp.task('css', function(done){
    console.log('minifying css...');
    gulp.src('./assets/sass/**/*.scss')
    .pipe(sass())
    .pipe(cssnano())
    .pipe(gulp.dest('./assets/'));

    return gulp.src('./assets/**/*.css')
    .pipe(rev())
    .pipe(gulp.dest('./public/assets/'))
    .pipe(rev.manifest('public/assets/rev-manifest.json', {
        base: './public/assets',
        merge: true // merge with the existing manifest (if one exists)
     }))
    .pipe(gulp.dest('./public/assets/'));
    done();
});

gulp.task('js', function (done) {
    console.log('minifying js...');
    gulp.src('./assets/**/*.js')
        .pipe(uglify())
        .pipe(rev())
        .pipe(gulp.dest('./public/assets/'))
        .pipe(rev.manifest('public/assets/rev-manifest.json', {
            base: './public/assets',
            merge: true // merge with the existing manifest (if one exists)
        }))
        .pipe(gulp.dest('./public/assets/'));
    done()
});

gulp.task('images', function(done){
    console.log('compressing images...');
    gulp.src('./assets/**/*.+(png|jpg|gif|svg|jpeg)')
    .pipe(imagemin())
    .pipe(rev())
    .pipe(gulp.dest('./public/assets/'))
    .pipe(rev.manifest('public/assets/rev-manifest.json', {
        base: './public/assets',
        merge: true // merge with the existing manifest (if one exists)
     }))
    .pipe(gulp.dest('./public/assets/'));
    done();
});


// empty the public/assets directory
gulp.task('clean:assets', function(done){
    del.sync('./public/assets');
    done();
});

gulp.task('build', gulp.series('clean:assets', 'css', 'js', 'images'), function(done){
    console.log('Building assets');
    done();
});


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM