forked from nineya/halo-theme-dream2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
146 lines (133 loc) · 3.94 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
const {src, dest, task, series, parallel} = require('gulp')
const webpack = require('webpack-stream')
const less = require('gulp-less')
const autoprefix = require('gulp-autoprefixer')
const uglify = require('gulp-uglify')
const minifyCSS = require('gulp-csso')
const zip = require('gulp-zip')
const rename = require('gulp-rename')
const clean = require('gulp-clean')
const path = require('path')
const execSync = require('child_process').execSync
const fs = require('fs')
const resolve = (name) => path.resolve(__dirname, name)
const cssPath = './templates/assets/css'
const jsPath = './templates/assets/js'
const distPath = './dist'
const devModel = process.env.npm_config_devel
const version = process.env.npm_config_tag
if (devModel) {
console.log('> 开发模式')
}
version && console.log(`> 发布版本:${version}`)
task('clean', () => {
return src([cssPath, jsPath, distPath], {
read: false,
allowEmpty: true,
}).pipe(
clean({
force: true,
})
)
})
task('version', (done) => {
if (version == null) {
console.log('[Version] No \'--tag\' parameters are specified')
done()
return
}
const themePath = 'theme.yaml'
const packagePath = 'package.json'
const themeData = fs.readFileSync(themePath, 'utf8')
.replace(/^(\s+version:)\s+[^\s]+$/m, '$1 ' + version)
fs.writeFileSync(themePath, themeData)
let packageData = fs.readFileSync(packagePath, 'utf8')
.replace(/"version":\s*"[^"]+"/, `"version": "${version}"`)
fs.writeFileSync(packagePath, packageData)
done()
})
task('css', () => {
const ignoreFiles = [].map((file) => `./src/css/${file}.less`)
let gw = src('./src/css/**/*.less', {
ignore: ignoreFiles,
})
.pipe(less())
.pipe(
autoprefix({
overrideBrowserslist: ['> 2%', 'last 2 versions', 'not ie 6-9'],
cascade: false,
})
)
if (!devModel) {
gw = gw.pipe(minifyCSS())
}
return gw.pipe(
rename({
suffix: '.min',
})
).pipe(dest(cssPath))
})
task('js', () => {
const readFile = (prefix, dir, ignoreFiles) => {
let result = {}
let files = fs.readdirSync(dir, 'utf-8')
files.forEach((file) => {
let filePath = path.join(dir, file)
let states = fs.statSync(filePath)
if (states.isDirectory()) {
Object.assign(result, readFile(path.join(prefix, file), filePath, ignoreFiles))
} else if (ignoreFiles.length
? /\.js$/.test(file) && !ignoreFiles.includes(path.join(prefix, file))
: /\.js$/.test(file)) {
const fileName = file.replace(/.js$/, '')
result[path.join(prefix, fileName)] = resolve(filePath)
}
})
return result
}
const getEntryData = () => {
return readFile('', './src/js', [])
}
return webpack({
mode: devModel ? 'development' : 'production',
entry: getEntryData(),
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: resolve('source'),
exclude: resolve('node_modules'),
options: {
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-transform-runtime'],
},
},
],
},
stats: 'errors-only',
output: {
filename: '[name].min.js',
},
})
.pipe(uglify())
.pipe(dest(jsPath))
})
task('zip', () => {
const target = ['./templates/**', './*.yaml', 'README.md', 'LICENSE']
return src(target, {base: '.'})
.pipe(zip('halo-theme-dream2.0.zip'))
.pipe(dest(distPath))
})
task('publish', (done) => {
// 需要将tag标签内容置为 latest
process.env.npm_config_tag = 'latest'
console.log(execSync('npm publish').toString())
done()
})
// 默认模式
task('default', series('clean', parallel('css', 'js'), 'zip'))
// release模式,需要使用--tag参数指定版本号
task('release', series('clean', 'version', parallel('css', 'js'), 'zip'))
// push模式,需要使用--tag参数指定版本号
task('push', series('clean', 'version', parallel('css', 'js'), 'zip', 'publish'))