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

Commit cfcd386

Browse files
committed
refactor(styles): rm redundant vars, break out addPostCssPlugin
1 parent 0f0401f commit cfcd386

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
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/tasks/stylesheets.js

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +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 projectPath = require('../lib/projectPath')
10-
var postcss = require('gulp-postcss')
11-
var autoprefixer = require('autoprefixer')
12-
var cssnano = require('cssnano')
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 projectPath = require('../lib/projectPath')
10+
var postcss = require('gulp-postcss')
11+
var autoprefixer = require('autoprefixer')
12+
var cssnano = require('cssnano')
13+
var addPostCssPlugin = require('../lib/addPostCssPlugin')
1314

1415
var sassTask = function () {
1516

@@ -24,24 +25,24 @@ var sassTask = function () {
2425
})
2526
}
2627

27-
var autoprefixerConfig = TASK_CONFIG.stylesheets.autoprefixer || {}
28+
TASK_CONFIG.stylesheets.autoprefixer = TASK_CONFIG.stylesheets.autoprefixer || {}
2829

29-
var cssnanoConfig = TASK_CONFIG.stylesheets.cssnano || {}
30-
cssnanoConfig.autoprefixer = false // this should always be false, since we're autoprefixing separately
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
3132

32-
var postcssOptions = TASK_CONFIG.stylesheets.postcss.options || {}
33-
var postcssPlugins = TASK_CONFIG.stylesheets.postcss.plugins || []
33+
TASK_CONFIG.stylesheets.postcss.options = TASK_CONFIG.stylesheets.postcss.options || {}
34+
TASK_CONFIG.stylesheets.postcss.plugins = TASK_CONFIG.stylesheets.postcss.plugins || []
3435

3536
var preprocess = !!TASK_CONFIG.stylesheets.sass
3637

3738
// when watching files, only run once
3839
if (!TASK_CONFIG.stylesheets.configured) {
3940
// ensure Autoprefixer is in the PostCSS config
40-
addPostCssPlugin('autoprefixer', autoprefixer(autoprefixerConfig))
41+
addPostCssPlugin('autoprefixer', autoprefixer(TASK_CONFIG.stylesheets.autoprefixer))
4142

4243
if (global.production) {
4344
// ensure cssnano is in the PostCSS config
44-
addPostCssPlugin('cssnano', cssnano(cssnanoConfig))
45+
addPostCssPlugin('cssnano', cssnano(TASK_CONFIG.stylesheets.cssnano))
4546
}
4647
}
4748

@@ -51,18 +52,11 @@ var sassTask = function () {
5152
.pipe(gulpif(!global.production, sourcemaps.init()))
5253
.pipe(gulpif(preprocess, sass(TASK_CONFIG.stylesheets.sass)))
5354
.on('error', handleErrors)
54-
.pipe(postcss(postcssPlugins, postcssOptions))
55+
.pipe(postcss(TASK_CONFIG.stylesheets.postcss.plugins, TASK_CONFIG.stylesheets.postcss.options))
5556
.on('error', handleErrors)
5657
.pipe(gulpif(!global.production, sourcemaps.write()))
5758
.pipe(gulp.dest(paths.dest))
5859
.pipe(browserSync.stream())
59-
60-
function addPostCssPlugin(name, config) {
61-
let hasPlugin = !!postcssPlugins.find(p => p.postcssPlugin === name)
62-
if (!hasPlugin) {
63-
postcssPlugins.push(config)
64-
}
65-
}
6660
}
6761

6862
const { alternateTask = () => sassTask } = TASK_CONFIG.stylesheets

0 commit comments

Comments
 (0)