forked from them-es/starter-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
178 lines (159 loc) · 3.25 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
import gulp from 'gulp';
import dartSass from 'sass';
import gulpSass from 'gulp-sass';
import postcss from 'gulp-postcss';
import sourcemaps from 'gulp-sourcemaps';
import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';
import imagemin, {gifsicle, mozjpeg, optipng, svgo} from 'gulp-imagemin';
import browserSync from 'browser-sync';
const sass = gulpSass( dartSass );
const server = browserSync.create();
const localHost = 'http://localhost:8000/';
/**
* Define all source paths
*/
var paths = {
styles: {
src: './source/css/*.scss',
dest: './css/'
},
scripts: {
src: './source/js/*.js',
dest: './js/'
},
php: {
src: './source/php/*.php',
dest: './'
},
img: {
src: './source/img/**/*.{gif,jpg,jpeg,png,svg}',
dest: './img/'
}
};
function build_php() {
return gulp.src( paths.php.src )
.pipe(
gulp.dest( paths.php.dest )
)
.pipe(
server.stream() // Browser Reload
);
}
/**
* Webpack compilation: https://webpack.js.org, https://github.com/shama/webpack-stream#usage-with-gulp-watch
*
* build_js()
*/
function build_js() {
const compiler = require( 'webpack' ),
webpackStream = require( 'webpack-stream' );
return gulp.src( paths.scripts.src )
.pipe(
webpackStream( {
config: require( './webpack.config.js' )
},
compiler
)
)
.pipe(
gulp.dest( paths.scripts.dest )
)
.pipe(
server.stream() // Browser Reload
);
}
/**
* SASS-CSS compilation: https://www.npmjs.com/package/gulp-sass
*
* build_css()
*/
function build_css() {
const plugins = [
autoprefixer(),
cssnano(),
];
return gulp.src( paths.styles.src )
.pipe(
sourcemaps.init()
)
.pipe(
sass( {
includePaths: [ './node_modules' ]
} )
.on( 'error', sass.logError )
)
.pipe(
postcss( plugins )
)
.pipe(
sourcemaps.write( './' )
)
.pipe(
gulp.dest( paths.styles.dest )
)
.pipe(
server.stream() // Browser Reload
);
}
function minify_img() {
return gulp.src( paths.img.src )
.pipe(
imagemin(
[
gifsicle( {interlaced: true} ),
mozjpeg( {quality: 75, progressive: true} ),
optipng( {optimizationLevel: 5} ),
svgo( {
plugins: [
{
name: 'removeViewBox',
active: true
},
{
name: 'cleanupIDs',
active: false
}
]
} )
]
)
)
.pipe(
gulp.dest( paths.img.dest )
)
.pipe(
server.stream() // Browser Reload
);
}
/**
* Watch task: Webpack + SASS
*
* $ gulp default
*/
gulp.task( 'default',
function () {
// Modify "localHost" constant and uncomment "server.init()" to use browser sync
server.init({
proxy: localHost,
} );
gulp.watch( paths.scripts.src, build_js );
gulp.watch( [ paths.styles.src, './source/css/*.scss' ], build_css );
gulp.watch( paths.php.src, build_php );
gulp.watch( paths.img.src, minify_img );
}
);
gulp.task( 'build',
function () {
// Modify "localHost" constant and uncomment "server.init()" to use browser sync
/*server.init({
proxy: localHost,
} );*/
gulp.build( paths.scripts.src, build_js );
gulp.build( [ paths.styles.src, './source/css/*.scss' ], build_css );
gulp.watch( paths.php.src, build_php );
gulp.watch( paths.img.src, minify_img );
}
);
// export default gulp.default;
// export function gulp.build;