forked from sweetalert2/sweetalert2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
162 lines (147 loc) · 4.78 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
/* eslint @typescript-eslint/no-var-requires: 0 */
const gulp = require('gulp')
const rollup = require('gulp-rollup')
const gulpif = require('gulp-if')
const uglify = require('gulp-uglify')
const rename = require('gulp-rename')
const css2js = require('gulp-css2js')
const concat = require('gulp-concat')
const autoprefixer = require('gulp-autoprefixer')
const cleanCss = require('gulp-clean-css')
const babel = require('rollup-plugin-babel')
const json = require('@rollup/plugin-json')
const merge = require('merge2')
const sass = require('sass')
const browserSync = require('browser-sync').create()
const log = require('fancy-log')
const fs = require('fs')
const packageJson = JSON.parse(fs.readFileSync('package.json'))
const version = process.env.VERSION || packageJson.version
const banner = `/*!
* ${packageJson.name} v${version}
* Released under the ${packageJson.license} License.
*/`
const srcScriptFiles = ['src/**/*.js']
const srcStyleFiles = ['src/**/*.scss']
const continueOnError = process.argv.includes('--continue-on-error')
const skipMinification = process.argv.includes('--skip-minification')
const skipStandalone = process.argv.includes('--skip-standalone')
// ---
gulp.task('clean', () => {
if (!fs.existsSync('dist')) {
fs.mkdirSync('dist')
}
return fs.promises
.readdir('dist')
.then((fileList) => {
if (fileList.length > 0) {
const unlinkPromises = []
fileList.forEach((fileName) => {
unlinkPromises.push(fs.promises.unlink(`dist/${fileName}`))
})
return Promise.all(unlinkPromises)
}
})
.catch((error) => {
if (error.code !== 'ENOENT') {
return Promise.reject(error)
}
})
})
gulp.task('build:scripts', () => {
return gulp
.src(['package.json', ...srcScriptFiles])
.pipe(
rollup({
plugins: [
json(),
babel({
exclude: 'node_modules/**',
}),
],
input: 'src/sweetalert2.js',
output: {
format: 'umd',
name: 'Sweetalert2',
banner: banner,
footer: `\
if (typeof this !== 'undefined' && this.Sweetalert2){\
this.swal = this.sweetAlert = this.Swal = this.SweetAlert = this.Sweetalert2\
}`,
},
// https://github.com/rollup/rollup/issues/2271
onwarn(warning, rollupWarn) {
if (warning.code !== 'CIRCULAR_DEPENDENCY' && warning.code !== 'THIS_IS_UNDEFINED') {
rollupWarn(warning)
}
},
})
)
.on('error', (error) => {
if (continueOnError) {
log(error)
} else {
throw error
}
})
.pipe(gulp.dest('dist'))
.pipe(gulpif(!skipMinification, uglify()))
.pipe(gulpif(!skipMinification, rename('sweetalert2.min.js')))
.pipe(gulpif(!skipMinification, gulp.dest('dist')))
})
gulp.task('build:styles', () => {
const result = sass.renderSync({ file: 'src/sweetalert2.scss' })
fs.writeFileSync('dist/sweetalert2.css', result.css)
return gulp
.src('dist/sweetalert2.css')
.pipe(autoprefixer())
.pipe(gulp.dest('dist'))
.pipe(gulpif(!skipMinification, cleanCss()))
.pipe(gulpif(!skipMinification, rename('sweetalert2.min.css')))
.pipe(gulpif(!skipMinification, gulp.dest('dist')))
})
/**
* Warning: This task depends on dist/sweetalert2.js & dist/sweetalert2.css
*/
gulp.task('build:standalone', () => {
const prettyJs = gulp.src('dist/sweetalert2.js')
const prettyCssAsJs = gulp.src('dist/sweetalert2.min.css').pipe(css2js())
const prettyStandalone = merge(prettyJs, prettyCssAsJs).pipe(concat('sweetalert2.all.js')).pipe(gulp.dest('dist'))
if (skipMinification) {
return prettyStandalone
} else {
const uglyJs = gulp.src('dist/sweetalert2.min.js')
const uglyCssAsJs = gulp.src('dist/sweetalert2.min.css').pipe(css2js())
const uglyStandalone = merge(uglyJs, uglyCssAsJs).pipe(concat('sweetalert2.all.min.js')).pipe(gulp.dest('dist'))
return merge([prettyStandalone, uglyStandalone])
}
})
gulp.task(
'build',
gulp.series('clean', gulp.parallel('build:scripts', 'build:styles'), ...(skipStandalone ? [] : ['build:standalone']))
)
gulp.task('default', gulp.parallel('build'))
// ---
gulp.task(
'develop',
gulp.series(
'build',
async function watch() {
// Does not rebuild standalone files, for speed in active development
gulp.watch(srcScriptFiles, gulp.parallel('build:scripts'))
gulp.watch(srcStyleFiles, gulp.parallel('build:styles'))
},
async function sandbox() {
browserSync.init({
port: 8080,
uiPort: 8081,
notify: false,
reloadOnRestart: true,
https: false,
server: ['./'],
startPath: 'test/sandbox.html',
})
gulp.watch(['test/sandbox.html', 'dist/sweetalert2.js', 'dist/sweetalert2.css']).on('change', browserSync.reload)
}
)
)