-
-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathgulpfile.js
179 lines (151 loc) · 4.83 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
/**
* Module dependencies
*/
const del = require('del')
const gulp = require('gulp')
const cssnano = require('cssnano')
const mqpacker = require('css-mqpacker')
const autoprefixer = require('autoprefixer')
const gulpLoadPlugins = require('gulp-load-plugins')
const rename = require('gulp-rename')
const pxtorem = require('postcss-pxtorem')
const stripCssComments = require('gulp-strip-css-comments')
const replace = require('gulp-replace')
const browserify = require('browserify')
// const babelify = require('babelify')
const source = require('vinyl-source-stream')
const buffer = require('vinyl-buffer')
//
const merge = require('merge-stream')
const { name, version, author, license, repository } = require('./package')
// loade all gulp plugins
const $ = gulpLoadPlugins()
// environment
const isProduction = process.argv.includes('--production') || process.env.NODE_ENV === 'production'
// build comments
const comments = `/*!
* ${name} v${version}
* Copyright ${new Date().getFullYear()} ${author.name} <${author.email}> (${repository.url})
* Licensed under ${license}
*/`
/**
* Private tasks
*/
const clean = () => {
return del([
'assets',
'partials/styles.hbs',
// 'partials/amp/amp-styles.hbs',
`${name}-v${version}.zip`
])
}
const style = () => {
return gulp.src('src/scss/*.scss')
.pipe($.plumber())
.pipe($.if(!isProduction, $.sourcemaps.init()))
.pipe($.sass({ outputStyle: 'expanded' }).on('error', $.sass.logError))
.pipe($.if(isProduction, $.postcss([autoprefixer(), pxtorem(), mqpacker(), cssnano()])))
.pipe($.if(isProduction, stripCssComments({ preserve: false })))
.pipe($.if(isProduction, $.header(comments)))
.pipe($.if(!isProduction, $.sourcemaps.write('./map')))
.pipe(gulp.dest('assets/styles'))
.pipe($.livereload())
}
const styleTheme = () => {
return gulp.src('src/scss/theme/*.scss')
.pipe($.plumber())
.pipe($.sass({ outputStyle: 'expanded' }).on('error', $.sass.logError))
.pipe($.postcss([autoprefixer(), mqpacker(), cssnano()]))
.pipe(stripCssComments({ preserve: false }))
.pipe(gulp.dest('assets/styles/theme'))
}
const script = () => {
const files = ['main', 'search', 'pagination', 'prismjs']
return merge(files.map(function (file) {
return browserify({
basedir: '.',
debug: true,
entries: `./src/js/${file}.js`,
cache: {},
packageCache: {}
}).transform('babelify', {
presets: ['@babel/env'],
plugins: ['@babel/plugin-transform-runtime']
})
.bundle()
.pipe(source(`${file}.js`))
.pipe($.plumber())
.pipe(buffer())
.pipe($.if(!isProduction, $.sourcemaps.init({ loadMaps: true })))
.pipe($.if(isProduction, $.uglify()))
.pipe($.if(isProduction, $.header(comments)))
.pipe($.if(!isProduction, $.sourcemaps.write('./map')))
.pipe(gulp.dest('assets/scripts'))
.pipe($.livereload())
}))
}
// const copyFonts = () => {
// return gulp.src('src/fonts/**/*.{ttf,woff,eof,svg}')
// .pipe(gulp.dest('assets/fonts'))
// }
const image = () => {
return gulp.src('src/img/**/*.*')
.pipe($.plumber())
.pipe($.if(isProduction, $.imagemin()))
.pipe(gulp.dest('assets/images'))
.pipe($.livereload())
}
const archive = () => {
const source = [
'assets/**',
'locales/*.json',
'**/*.hbs',
'!node_modules', '!node_modules/**',
'!src', '!src/**',
'!documentation', '!documentation/**',
'LICENSE',
'package.json',
'README.md'
]
return gulp.src(source, { base: '.' })
.pipe($.plumber())
.pipe($.zip(`${name}-v${version}.zip`))
.pipe(gulp.dest('.'))
}
const copyPrismJs = () => {
return gulp.src('node_modules/prismjs/components/*.min.js')
.pipe($.plumber())
.pipe(gulp.dest('assets/scripts/components'))
}
const copyMainStyle = () => {
return gulp.src('assets/styles/main.css')
.pipe($.plumber())
.pipe(rename('styles.hbs'))
.pipe(gulp.dest('partials'))
}
const copyAmpStyle = () => {
return gulp.src('assets/styles/amp.css')
.pipe($.plumber())
.pipe(replace('@charset "UTF-8";', ''))
.pipe(stripCssComments({ preserve: false }))
.pipe($.postcss([cssnano()]))
.pipe(rename('amp-styles.hbs'))
.pipe(gulp.dest('partials/amp'))
}
const watch = () => {
$.livereload.listen()
gulp.watch('src/scss/**', style)
// gulp.watch('src/scss/theme/**', styleTheme)
gulp.watch('src/js/**', script)
gulp.watch('src/img/**/*.*', image)
gulp.watch('**/*.hbs').on('change', p => $.livereload.changed(p))
}
// const compile = gulp.parallel(style, script, copyFonts, image)
const compile = gulp.parallel(style, script, image)
/**
* Public tasks
*/
const build = gulp.series(clean, compile)
const release = gulp.series(build, styleTheme, copyMainStyle, copyAmpStyle, copyPrismJs, archive)
const develop = gulp.series(build, watch)
module.exports = { build, release, develop }