Gulp Rev不在我的清单文件中包含路径

时间:2015-12-27 23:56:19

标签: javascript node.js gulp gulp-rev

我将Gulp任务设置为一次性连接,缩小和指纹资产,将它们直接写入我的资产文件夹:

(css部分)

gulp.task('styles:cssmin', function(){
    return gulp.src('src/css/*.css')
        .pipe(concat('main.css')) // combine them
        .pipe(cssmin()) // minify them
        .pipe(rev()) // Pipe through gulp-rev to fingerprint generated file
        .pipe(gulp.dest('assets/css'))  // write rev'd assets 
        .pipe(rev.manifest({
            merge: true // Merge because this happens for .js and images too
        }))
        .pipe(gulp.dest('./')); // write manifest
});

这会指纹文件并生成清单,但我无法将任何路径保存到清单文件中。这是它产生的清单:

{
  "bg.jpg": "bg-447ac2238b.jpg",
  "logo.png": "logo-e31e139d4d.png",
  "main-min.js": "main-min-cc3a682299.js",
  "main.css": "main-ea0d06582f.css",
  "main.js": "main-35ec0bb3c8.js"
}

我想拥有:

{
  "assets/img/bg.jpg": "assets/img/bg-447ac2238b.jpg",
  "assets/img/logo.png": "assets/img/logo-e31e139d4d.png",
  "assets/js/main-min.js": "assets/js/main-min-cc3a682299.js",
  "assets/css/main.css": "assets/css/main-ea0d06582f.css",
  "assets/jsg/main.js": "assets/js/main-35ec0bb3c8.js"
}

特别考虑the docs give an example which includes the relative paths

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,最后将流中的资产目录重命名为我想要的并指定rev-manifest的路径。

$ sed '/^[\]section[{]Part 1[}]/, /^Here is some text/d' dat/latex.txt
\maketitle
\section{Part 2}
Here is some more text

答案 1 :(得分:0)

我最终不得不编辑源代码。在那里注释,抱歉我无法突出它。

plugin.manifest = function (pth, opts) {
    if (typeof pth === 'string') {
        pth = {path: pth};
    }

    opts = objectAssign({
        path: 'rev-manifest.json',
        merge: false
    }, opts, pth);

    // Changed the following line to the next line:
    // var firstFileBase = null
    var firstFileBase = opts.base || null;
    var manifest = {};

    return through.obj(function (file, enc, cb) {
        // ignore all non-rev'd files
        if (!file.path || !file.revOrigPath) {
            cb();
            return;
        }

        firstFileBase = firstFileBase || file.base;

        var revisionedFile = relPath(firstFileBase, file.path);
        var originalFile = path.join(path.dirname(revisionedFile), path.basename(file.revOrigPath)).replace(/\\/g, '/');

        manifest[originalFile] = revisionedFile;

        cb();
    }, function (cb) {
        // no need to write a manifest file if there's nothing to manifest
        if (Object.keys(manifest).length === 0) {
            cb();
            return;
        }

        getManifestFile(opts, function (err, manifestFile) {
            if (err) {
                cb(err);
                return;
            }

            if (opts.merge && !manifestFile.isNull()) {
                var oldManifest = {};

                try {
                    oldManifest = JSON.parse(manifestFile.contents.toString());
                } catch (err) {}

                manifest = objectAssign(oldManifest, manifest);
            }

            manifestFile.contents = new Buffer(JSON.stringify(sortKeys(manifest), null, '  '));
            this.push(manifestFile);
            cb();
        }.bind(this));
    });
};

不是理想的解决方案,会在更新时中断。如果有更好的方法,我会喜欢它。