forked from codesnippetspro/code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.babel.ts
161 lines (136 loc) · 4.1 KB
/
gulpfile.babel.ts
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
import { src, dest, series, parallel, watch as watchFiles, TaskFunction } from 'gulp'
import * as path from 'path'
import { exec } from 'child_process'
import { promises as fs } from 'fs'
import { webpack } from 'webpack'
import del from 'del'
import * as sourcemaps from 'gulp-sourcemaps'
import rename from 'gulp-rename'
import postcss from 'gulp-postcss'
import sass from 'gulp-sass'
import libsass from 'sass'
import cssnano from 'cssnano'
import autoprefixer from 'autoprefixer'
import zip from 'gulp-zip'
import rtlcss from 'gulp-rtlcss'
import cssimport from 'postcss-easy-import'
import hexrgba from 'postcss-hexrgba'
import eslint from 'gulp-eslint'
import codesniffer from 'gulp-phpcs'
import removeSourceMaps from 'gulp-remove-sourcemaps'
import * as pkg from './package.json'
import { config as webpackConfig } from './webpack.config'
const SRC_FILES = {
php: ['*.php', 'php/**/*.php'],
js: ['js/**/*.ts', 'js/**/*.tsx', 'js/**/*.js', 'js/**/*.jsx'],
css: {
all: ['css/**/*.scss'],
source: ['css/*.scss', '!css/**/_*.scss'],
directional: ['edit.css', 'manage.css'],
},
}
const DEST_DIR = 'dist/'
const BUNDLE_DIR = 'bundle/'
const BUNDLE_FILES = [
'assets/**/*',
'css/**/*',
'js/**/*',
'dist/**/*',
'!dist/**/*.map',
'php/**/*',
'vendor/**/*',
'code-snippets.php',
'uninstall.php',
'readme.txt',
'license.txt',
]
const transformCss = () =>
src(SRC_FILES.css.source)
.pipe(sourcemaps.init())
.pipe(sass(libsass)().on('error', sass(libsass).logError))
.pipe(postcss([
cssimport({
prefix: '_',
extensions: ['.scss', '.css']
}),
hexrgba(),
autoprefixer(),
cssnano({
preset: ['default', { discardComments: { removeAll: true } }]
})
]))
.pipe(sourcemaps.write('.'))
.pipe(dest(DEST_DIR))
const createRtlCss: TaskFunction = () =>
src(SRC_FILES.css.directional.map(file => path.join(DEST_DIR, file)))
.pipe(rename({ suffix: '-rtl' }))
.pipe(sourcemaps.init())
.pipe(rtlcss())
.pipe(sourcemaps.write('.'))
.pipe(dest(DEST_DIR))
export const css: TaskFunction = series(transformCss, createRtlCss)
export const lintJs: TaskFunction = () =>
src(SRC_FILES.js)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError())
const transformJs: TaskFunction = done => {
webpack({
...webpackConfig,
mode: 'development',
devtool: 'eval'
}, done)
}
export const js: TaskFunction = series(lintJs, transformJs)
export const phpcs: TaskFunction = () =>
src(SRC_FILES.php)
.pipe(codesniffer({ bin: 'vendor/bin/phpcs', showSniffCode: true }))
.pipe(codesniffer.reporter('log'))
const copyCodeMirrorThemes: TaskFunction = () =>
src('node_modules/codemirror/theme/*.css')
.pipe(postcss([cssnano()]))
.pipe(dest(`${DEST_DIR}/editor-themes`))
export const vendor: TaskFunction = copyCodeMirrorThemes
export const clean: TaskFunction = () => del(DEST_DIR)
export const test = parallel(lintJs, phpcs)
export const build = series(clean, parallel(vendor, css, js))
export default build
export const bundle: TaskFunction = (() => {
const cleanupBefore: TaskFunction = () =>
del([BUNDLE_DIR, pkg.name, `${pkg.name}.*.zip`])
const composerProduction: TaskFunction = done =>
exec('composer install --no-dev', error => {
if (error) throw error
done()
})
const composer: TaskFunction = done =>
exec('composer install', error => {
if (error) throw error
done()
})
const webpackProduction: TaskFunction = done => {
webpack({ ...webpackConfig, mode: 'production' }, done)
}
const copyFiles: TaskFunction = () =>
src(BUNDLE_FILES, { base: '.' })
.pipe(removeSourceMaps())
.pipe(dest(pkg.name))
const archive: TaskFunction = () =>
src(`${pkg.name}/**/*`, { base: '.' })
.pipe(zip(`${pkg.name}.${pkg.version}.zip`))
.pipe(dest('.'))
const cleanupAfter: TaskFunction = done =>
fs.rename(pkg.name, BUNDLE_DIR).then(() => done())
return series(
parallel(build, cleanupBefore),
parallel(composerProduction, webpackProduction),
copyFiles,
archive,
parallel(composer, cleanupAfter)
)
})()
export const watch: TaskFunction = series(build, done => {
watchFiles(SRC_FILES.css.all, series(css))
watchFiles(SRC_FILES.js, series(js))
done()
})