Reputation: 1663
I have Gulp tasks set up to concat, minify and fingerprint assets all at once, writing them directly to my assets folder:
(the css part)
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
});
This fingerprints the files and generates a manifest, but I can't get any paths to be saved to the manifest file. Here's the manifest it generates:
{
"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"
}
Which I would like to have as:
{
"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"
}
Especially considering the docs give an example which includes the relative paths
Upvotes: 5
Views: 1992
Reputation: 880
The right answer is old. But still I figured out from that. For new versions of plugins it must be slightly different. By the way, I'm using it in Symfony v6.
const paths = {
styles: {
src: '../assets/site/scss/**/*.scss',
dest: '../public/site/css/'
},
scripts: {
src: '../assets/site/lib/*.js',
dest: '../public/site/js/'
},
revision: {
manifest: '../assets/site/rev-manifest.json',
dest_manifest: '../assets/site/',
dest: '../public/site/'
},
templates: {
src: '../templates/site/**/*.html.twig'
}
};
export function styles() {
return gulp.src(paths.styles.src, {base: '../assets'})
.pipe(sass.sync({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(autoprefixer('last 2 version', 'safari 5', 'ie 7', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
.pipe(concat('main.css'))
.pipe(gulp.dest(paths.styles.dest))
.pipe(rename({dirname: "css"}))
.pipe(rev())
.pipe(gulp.dest(paths.revision.dest))
.pipe(rev.manifest(paths.revision.manifest, {
base: paths.revision.dest_manifest,
merge: true
}))
.pipe(gulp.dest(paths.revision.dest_manifest))
.pipe(livereload());
}
My Dependencies:
"devDependencies": {
"del": "^7.0.0",
"gulp": "^4.0.2",
"gulp-autoprefixer": "^8.0.0",
"gulp-concat": "^2.6.1",
"gulp-livereload": "^4.0.2",
"gulp-rename": "^2.0.0",
"gulp-rev": "^10.0.0",
"gulp-sass": "^5.1.0",
"gulp-sourcemaps": "^3.0.0",
"gulp-uglify": "^3.0.2",
"sass": "^1.58.0"
},
"dependencies": {
"bootstrap": "^5.2.3"
}
Upvotes: 0
Reputation: 106
I was having the same issue, and ended up renaming the asset dirs in the stream to what I wanted and specifying the path of the rev-manifest.
var rename = require("gulp-rename");
gulp.task('styles:cssmin', function(){
return gulp.src('src/css/*.css')
.pipe(concat('main.css')) // combine them
.pipe(cssmin()) // minify them
.pipe(rename({
dirname: "assets/css" //manually fixing path for rev-manifest
}))
.pipe(rev('path/to/your/rev/rev-manifest.json')) // Specify manifest location
.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
});
Upvotes: 6
Reputation: 1663
I ended up having to edit the source. Annotated in there, sorry I can't highlight it.
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));
});
};
Not an ideal solution, will break on update. I would love it if there were a better way.
Upvotes: 0