-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
188 lines (164 loc) · 4.89 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
180
181
182
183
184
185
186
187
188
// GULP PACKAGES
// Most packages are lazy loaded.
const atImport = require('postcss-import'),
autoprefixer = require('autoprefixer'),
browserSync = require('browser-sync').create(),
cssnano = require('cssnano'),
filter = require('gulp-filter'),
gulp = require('gulp'),
gutil = require('gulp-util'),
imagemin = require('gulp-imagemin'),
imageminMozjpeg = require('imagemin-Mozjpeg'),
plugin = require('gulp-load-plugins')(),
webpackStream = require('webpack-stream'),
webpack3 = require('webpack'),
webpack_config = require('./webpack.config.js');
// GULP VARIABLES
// Modify these variables to match your project needs
// Set local URL if using Browser-Sync
const LOCAL_URL = 'http://lightmatterwp.dev/';
// Set path to Foundation files
const FOUNDATION = 'node_modules/foundation-sites';
// Select Foundation components, remove components project will not use
const SOURCE = {
scripts: [
// Use Foundation and plugins first, then compile the rest.
'assets/scripts/js/init.js',
],
// SCSS files will be concatenated, prefixed and minified.
styles: 'assets/styles/scss/**/*.scss',
// Images placed here will be optimized.
images: 'assets/images/**/*',
php: '**/*.php',
};
const ASSETS = {
styles: 'assets/styles/',
scripts: 'assets/scripts/',
images: 'assets/images/',
all: 'assets/',
};
const DIST = {
styles: 'dist/styles/',
scripts: 'dist/scripts/',
images: 'dist/images/',
all: 'dist/',
};
const JSHINT_CONFIG = {
node: true,
globals: {
document: true,
jQuery: true,
},
esversion: 6,
};
const SHOW_ERROR = function(error) {
gutil.log(gutil.colors.red(error.message));
this.emit('end');
};
// GULP FUNCTIONS
// JSHint, transpile, concat, and minify JavaScript.
gulp.task('scripts', function() {
// Use a custom filter so we only lint custom JS
const CUSTOMFILTER = filter(ASSETS.scripts + 'js/**/*.js', { restore: true });
return gulp
.src(SOURCE.scripts)
.pipe(plugin.plumber(SHOW_ERROR)) // Show errors happening in the piping process.
.pipe(webpackStream(webpack_config, webpack3)) // Transpile all JavaScript ES6 to compatible browser code.
.pipe(CUSTOMFILTER)
.pipe(plugin.jshint(JSHINT_CONFIG))
.pipe(plugin.jshint.reporter('jshint-stylish'))
.pipe(CUSTOMFILTER.restore)
.pipe(gulp.dest(DIST.scripts));
});
// Compile, postcss and minify SCSS.
gulp.task('styles', function() {
// Process CSS with custom plugins.
const PROCESSORS = [
atImport(), // Import .css files in .scss files.
autoprefixer({
// Prefix CSS.
browsers: ['last 2 version', 'ie >= 9', 'Android >= 2.3', 'ios >= 7'], // Support specific browser versions and the last 2 version from each browser.
}),
cssnano(), // Minify CSS.
];
return gulp
.src(SOURCE.styles)
.pipe(plugin.plumber(SHOW_ERROR)) // Show errors happening in the piping process.
.pipe(plugin.sourcemaps.init()) // Initialize sourcemaps.
.pipe(plugin.sass()) // Compile SCSS to CSS.
.pipe(plugin.postcss(PROCESSORS)) // Process CSS with custom plugins.
.pipe(plugin.sourcemaps.write('.')) // Write sourcemaps.
.pipe(gulp.dest(DIST.styles))
.pipe(
browserSync.reload({
stream: true,
})
);
});
// Optimize images, move into assets directory
gulp.task('images', function() {
// Process images with custom plugins.
const PROCESSORS = {
plugins: [
imagemin.gifsicle({
interlaced: true,
optimizationLevel: 3,
}),
imageminMozjpeg({
quality: 85,
}),
imagemin.optipng({
optimizationLevel: 5,
}),
imagemin.svgo({
plugins: [
{
removeViewBox: false,
},
],
}),
],
options: {
verbose: true, // Log every image in the console.
},
};
return gulp
.src(SOURCE.images)
.pipe(plugin.plumber(SHOW_ERROR)) // Show errors happening in the piping process.
.pipe(plugin.imagemin(PROCESSORS.plugins, PROCESSORS.options))
.pipe(gulp.dest(DIST.images));
});
gulp.task('translate', function() {
return gulp
.src(SOURCE.php)
.pipe(plugin.plumber(SHOW_ERROR)) // Show errors happening in the piping process.
.pipe(
plugin.wpPot({
domain: 'lightmatter',
package: 'Example project',
})
)
.pipe(gulp.dest('dist/translation/translation.pot'));
});
// Browser-Sync watch files and inject changes.
gulp.task('browsersync', function() {
// Watch these files.
const files = [SOURCE.php];
browserSync.init(files, {
proxy: LOCAL_URL,
});
gulp.watch(SOURCE.styles, gulp.parallel('styles'));
gulp.watch(SOURCE.scripts, gulp.parallel('scripts')).on('change', browserSync.reload);
gulp.watch(SOURCE.images, gulp.parallel('images'));
});
// Watch files for changes (without Browser-Sync).
gulp.task('watch', function() {
// Watch .scss files.
gulp.watch(SOURCE.styles, gulp.parallel('styles'));
// Watch scripts files.
gulp.watch(SOURCE.scripts, gulp.parallel('scripts'));
// Watch images files.
gulp.watch(SOURCE.images, gulp.parallel('images'));
});
// Run styles, scripts and images.
gulp.task('default', gulp.parallel('styles', 'scripts', 'images'));