Skip to content
This repository was archived by the owner on Sep 15, 2023. It is now read-only.

Commit 73158fe

Browse files
committed
Merge branch 'postcss' (PR #582)
2 parents d4e1d67 + 66b9758 commit 73158fe

File tree

5 files changed

+760
-492
lines changed

5 files changed

+760
-492
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = function addPostCssPlugin(name, config) {
2+
let hasPlugin = !!TASK_CONFIG.stylesheets.postcss.plugins.find(p => p.postcssPlugin === name)
3+
if (!hasPlugin) {
4+
TASK_CONFIG.stylesheets.postcss.plugins.push(config)
5+
}
6+
}

gulpfile.js/lib/task-defaults.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,17 @@ module.exports = {
4040
"./node_modules"
4141
]
4242
},
43-
extensions: ["sass", "scss", "css"]
43+
extensions: ["sass", "scss", "css", "pcss"],
44+
cssnano: {
45+
// deprecated. configure cssnano in stylesheets.postcss.plugins
46+
},
47+
postcss: {
48+
plugins: [
49+
// Autoprefixer and cssnano are added automatically,
50+
// with default settings, if not given custom configuration here
51+
],
52+
options: {}
53+
}
4454
},
4555

4656
html: {

gulpfile.js/tasks/stylesheets.js

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
if(!TASK_CONFIG.stylesheets) return
22

3-
var gulp = require('gulp')
4-
var gulpif = require('gulp-if')
5-
var browserSync = require('browser-sync')
6-
var sass = require('gulp-sass')
7-
var sourcemaps = require('gulp-sourcemaps')
8-
var handleErrors = require('../lib/handleErrors')
9-
var autoprefixer = require('gulp-autoprefixer')
10-
var projectPath = require('../lib/projectPath')
11-
var cssnano = require('gulp-cssnano')
3+
var gulp = require('gulp')
4+
var addPostCssPlugin = require('../lib/addPostCssPlugin')
5+
var autoprefixer = require('autoprefixer')
6+
var browserSync = require('browser-sync')
7+
var cssnano = require('cssnano')
8+
var gulpif = require('gulp-if')
9+
var handleErrors = require('../lib/handleErrors')
10+
var postcss = require('gulp-postcss')
11+
var projectPath = require('../lib/projectPath')
12+
var sass = require('gulp-sass')
13+
var sourcemaps = require('gulp-sourcemaps')
1214

1315
var sassTask = function () {
1416

@@ -17,21 +19,41 @@ var sassTask = function () {
1719
dest: projectPath(PATH_CONFIG.dest, PATH_CONFIG.stylesheets.dest)
1820
}
1921

20-
if(TASK_CONFIG.stylesheets.sass && TASK_CONFIG.stylesheets.sass.includePaths) {
22+
if (TASK_CONFIG.stylesheets.sass && TASK_CONFIG.stylesheets.sass.includePaths) {
2123
TASK_CONFIG.stylesheets.sass.includePaths = TASK_CONFIG.stylesheets.sass.includePaths.map(function(includePath) {
2224
return projectPath(includePath)
2325
})
2426
}
2527

26-
var cssnanoConfig = TASK_CONFIG.stylesheets.cssnano || {}
27-
cssnanoConfig.autoprefixer = false // this should always be false, since we're autoprefixing separately
28+
TASK_CONFIG.stylesheets.autoprefixer = TASK_CONFIG.stylesheets.autoprefixer || {}
29+
30+
TASK_CONFIG.stylesheets.cssnano = TASK_CONFIG.stylesheets.cssnano || {}
31+
TASK_CONFIG.stylesheets.cssnano.autoprefixer = false // this should always be false, since we're autoprefixing separately
32+
33+
TASK_CONFIG.stylesheets.postcss.options = TASK_CONFIG.stylesheets.postcss.options || {}
34+
TASK_CONFIG.stylesheets.postcss.plugins = TASK_CONFIG.stylesheets.postcss.plugins || []
35+
36+
var preprocess = !!TASK_CONFIG.stylesheets.sass
37+
38+
// when watching files, only run once
39+
if (!TASK_CONFIG.stylesheets.configured) {
40+
// ensure Autoprefixer is in the PostCSS config
41+
addPostCssPlugin('autoprefixer', autoprefixer(TASK_CONFIG.stylesheets.autoprefixer))
42+
43+
if (global.production) {
44+
// ensure cssnano is in the PostCSS config
45+
addPostCssPlugin('cssnano', cssnano(TASK_CONFIG.stylesheets.cssnano))
46+
}
47+
}
48+
49+
TASK_CONFIG.stylesheets.configured = true
2850

2951
return gulp.src(paths.src)
3052
.pipe(gulpif(!global.production, sourcemaps.init()))
31-
.pipe(sass(TASK_CONFIG.stylesheets.sass))
53+
.pipe(gulpif(preprocess, sass(TASK_CONFIG.stylesheets.sass)))
54+
.on('error', handleErrors)
55+
.pipe(postcss(TASK_CONFIG.stylesheets.postcss.plugins, TASK_CONFIG.stylesheets.postcss.options))
3256
.on('error', handleErrors)
33-
.pipe(autoprefixer(TASK_CONFIG.stylesheets.autoprefixer))
34-
.pipe(gulpif(global.production, cssnano(cssnanoConfig)))
3557
.pipe(gulpif(!global.production, sourcemaps.write()))
3658
.pipe(gulp.dest(paths.dest))
3759
.pipe(browserSync.stream())

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,26 @@
2525
},
2626
"dependencies": {
2727
"ansi-colors": "^3.2.4",
28+
"autoprefixer": "^9.6.0",
2829
"babel-core": "^6.26.3",
2930
"babel-loader": "^7.1.1",
3031
"babel-preset-env": "^1.7.0",
3132
"babel-preset-stage-1": "^6.24.1",
3233
"browser-sync": "^2.26.3",
34+
"cssnano": "^4.1.10",
3335
"del": "4.0.0",
3436
"es6-promise": "^4.2.6",
3537
"fancy-log": "^1.3.3",
3638
"gulp": "3.9.1",
37-
"gulp-autoprefixer": "6.0.0",
3839
"gulp-changed": "^3.2.0",
39-
"gulp-cssnano": "2.1.3",
4040
"gulp-data": "1.3.1",
4141
"gulp-gh-pages": "0.5.4",
4242
"gulp-htmlmin": "5.0.1",
4343
"gulp-if": "2.0.2",
4444
"gulp-notify": "^3.2.0",
4545
"gulp-nunjucks-render": "^2.2.2",
46-
"gulp-rename": "1.4.0",
46+
"gulp-postcss": "^8.0.0",
47+
"gulp-rename": "^1.4.0",
4748
"gulp-replace": "^1.0.0",
4849
"gulp-rev": "9.0.0",
4950
"gulp-rev-delete-original": "^0.2.3",

0 commit comments

Comments
 (0)