From d207faaf53c8bae5de0dfa0afd773933b6122e1a Mon Sep 17 00:00:00 2001 From: Martijn Cuppens Date: Wed, 19 Feb 2020 11:19:43 +0100 Subject: [PATCH] Config rule per extension (#121) --- README.md | 14 ++++++- lib/.buildozerrc | 14 ++++++- lib/gulp/css.js | 26 +++++++----- lib/gulp/image.js | 45 ++++++++++++++++----- package.json | 1 + test/config-search/nested/dest/css/main.css | 6 ++- test/css/.buildozerrc | 12 ++++++ test/css/dest/css/main.css | 1 + test/css/scss/main.css | 4 ++ 9 files changed, 100 insertions(+), 23 deletions(-) create mode 100644 test/css/.buildozerrc create mode 100644 test/css/dest/css/main.css create mode 100644 test/css/scss/main.css diff --git a/README.md b/README.md index 56084f0..22fd17a 100644 --- a/README.md +++ b/README.md @@ -137,10 +137,22 @@ Buildozer uses a `.buildozerrc` configuration file which uses the yaml syntax an src_base_path: ./ dest_base_path: ./ scss: + - src: scss/**/*.css + dest: dest/css - src: scss/**/*.scss dest: dest/css + - src: scss/**/*.sass + dest: dest/css img: - - src: img/**/*.{png,jpg,jpeg,gif,svg} + - src: img/**/*.png + dest: dest/img + - src: img/**/*.jpg + dest: dest/img + - src: img/**/*.jpeg + dest: dest/img + - src: img/**/*.gif + dest: dest/img + - src: img/**/*.svg dest: dest/img js: - src: js/**/*.js diff --git a/lib/.buildozerrc b/lib/.buildozerrc index ee23fdc..53b662f 100644 --- a/lib/.buildozerrc +++ b/lib/.buildozerrc @@ -1,10 +1,22 @@ src_base_path: ./ dest_base_path: ./ scss: + - src: scss/**/*.css + dest: dest/css - src: scss/**/*.scss dest: dest/css + - src: scss/**/*.sass + dest: dest/css img: - - src: img/**/*.{png,jpg,jpeg,gif,svg} + - src: img/**/*.png + dest: dest/img + - src: img/**/*.jpg + dest: dest/img + - src: img/**/*.jpeg + dest: dest/img + - src: img/**/*.gif + dest: dest/img + - src: img/**/*.svg dest: dest/img js: - src: js/**/*.js diff --git a/lib/gulp/css.js b/lib/gulp/css.js index 1225a67..c72e5f7 100644 --- a/lib/gulp/css.js +++ b/lib/gulp/css.js @@ -87,16 +87,22 @@ function cssCompile({src, dest, cwd, browserSync = false}) { .pipe(gulpIf(file => process.argv.includes('--fix') && file._contents.toString('utf8') !== file.original, gulp.dest(file => file._base))); // Update fixed version differs } - if (process.env.NODE_ENV === 'production') { - stream = stream - .pipe(sass({outputStyle: 'compressed', fiber})) - .pipe(postcss(plugins, options)); - } else { - stream = stream - .pipe(sourcemaps.init()) - .pipe(sass({outputStyle: 'expanded'})) - .pipe(postcss(plugins, options)) - .pipe(sourcemaps.write('.')); + // Init sourcemaps + if (process.env.NODE_ENV !== 'production') { + stream = stream.pipe(sourcemaps.init()); + } + + // We better check for .scss and .sass extentions, but use this for backwards compatibility with <= v0.3.1 + if (!src.endsWith('.css')) { + stream = stream.pipe(sass({outputStyle: 'expanded', fiber})); + } + + // Apply PostCSS plugins + stream = stream.pipe(postcss(plugins, options)); + + // Write sourcemaps + if (process.env.NODE_ENV !== 'production') { + stream = stream.pipe(sourcemaps.write('.')); } stream = stream.pipe(gulp.dest(dest)); diff --git a/lib/gulp/image.js b/lib/gulp/image.js index 98e410a..3805ad6 100644 --- a/lib/gulp/image.js +++ b/lib/gulp/image.js @@ -38,21 +38,46 @@ function imgCompile({src, dest, browserSync = false}) { stream = stream.pipe(plumber()); } - stream = stream.pipe(newer(dest)) - .pipe( - imagemin([ - imagemin.gifsicle({interlaced: true}), - imagemin.mozjpeg({progressive: true}), - imagemin.optipng({optimizationLevel: 5}), - imagemin.svgo({ + const extension = src.split('.').pop(); + + const plugins = (() => { + switch (extension) { + case 'gif': + return imagemin.gifsicle({interlaced: true}); + case 'png': + return imagemin.optipng({optimizationLevel: 5}); + case 'svg': + return imagemin.svgo({ plugins: [ { removeViewBox: false, collapseGroups: true } ] - }) - ], { + }); + case 'jpg': + case 'jpeg': + return imagemin.mozjpeg({progressive: true}); + default: // Required for backwards compatibility with <= v0.3.1 + return [ + imagemin.gifsicle({interlaced: true}), + imagemin.mozjpeg({progressive: true}), + imagemin.optipng({optimizationLevel: 5}), + imagemin.svgo({ + plugins: [ + { + removeViewBox: false, + collapseGroups: true + } + ] + }) + ]; + } + })(); + + stream = stream.pipe(newer(dest)) + .pipe( + imagemin([plugins], { silent: true }) ); @@ -60,7 +85,7 @@ function imgCompile({src, dest, browserSync = false}) { stream = stream.pipe(gulp.dest(dest)); if (browserSync !== false) { - stream = stream.pipe(browserSync.stream({match: '**/*.{png,jpg,jpeg,gif,svg}'})); + stream = stream.pipe(browserSync.stream({match: `**/*.${extension}`})); } stream.on('end', resolve); diff --git a/package.json b/package.json index 0b3c948..22c4041 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "unit-tests": "npm-run-all test-* --parallel", "lint": "xo", "test-default": "cd test/default && node ../../bin/buildozer build", + "test-css": "cd test/css && node ../../bin/buildozer build", "test-env": "cd test/env && node ../../bin/buildozer build --env=development", "test-copy": "cd test/copy && node ../../bin/buildozer build", "test-sass-module": "cd test/sass-module && node ../../bin/buildozer build", diff --git a/test/config-search/nested/dest/css/main.css b/test/config-search/nested/dest/css/main.css index 91a6f41..0ab5801 100644 --- a/test/config-search/nested/dest/css/main.css +++ b/test/config-search/nested/dest/css/main.css @@ -1 +1,5 @@ -body{background:red;appearance:none;font-size:1.25rem} \ No newline at end of file +body { + background: red; + appearance: none; + font-size: 1.25rem; +} \ No newline at end of file diff --git a/test/css/.buildozerrc b/test/css/.buildozerrc new file mode 100644 index 0000000..3dc3c12 --- /dev/null +++ b/test/css/.buildozerrc @@ -0,0 +1,12 @@ +src_base_path: ./ +dest_base_path: ./ +img: [] +js: [] +js-concat: [] +svg-sprite: [] +browsersync: + server: null # Static sites + proxy: null # Dynamic sites + reload: null # Glob to watch for reload +config_search: + enabled: false diff --git a/test/css/dest/css/main.css b/test/css/dest/css/main.css new file mode 100644 index 0000000..90a0ee5 --- /dev/null +++ b/test/css/dest/css/main.css @@ -0,0 +1 @@ +select{-webkit-appearance:none;-moz-appearance:none;appearance:none} \ No newline at end of file diff --git a/test/css/scss/main.css b/test/css/scss/main.css new file mode 100644 index 0000000..7097ac3 --- /dev/null +++ b/test/css/scss/main.css @@ -0,0 +1,4 @@ +select { + /* Comment */ + appearance: none; +}