Closed sc0rp10 closed 9 years ago
Hey, @sc0rp10. Sorry, I didn't notice your question earlier.
The original files should not be affected. Could you please share the full code you're using?
Hello! Sure, I've created demo repository for demonstrating this problem: https://github.com/sc0rp10/gzip-gulp-demo
Please clone it, and run npm run build
within.
After building, you're got folder build
with only one file: app.css.gz
, but I need two files: app.css
and app.css.gz
.
Is it possbile?
Thanks! That helps clarify.
I recommend adding a second gulp task that copies the files. Then you can run both tasks together to accomplish what you're after.
Something like this:
var gulp = require("gulp");
var gzip = require("gulp-gzip");
gulp.task("default", ["copy", "gzip"]);
gulp.task("copy", function () {
return gulp.src("./src/*")
.pipe(gulp.dest("./build"));
});
gulp.task("gzip", function () {
return gulp.src("./src/*")
.pipe(gzip())
.pipe(gulp.dest("./build"));
});
@jstuckey thanks for solution, but it adds some complicate to my workflow. My task looks like this:
gulp.task("minify-css", function() {
return gulp.src(SOURCE_POSTCSS_GLOB)
.pipe(plugins.concat(CONCATENATED_POSTCSS_FILE))
.pipe(plugins.postcss(postcss_plugins))
.pipe(plugins.rename({
extname: ".css"
}))
.pipe(plugins.shorthand())
.pipe(plugins.csso())
.pipe(plugins.minify_css())
.pipe(plugins.rename(MINIFIED_CSS_FILE))
.pipe(plugins.gzip())
.pipe(gulp.dest(BUILD_CSS_PATH));
});
i.e. I haven't original file in other task - it's be generated by some pipes queue. And there will be nice option like 'dont remove original files' in gzip() function. Maybe I can make PR with it. What do you think about it?
P.S. Original files needed by http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html - without it this module won't work and returns 404 to each .css.gz file without .css in same dir.
Would this work?
gulp.task("default", ["minify-css", "gzip-css"]);
gulp.task("minify-css", function() {
return gulp.src(SOURCE_POSTCSS_GLOB)
.pipe(plugins.concat(CONCATENATED_POSTCSS_FILE))
.pipe(plugins.postcss(postcss_plugins))
.pipe(plugins.rename({
extname: ".css"
}))
.pipe(plugins.shorthand())
.pipe(plugins.csso())
.pipe(plugins.minify_css())
.pipe(plugins.rename(MINIFIED_CSS_FILE))
.pipe(gulp.dest(BUILD_CSS_PATH));
});
gulp.task("gzip-css", function() {
return gulp.src(BUILD_CSS_GLOB)
.pipe(plugins.gzip())
.pipe(gulp.dest(BUILD_CSS_PATH));
});
Where BUILD_CSS_GLOB
refers to the minified css files in your build directory.
I'm newbie in gulp, but I think that gzip-css
may start earlier than minify-css
ends and result will be unpredictable. And plugins.gzip()
still removes BUILD_CSS_GLOB file.
IMO, single option 'keep_original' in gzip() will be more intuitive, than additional task with own limitations.
Try outputting the file with gulp.dest()
before you gzip it.
gulp.task("minify-css", function() {
return gulp.src(SOURCE_POSTCSS_GLOB)
.pipe(plugins.concat(CONCATENATED_POSTCSS_FILE))
.pipe(plugins.postcss(postcss_plugins))
.pipe(plugins.rename({
extname: ".css"
}))
.pipe(plugins.shorthand())
.pipe(plugins.csso())
.pipe(plugins.minify_css())
.pipe(plugins.rename(MINIFIED_CSS_FILE))
.pipe(gulp.dest(BUILD_CSS_PATH));
.pipe(plugins.gzip())
.pipe(gulp.dest(BUILD_CSS_PATH));
});
@thebuilder i tried it - you can see it in my test repo below (i've updated it). If i call gzip after dest in my build
dir contains only css file, not css.gz.
@thebuilder ahh, I'm very inattentive, and didn't see you point. It looks like a nice solution, thanks!
Is that working for you, @sc0rp10?
I was reluctant to add a keep_original
option because gulp tends to favor composing plugins over adding configuration options. This behavior seemed like something that could be accomplished without adding another option to gulp-gzip. It also seemed odd to output two files when only one is input.
Thanks, @thebuilder, for an idiomatic-gulp way of accomplishing this!
@jstuckey it sounds very logical. With @thebuilder method this option will be useless. Thank, guys!
Great! :tada:
Hello. How can I keep original files after
.pipe(gzip())
? Maybe is there special zlib option? Thanks.