-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
179 lines (163 loc) · 4.96 KB
/
gulpfile.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
/**
* WordPress plugin-specific gulp file.
*
* Instructions
*
* In command line, cd into the project directory and run these commands:
* npm init
* sudo npm install --save-dev gulp gulp-util gulp-load-plugins browser-sync
* sudo npm install --save-dev gulp-sourcemaps gulp-autoprefixer gulp-line-ending-corrector gulp-filter gulp-merge-media-queries gulp-cssnano gulp-sass gulp-concat gulp-uglify gulp-notify gulp-imagemin gulp-rename gulp-wp-pot gulp-sort fs path merge-stream gulp-wp-pot
*
* Implements:
* 1. Live reloads browser with BrowserSync.
* 2. CSS: Sass to CSS conversion, error catching, Autoprixing, Sourcemaps,
* CSS minification, and Merge Media Queries.
* 3. JS: Concatenates & uglifies Vendor and Custom JS files.
* 4. Images: Minifies PNG, JPEG, GIF and SVG images.
* 5. Watches files for changes in CSS or JS.
* 6. Watches files for changes in PHP.
* 7. Corrects the line endings.
* 8. InjectCSS instead of browser page reload.
* 9. Generates .pot file for i18n and l10n.
*
* @since 1.0.0
* @author Ahmad Awais (@mrahmadawais) and Chris Wilcoxson (@slushman)
*/
/**
* Project Configuration for gulp tasks.
*/
var project = {
'name': 'wp-style-guide',
'url': 'themes.dev',
'i18n': {
'domain': 'wp-style-guide',
'destFile': 'wp-style-guide.pot',
'package': 'WP_Style_Guide',
'bugReport': 'http://www.dccmarketing.com/contact',
'translator': 'Chris Wilcoxson <[email protected]>',
'lastTranslator': 'DCC Marketing <[email protected]>',
'path': './assets/languages',
}
};
var images = {
'source': './images/*.{png,jpg,gif,svg}',
'dest': './images/'
}
var watch = {
'styles': {
'dest': './assets/css/',
'source': './src/sass/**/*.scss',
'task' : 'styles'
},
'php': './*.php'
}
var tasks = [];
var zipFiles = [ './**',
'!node_modules/**/*',
'!src/**/*',
'!.git/**/*',
'!node_modules',
'!src',
'!.git',
'!*.zip' ];
/**
* Browsers you care about for autoprefixing.
*/
const AUTOPREFIXER_BROWSERS = [
'last 2 version',
'> 1%',
'ie >= 9',
'ie_mob >= 10',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4',
'bb >= 10'
];
/**
* Load gulp plugins and assing them semantic names.
*/
var gulp = require( 'gulp' ); // Gulp of-course
var plugins = require( 'gulp-load-plugins' )();
var browserSync = require( 'browser-sync' ).create(); // Reloads browser and injects CSS.
var reload = browserSync.reload; // For manual browser reload.
var path = require( 'path' );
/**
* Live Reloads, CSS injections, Localhost tunneling.
*
* @link http://www.browsersync.io/docs/options/
*/
gulp.task( 'browser-sync', function() {
browserSync.init({
proxy: project.url,
open: true,
injectChanges: true,
browser: "google chrome"
});
});
/**
* Creates style files and put them in the root folder.
*/
gulp.task( 'styles', function () {
gulp.src( watch.styles.source )
.pipe( plugins.sourcemaps.init() )
.pipe( plugins.sass( {
errLogToConsole: true,
includePaths: ['./sass'],
outputStyle: 'compact',
precision: 10
} ) )
.on('error', console.error.bind(console))
.pipe( plugins.autoprefixer( AUTOPREFIXER_BROWSERS ) )
.pipe( plugins.sourcemaps.write ( './', { includeContent: false } ) )
.pipe( gulp.dest( watch.styles.dest ) )
.pipe( plugins.filter( '**/*.css' ) ) // Filtering stream to only css files
.pipe( plugins.mergeMediaQueries( { log: true } ) ) // Merge Media Queries
.pipe( plugins.cssnano())
.pipe( plugins.rename({
prefix: project.name + '-'
}))
.pipe( gulp.dest( watch.styles.dest ) )
.pipe( plugins.filter( '**/*.css' ) ) // Filtering stream to only css files
.pipe( browserSync.stream() ) // Reloads style.css if that is enqueued.
.pipe( plugins.notify( { message: 'TASK: "styles" Completed! 💯', onLast: true } ) );
});
/**
* Minifies PNG, JPEG, GIF and SVG images.
*/
gulp.task( 'images', function() {
gulp.src( images.source )
.pipe( plugins.imagemin({
progressive: true,
optimizationLevel: 3, // 0-7 low-high
interlaced: true,
svgoPlugins: [{removeViewBox: false}]
}))
.pipe( gulp.dest( images.dest ) )
.pipe( plugins.notify( { message: 'TASK: "images" Completed! 💯', onLast: true } ) );
});
/**
* WP POT Translation File Generator.
*/
gulp.task( 'translate', function () {
return gulp.src( watch.php )
.pipe( plugins.sort() )
.pipe( plugins.wpPot( project.i18n ))
.pipe( gulp.dest( project.i18n.path ) )
.pipe( plugins.notify( { message: 'TASK: "translate" Completed! 💯', onLast: true } ) );
});
gulp.task( 'zipIt', function() {
return gulp.src( zipFiles )
.pipe( plugins.zip( project + '.zip' ) )
.pipe( gulp.dest( './' ) );
});
tasks = [ 'translate', 'images', 'browser-sync', 'styles' ];
/**
* Watches for file changes and runs specific tasks.
*/
gulp.task( 'default', tasks, function () {
gulp.watch( watch.php, reload ); // Reload on PHP file changes.
gulp.watch( watch.styles.source, ['styles', reload] ); // Reload on SCSS file changes.
});