-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
180 lines (165 loc) · 4.81 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
const gulp = require( 'gulp' );
const $ = require( 'gulp-load-plugins' )();
const pngquant = require( 'imagemin-pngquant' );
const mergeStream = require( 'merge-stream' );
const sass = require('gulp-sass')(require('sass'));
const { scanDir } = require( '@kunoichi/grab-deps' );
const fs = require( 'fs' );
const named = require( 'vinyl-named' );
const webpackBundle = require( 'webpack' );
const webpack = require( 'webpack-stream' );
let plumber = true;
// Toggle flag.
gulp.task( 'noplumber', ( done ) => {
plumber = false;
done();
} );
// Compile sass.
gulp.task( 'scss:compile', function () {
return gulp.src( [ 'assets/scss/**/*.scss' ] )
.pipe( $.plumber() )
.pipe( sass( {
sourcemap: true,
outputStyle: 'compressed',
} ) )
.pipe( gulp.dest( 'assets/css' ) );
} );
// Stylelint.
gulp.task( 'scss:lint', () => {
let task = gulp.src( [ 'assets/scss/**/*.scss' ] );
if ( plumber ) {
task = task.pipe( $.plumber() );
}
return task.pipe( $.stylelint( {
reporters: [
{
formatter: 'string',
console: true,
},
],
} ) );
} );
// CSS tasks.
gulp.task( 'scss', gulp.parallel( 'scss:lint', 'scss:compile' ) );
// Minify
gulp.task( 'js:bundle', function () {
const tmp = {};
return gulp.src( [ './assets/js/src/**/*.js' ] )
.pipe( $.plumber( {
errorHandler: $.notify.onError( '<%= error.message %>' ),
} ) )
.pipe( named() )
.pipe( $.rename( function ( path ) {
tmp[ path.basename ] = path.dirname;
} ) )
.pipe( webpack( require( './webpack.config.js' ), webpackBundle ) )
.pipe( $.rename( function ( path ) {
if ( tmp[ path.basename ] ) {
path.dirname = tmp[ path.basename ];
} else if ( path.extname === '.map' && tmp[ path.basename.replace( /\.js$/, '' ) ] ) {
path.dirname = tmp[ path.basename.replace( /\.js$/, '' ) ];
}
return path;
} ) )
.pipe( gulp.dest( './assets/js/dist/') );
} );
// JS Hint
gulp.task( 'js:lint', function () {
let task = gulp.src( [ './assets/js/src/**/*.js' ] );
if ( plumber ) {
task = task.pipe( $.plumber() );
}
return task.pipe( $.eslint( { useEslintrc: true } ) )
.pipe( $.eslint.format() );
} );
// JS task
gulp.task( 'js', gulp.parallel( 'js:bundle', 'js:lint' ) );
// Imagemin
gulp.task( 'imagemin', function () {
return gulp.src( './assets/images/**/*' )
.pipe( $.imagemin( {
progressive: true,
svgoPlugins: [ { removeViewBox: false } ],
use: [ pngquant() ]
} ) )
.pipe( gulp.dest( './assets/images' ) );
} );
// Copy required
gulp.task( 'copy', () => {
return mergeStream(
gulp.src(
[
'node_modules/chart.js/dist/Chart.min.js',
]
)
.pipe( gulp.dest( './assets/vendor/chart-js' ) ),
gulp.src(
[
'node_modules/jsrender/jsrender.min.js',
'node_modules/jsrender/jsrender.min.js.map',
]
)
.pipe( gulp.dest( './assets/vendor/jsrender' ) ),
gulp.src(
[
'node_modules/jquery-ui/ui/i18n/*.js',
],
{ base: 'node_modules/jquery-ui/ui/i18n' }
)
.pipe( gulp.dest( './assets/vendor/jquery-ui-i18n' ) ),
gulp.src(
[
'node_modules/font-awesome/css/font-awesome.min.css',
'node_modules/font-awesome/css/font-awesome.css.map',
'node_modules/font-awesome/fonts/**/*'
],
{ base: 'node_modules/font-awesome' }
)
.pipe( gulp.dest( './assets/vendor/font-awesome' ) ),
gulp.src(
[
'node_modules/jquery-ui-mp6/src/css/*',
'node_modules/jquery-ui-mp6/src/images/*'
],
{ base: 'node_modules/jquery-ui-mp6/src' }
)
.pipe( gulp.dest( './assets/vendor/jquery-ui-mp6' ) ),
gulp.src(
[
'node_modules/jquery-ui-timepicker-addon/dist/jquery-ui-timepicker-addon.min.js',
'node_modules/jquery-ui-timepicker-addon/dist/jquery-ui-timepicker-addon.min.css'
],
{ base: 'node_modules/jquery-ui-timepicker-addon/dist' }
)
.pipe( gulp.dest( './assets/vendor/jquery-ui-timepicker-addon' ) ),
gulp.src(
[
'node_modules/jquery-ui-timepicker-addon/dist/i18n/*',
],
)
.pipe( gulp.dest( './assets/vendor/jquery-ui-timepicker-addon/i18n' ) )
);
} );
// watch
gulp.task( 'watch', function () {
gulp.watch( './assets/scss/**/*.scss', gulp.parallel( [ 'scss:compile', 'scss:lint' ] ) );
gulp.watch( './assets/js/src/**/*.js', gulp.parallel( 'js:lint', 'js:bundle' ) );
gulp.watch( [ 'assets/**/*.css', 'assets/**/*.js' ], gulp.task( 'dump' ) );
} );
// Dump task.
gulp.task( 'dump', function( done ) {
const result = [];
for ( const dir of [ 'js/dist', 'css' ] ) {
scanDir( `assets/${dir}` ).forEach( ( setting ) => {
result.push( setting );
} );
}
fs.writeFileSync( './wp-dependencies.json', JSON.stringify( result, null, "\t" ) );
done();
} );
// Build
gulp.task( 'build', gulp.series( gulp.parallel( 'copy', 'js:bundle', 'scss:compile', 'imagemin' ), gulp.task( 'dump' ) ) );
// Lint task.
gulp.task( 'lint', gulp.series( 'noplumber', gulp.parallel( 'js:lint', 'scss:lint' ) ) );
// Default Tasks
gulp.task( 'default', gulp.task( 'watch' ) );