This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
212 lines (175 loc) · 6.88 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import * as path from 'path';
import * as fs from 'fs';
import * as packageJson from './package';
import gulp from 'gulp';
import concat from 'gulp-concat';
import * as eslint from 'gulp-eslint';
import header from 'gulp-header';
import uglify from 'gulp-uglify';
import jsdoc from 'gulp-jsdoc3';
import gulpBabel from 'gulp-babel';
/** Rollup plugins **/
import * as rollup from 'rollup';
import rollupBabel from 'rollup-plugin-babel';
import rollupResolve from 'rollup-plugin-node-resolve';
/** Util Modules **/
import del from 'del';
import moduleMemberListsReadStream from './node-scripts/moduleMemberListsReadStream';
/** Module's inner modules map (for members list stream) **/
import * as Input from './src/Input';
import * as InputFilter from './src/InputFilter';
// import * as Utils from './src/Utils';
const
/** System and config includes **/
{buildConfig, buildConfig: {srcsGlob}} = packageJson,
/** Gulp Modules (or modules used by gulp) **/
getReadStreamFinish = (resolve, reject) => err => err ? reject(err) : resolve(),
/** Paths **/
{docs: docsBuildPath, dist: buildPathRoot} = buildConfig.paths,
buildPath = (...tails) => path.join(buildPathRoot, ...tails),
log = console.log.bind(console),
// Build paths
cjsBuildPath = buildPath(buildConfig.folderNames.cjs),
amdBuildPath = buildPath(buildConfig.folderNames.amd),
umdBuildPath = buildPath(buildConfig.folderNames.umd),
iifeBuildPath = buildPath(buildConfig.folderNames.iife),
es6BuildPath = buildPath(buildConfig.folderNames.es6Module),
// Module names
{outputFileNameMin, outputFileName, outputFileNameMjs, inputModuleName} = buildConfig,
{series, dest, src, parallel} = gulp,
innerModulesMap = {
Input,
InputFilter,
// Utils
},
deleteFilePaths = pathsToDelete =>
del(pathsToDelete)
.then(deletedPaths => {
if (deletedPaths.length) {
log('\nThe following paths have been deleted: \n - ' + deletedPaths.join('\n -'));
return;
}
log(' - No paths to clean.\n', '--mandatory');
})
.catch(log),
cleanTask = () => {
let pathsToDelete = [cjsBuildPath, amdBuildPath, umdBuildPath, iifeBuildPath, es6BuildPath]
.map(partialPath => path.join(partialPath, '**', '*.js'));
return deleteFilePaths(pathsToDelete);
},
eslintTask = () =>
src([srcsGlob, '!node_modules/**'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failOnError()),
umdTask = () =>
src(srcsGlob)
.pipe(gulpBabel(buildConfig.buildUmdOptions.babel))
.pipe(dest(umdBuildPath)),
amdTask = () =>
src(srcsGlob)
.pipe(gulpBabel(buildConfig.buildAmdOptions.babel))
.pipe(dest(amdBuildPath)),
cjsTask = () =>
src(srcsGlob)
.pipe(gulpBabel(buildConfig.buildCjsOptions.babel))
.pipe(dest(cjsBuildPath)),
iifeTask = () =>
rollup.rollup({
input: `src/${inputModuleName}.js`,
external: buildConfig.buildIifeOptions.rollup.external,
plugins: [
rollupResolve(),
rollupBabel({
babelrc: false,
presets: [
[
'@babel/preset-env',
{
modules: false
}
]
],
exclude: ['node_modules/**'] // only transpile our source code
})
]
})
.then(bundle => bundle.write({
file: path.join(iifeBuildPath, outputFileName),
format: 'iife',
name: inputModuleName,
sourcemap: true
})),
es6ModuleTask = () =>
rollup.rollup({
input: `src/${inputModuleName}.js`,
external: buildConfig.es6ModuleRollup.config.external,
plugins: [
rollupResolve(),
rollupBabel({
babelrc: false,
presets: [],
exclude: ['node_modules/**'],
})
]
})
.then(bundle => bundle.write({
file: path.join(es6BuildPath, outputFileNameMjs),
format: 'es',
name: inputModuleName,
sourcemap: true
})
.then(() => bundle)
)
.then(bundle => bundle.write({
file: path.join(es6BuildPath, outputFileName),
format: 'es',
name: inputModuleName,
sourcemap: true
})),
uglifyTask = () => {
const data = {};
return src(path.join(iifeBuildPath, outputFileName))
.pipe(concat(path.join(iifeBuildPath, outputFileNameMin)))
.pipe(gulpBabel(buildConfig.buildIifeOptions.babel))
.pipe(uglify(buildConfig.uglifyOptions))
.pipe(header('/**! ' + outputFileName + ' <%= version %> | License: <%= license %> | ' +
'Built-on: <%= (new Date()) %> **/', Object.assign(data, packageJson)))
.pipe(dest('./'));
},
buildJsTask = parallel(series(iifeTask, uglifyTask), cjsTask, amdTask, umdTask, es6ModuleTask),
buildTask = series(cleanTask, buildJsTask),
readmeTask = () => {
const moduleMemberListOutputPath = './markdown-fragments-generated/module-and-member-list.md';
return deleteFilePaths([
'./markdown-fragments-generated/*.md',
'./README.md'
])
.then(() => new Promise((resolve, reject) => moduleMemberListsReadStream(innerModulesMap)
.pipe(fs.createWriteStream(moduleMemberListOutputPath))
.on('finish', getReadStreamFinish(resolve, reject))
))
.then(() => new Promise((resolve, reject) => gulp.src(buildConfig.readme.files)
.pipe(concat('./README.md'))
.pipe(gulp.dest('./'))
.on('finish', getReadStreamFinish(resolve, reject))
));
},
docTask = series(readmeTask, function docTask () {
return deleteFilePaths([`${docsBuildPath}/**/*`])
.then(() => new Promise((resolve, reject) =>
src(['README.md', srcsGlob])
.on('finish', getReadStreamFinish(resolve, reject))
.pipe(jsdoc(buildConfig.jsdoc))
));
}),
watchTask = series(buildTask, function watchTask () {
return gulp.watch([srcsGlob, './node_modules/**'], buildJsTask);
}
);
gulp.task('eslint', eslintTask);
gulp.task('build', buildTask);
gulp.task('readme', readmeTask);
gulp.task('docs', docTask);
gulp.task('watch', watchTask);
gulp.task('default', watchTask);