-
Notifications
You must be signed in to change notification settings - Fork 771
/
gulpfile.js
172 lines (159 loc) · 4.84 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
'use strict'
const path = require('path')
const gulp = require('gulp')
const gulpBase64 = require('gulp-base64')
const preprocess = require('gulp-preprocess')
const babel = require('gulp-babel')
const gutil = require('gulp-util')
const compiler = require('vueify').compiler
const stylus = require('stylus')
const gulpStylus = require('gulp-stylus')
const through = require('through2')
const merge2 = require('merge2')
const colors = require('colors')
const pkg = require('./package.json')
const components = require('./examples/components.json')
// const execSync = require('child_process').execSync
colors.setTheme({
info: 'green',
warn: 'yellow',
error: 'red',
bold: 'bold'
})
const componentList = generateComponentsList(components)
const uncompliedComponentList = [...componentList]
const mixins = [
path.join(__dirname, './components/_style/mixin/*.styl'),
path.join(__dirname, './node_modules/nib/lib/nib/vendor'),
path.join(__dirname, './node_modules/nib/lib/nib/gradients'),
path.join(__dirname, './node_modules/nib/lib/nib/flex')
]
let succNum = 0
let failNum = 0
function generateComponentsList (components) {
let list = []
components.map(nav =>
nav.list.map(item =>
list.push(item.path.substr(1))
)
)
return list
}
function compilingComponentLog () {
return through.obj((file, encode, callback) => {
if (file.path) {
const res = file.content ? 0 : -1
file.path.replace(/lib\/((\S)+)/ig, (s, v) => {
if (!v) return
if (res < 0) {
succNum++
console.log(`${pkg.name}/lib/${v} ✔`.info)
} else {
failNum++
console.log(`${pkg.name}/lib/${v} ✘ (complie error)`.error)
}
uncompliedComponentList.splice(uncompliedComponentList.indexOf(v), 1)
})
}
callback()
})
}
function compileVueStylus (content, cb, compiler, filePath) {
stylus(content)
.import(mixins[0])
.import(mixins[1])
.import(mixins[2])
.import(mixins[3])
.render((err, css) => {
if (err) throw err
cb(null, css)
})
}
function gulpVueify (options) {
return through.obj(function (file, encode, callback) {
if (file.isNull()) {
return callback(null, file)
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-vueify', 'Streams are not supported'))
return callback()
}
if (options) {
compiler.applyConfig(options)
}
const isExactCss = options.isExactCss
let styleContent = ''
const styleCb = res => {
if (res.style) {
styleContent = res.style
}
}
if (isExactCss) {
compiler.on('style', styleCb)
}
compiler.compile(file.contents.toString(), file.path, (err, result) => {
if (err) {
this.emit('error', new gutil.PluginError('gulp-vueify',
`In file ${path.relative(process.cwd(), file.path)}:\n${err.message}`))
return callback()
}
if (isExactCss) {
// 仅提取css
file.path = gutil.replaceExtension(file.path, '.css')
file.contents = Buffer.from(styleContent)
compiler.removeListener('style', styleCb)
} else {
// js & css 集成至一个文件
file.path = gutil.replaceExtension(file.path, '.js')
file.contents = Buffer.from(result)
}
callback(null, file)
})
})
}
gulp.task('compile', () => {
const streamCompiledVue = gulp.src('./components/*/*.vue')
.pipe(gulpVueify({
extractCSS: true,
customCompilers: {
stylus: compileVueStylus
}
}))
.pipe(gulp.dest('./lib'))
.pipe(compilingComponentLog())
const streamCompiledCss = gulp.src('./components/*/*.vue')
.pipe(gulpVueify({
isExactCss: true,
customCompilers: {
stylus: compileVueStylus
}
}))
.pipe(gulpBase64())
.pipe(gulp.dest('./lib/style'))
const streamCompiledStylus = gulp.src('./components/_style/*.styl')
.pipe(gulpStylus({
compress: true,
import: mixins
}))
.pipe(gulp.dest('./lib/style'))
const streamCompiledJs = gulp.src('./components/**/!(test)/!(component).js')
.pipe(preprocess({
context: {
'NODE_ENV': 'production',
'MAN_VERSION': `'${pkg.version}' //`
}}))
.pipe(babel())
.pipe(gulp.dest('./lib'))
return merge2([streamCompiledVue, streamCompiledCss, streamCompiledStylus, streamCompiledJs])
})
gulp.task('build', ['compile'], () => {
uncompliedComponentList.map(item => {
failNum++
console.log(`${pkg.name}/lib/${item} ✘ (not found)`.error)
})
console.log(
`\n${pkg.name}(${pkg.version}) `.warn.bold +
`build ${componentList.length} components : `.warn.bold +
`${succNum} successed / ${failNum} failed\n`.warn.bold
)
})