This repository has been archived by the owner on Nov 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
146 lines (125 loc) · 3.25 KB
/
gulpfile.babel.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
import autoprefixer from 'gulp-autoprefixer'
import babel from 'rollup-plugin-babel'
import buffer from 'vinyl-buffer'
import cleanCSS from 'gulp-clean-css'
import commonJs from 'rollup-plugin-commonjs'
import gulp from 'gulp'
import nodeResolve from 'rollup-plugin-node-resolve'
import rename from 'gulp-rename'
import rollup from 'rollup-stream'
import sass from 'gulp-sass'
import sassSVGInliner from 'sass-inline-svg'
import source from 'vinyl-source-stream'
import sourcemaps from 'gulp-sourcemaps'
import standard from 'gulp-standard'
import uglify from 'gulp-uglify'
let meta = require('./package.json')
let paths = {
script: './script',
scriptDist: './public/dist/script',
style: './style',
styleDist: './public/dist/style',
}
gulp.task('lint-script', () => {
return gulp.src(paths.script + '/**/*.js')
.pipe(standard())
.pipe(standard.reporter('default', {
breakOnError: true,
quiet: true
}))
})
let rollupCache
gulp.task('script', ['lint-script'], () => {
// run module builder and return a stream
const stream = rollup({
input: paths.script + '/index.js',
external: [
],
plugins: [
babel({
babelrc: false,
presets: [
['es2015', {
loose: true,
modules: false
}]
],
plugins: ['external-helpers'],
exclude: ['node_modules/**']
}),
nodeResolve({
jsnext: true,
main: true
}),
commonJs({
include: ['node_modules/**']
})
],
format: 'umd',
sourcemap: true,
cache: rollupCache,
amd: { id: meta.name }
})
return stream
// enable rollup cache
.on('bundle', (bundle) => {
rollupCache = bundle
})
// handle errors gracefully
.on('error', err => {
console.error(err.message)
if (err.codeFrame !== undefined) {
console.error(err.codeFrame)
}
stream.emit('end')
})
// set output filename
.pipe(source(meta.name + '.js', paths.src))
// buffer the output, most gulp plugins do not support streams
.pipe(buffer())
// init sourcemaps with inline sourcemap produced by rollup-stream
.pipe(sourcemaps.init({
loadMaps: true
}))
// minify code
.pipe(uglify())
// save result
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(paths.scriptDist))
})
gulp.task('style', () => {
return gulp.src(paths.style + '/main.scss')
// init sourcemaps
.pipe(sourcemaps.init())
// compile sass to css
.pipe(
sass({
includePaths: ['node_modules'],
outputStyle: 'expanded',
functions: {
'inline-svg': sassSVGInliner('assets/svg')
}
})
.on('error', sass.logError)
)
// autoprefix css
.pipe(autoprefixer('last 2 version', 'ie 11', '> 1%'))
// minify
.pipe(cleanCSS({ processImport: false }))
// save result
.pipe(rename(meta.name + '.css'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(paths.styleDist))
})
gulp.task('watch', () => {
// watch scripts
gulp.watch(
[paths.script + '/**/*.js'],
['script'])
// watch styles
gulp.watch(
[paths.style + '/**/*.scss'],
['style'])
})
gulp.task('build', ['script', 'style'])
gulp.task('default', ['script', 'style', 'watch'])