-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyles.js
219 lines (196 loc) · 5.32 KB
/
styles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict';
/**
* External dependencies
*/
import {
src,
dest,
} from 'gulp';
import newer from 'gulp-newer';
import phpcs from 'gulp-phpcs';
import postcss from 'gulp-postcss';
import gulpIf from 'gulp-if';
import tabify from 'gulp-tabify';
import rename from 'gulp-rename';
import postcssPresetEnv from 'postcss-preset-env';
import AtImport from 'postcss-import';
import cssnano from 'cssnano';
import stylelint from 'stylelint';
import reporter from 'postcss-reporter';
import calc from 'postcss-calc';
import { pipeline } from 'mississippi';
/**
* Internal dependencies
*/
import {
paths,
isProd,
PHPCSOptions,
} from './constants';
import {
getMainConfig,
logError,
} from './utils';
import {
mapPath,
sourcemaps,
startStringReplace,
} from './helper/base';
import { server } from './browserSync';
/** The user configuration. */
const config = getMainConfig( isProd );
/**
* Returns a single stream code sniffing using WPCS.
*
* Logs all rules that that code violates.
*
* @param {boolean} editor Whether sniff for editor style.
* @return {Object} Pumpify pipeline object.
* @link https://github.com/JustBlackBird/gulp-phpcs
*/
export const sniffStyle = ( editor = false ) => {
return pipeline.obj( [
logError( editor ? 'CSS' : 'Editor CSS' ),
newer( {
dest: paths.styles.dest,
extra: [ paths.config.mainConfig ],
} ),
phpcs( {
bin: PHPCSOptions.bin,
standard: 'WordPress',
warningSeverity: 0,
} ),
phpcs.reporter( 'log' ),
] );
};
/** Post CSS Preset Environment Import. */
export const getDevStyleImport = config?.dev?.styles?.importFrom ?
mapPath( config.dev.styles.importFrom, paths.styles.srcDir ) :
[];
/**
* Gets Post CSS Preset Environment Features.
*
* @param {boolean} custom Whether custom properties to be preserved.
* This must be false for the editor.
* @return {Object<Object|boolean>} The features as an object.
*/
export const getDevStyleFeatures = ( custom ) => {
return {
'custom-media-queries': { preserve: false },
'custom-properties': { preserve: custom },
'nesting-rules': true,
};
};
/**
* Gets Post CSS Preset Environment.
*
* @param {boolean} editor Whether env for style editor.
* @param {boolean} preserveCustom Set preserve custom properties.
* @return {Object} The environment option.
* @link https://github.com/csstools/postcss-plugins/tree/main/plugin-packs/postcss-preset-env
*/
export const getPresetEnv = ( editor = false, preserveCustom = true ) => {
const preset = {
/**
* By default, `_custom-media.css` is imported.
*
* @link https://github.com/csstools/postcss-plugins/tree/main/plugin-packs/postcss-preset-env#importfrom
*/
importFrom: getDevStyleImport,
/**
* Features to implement.
*
* 0 => experimental, 4 => stable.
* For specific polyfills, see `features` property.
*/
stage: config?.dev?.styles?.stage ?? 3,
/**
* By default, following features are enabled.
*
* - `custom-media-queries`
* - `custom-properties`
* - `nesting-rules`
*
* @link https://github.com/csstools/postcss-plugins/tree/main/plugin-packs/postcss-preset-env#features
* @link https://preset-env.cssdb.org/features/
*/
features: config?.dev?.styles?.features ?? getDevStyleFeatures( preserveCustom ),
/**
* By default, disabled grid support for IE 10-11.
*
* Learn more from following links:
*
* @link https://github.com/postcss/autoprefixer#options
* @link https://github.com/postcss/autoprefixer#does-autoprefixer-polyfill-grid-layout-for-ie.
*/
autoprefixer: config?.dev?.styles?.autoprefixer ?? {},
};
if ( editor ) {
/**
* For editor stylesheet, remove polyfilled CSS by other plugins.
*
* For eg: by default, remove for `editor-styles.css`.
*
* @link https://github.com/csstools/postcss-plugins/tree/main/plugin-packs/postcss-preset-env#preserve
*/
preset.preserve = false;
}
return preset;
};
/**
* PostCSS At Import property.
*
* Makes sure custom import feature for CSS to work.
*/
export const getPostCSSAtImport = AtImport( {
path: [ paths.styles.srcDir ],
plugins: [ stylelint() ],
} );
/**
* Processes styles.
*
* - Apply stylelint.
* - PostCSS preset env (imports stylesheet files).
* - Minify CSS.
*
* @return {Object} Pumpify pipeline object.
*/
export const processStyle = () => {
const postcssPlugins = [
stylelint(),
postcssPresetEnv( getPresetEnv() ),
calc( { preserve: false } ),
cssnano(),
];
// Skip minifying files during development and debug is enabled.
if ( config.dev.debug.styles && ! isProd ) {
postcssPlugins.pop(); // Unset cssnano().
}
// Report messages from other postcss plugins.
postcssPlugins.push(
reporter( { clearReportedMessages: true } )
);
// Single stream with postCSS events.
return pipeline.obj( [
postcss( [ getPostCSSAtImport ] ),
postcss( postcssPlugins ),
gulpIf( config.dev.debug.styles, tabify( 2, true ) ),
rename( { suffix: '.min' } ),
server.stream( { match: '**/*.css' } ),
] );
};
/**
* Lints CSS via PostCSS and CSSNext.
*
* Includes Autoprefixer by default.
*
* @return {NodeJS.ReadWriteStream} NodeJS stream.
*/
const styles = () => {
return src( paths.styles.src, { sourcemaps } )
.pipe( sniffStyle() )
.pipe( processStyle() )
.pipe( gulpIf( isProd, startStringReplace() ) )
.pipe( dest( paths.styles.dest, { sourcemaps } ) );
};
export default styles;