-
Notifications
You must be signed in to change notification settings - Fork 436
/
gulpfile.js
324 lines (276 loc) · 9.85 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
var gulp = require('gulp'),
$ = require( "gulp-load-plugins" )({ lazy: true, pattern:['*', 'gulp-'], rename: {
'sass': 'xsass' // map to some other name because 'gulp-sass' already loads "sass", so avoid collusion
} }),
rollupTerser = require("@rollup/plugin-terser"),
swc = require('gulp-swc'),
rollupSwc = require('rollup-plugin-swc3').swc,
rollupBanner = require("rollup-plugin-banner2"),
fs = require('fs'),
path = require('path'),
buffer = require('vinyl-buffer'),
rollupStream = require("@rollup/stream"),
pkg = require('./package.json'),
sass = require('gulp-sass')(require('sass')),
opts = process.argv.reduce((result, item) => {
if( item.indexOf('--') == 0 )
result[item.replace('--','')] = 1
return result;
}, {});
const LICENSE = fs.readFileSync("./LICENSE", "utf8");
var rollupCache = {};
const swcOptions = {
sourceMaps: true,
jsc: {
parser: {
syntax: 'ecmascript',
jsx: true, // Enable JSX
decorators: true, // Optionally enable decorators
},
transform: {
react: {
runtime: 'automatic', // Choose 'automatic' or 'classic'
pragma: 'React.createElement', // Customize if needed
pragmaFrag: 'React.Fragment', // Customize if needed
}
}
}
};
var banner = `
Tagify v${process.env.npm_package_version} - tags input component
By: ${pkg.author}
${pkg.homepage}
${LICENSE}
`;
var jQueryPluginWrap = [`;(function($){
// just a jQuery wrapper for the vanilla version of this component
$.fn.tagify = function(settings = {}){
return this.each(function() {
var $input = $(this),
tagify;
if( $input.data("tagify") ) // don't continue if already "tagified"
return this;
settings.isJQueryPlugin = true;
tagify = new Tagify($input[0], settings);
$input.data("tagify", tagify);
});
}
` , ` })(jQuery); `];
////////////////////////////////////////////////////
// Compile main app SCSS to CSS
function scss(){
return gulp.src('src/*.scss')
.pipe($.cssGlobbing({
extensions: '.scss'
}))
.pipe(
sass().on('error', sass.logError)
)
// .pipe($.combineMq()) // combine media queries
.pipe($.autoprefixer({ overrideBrowserslist:['> 5%'] }) )
.pipe($.cleanCss())
.pipe(gulp.dest('./dist'))
}
// https://medium.com/recraftrelic/building-a-react-component-as-a-npm-module-18308d4ccde9
function react(done){
return bundle({
entry: 'src/react.tagify.jsx',
outputName: `react.tagify.jsx`,
})
.on('end', done)
// return rollupStream({
// input: 'src/react.tagify.jsx',
// output: {
// sourcemap: true,
// name: 'Tagify',
// format: 'es'
// }
// })
// .pipe(
// swc(swcOptions)
// )
// .pipe($.headerComment(banner))
// .pipe($.concat('react.tagify.jsx'))
// .pipe($.sourcemaps.write('.'))
// .pipe( gulp.dest('./dist/') )
// .on('end', done);
return gulp.src('src/react.tagify.jsx', { sourcemaps: true })
// .pipe($.sourcemaps.init({ loadMaps: true }))
.pipe(swc(swcOptions))
// .pipe(opts.dev ? $.tap(()=>{}) : $.terser())
.pipe($.headerComment(banner))
.pipe($.concat('react.tagify.jsx'))
// .pipe($.sourcemaps.write('.'))
.pipe( gulp.dest('./dist/', { sourcemaps: '.' }) )
}
function js(done){
return bundle({
entry: 'src/tagify.js',
outputName: 'tagify.js'
})
.on('end', done)
}
function esm(done){
if( opts.dev ) return done();
return bundle({
entry: 'src/tagify.js',
outputName: 'tagify.esm.js',
format: 'es'
})
.on('end', done)
}
/**
* DEPRECATED - as of APR 2024, i've deciced it's not worth the efforts of generating this after recent gulpfile changes.
* wraps the output of the "js" task with "jQueryPluginWrap"
*/
function jquery(){
// do not proccess jQuery version while developeing
// if( opts.dev )
// return Promise.resolve('"dev" does not compile jQuery')
return gulp.src('dist/tagify.min.js')
.pipe($.insert.wrap(jQueryPluginWrap[0], jQueryPluginWrap[1]))
.pipe($.rename('jQuery.tagify.min.js'))
.pipe(opts.dev ? $.tap(()=>{}) : $.terser())
.pipe($.headerComment(banner))
.pipe(gulp.dest('./dist/'))
}
function polyfills(done){
return bundle({
entry: 'src/tagify.polyfills.js',
outputName: 'tagify.polyfills.min.js'
})
.on('end', done)
}
function bundle({ entry, outputName, dest, plugins=[], format='umd' }){
plugins = [
rollupSwc(swcOptions),
...plugins
]
if( !opts.dev ) {
plugins.push(rollupTerser())
}
plugins.push( rollupBanner(() => `/*${banner}*/\n\n`) )
// https://github.com/rollup/stream
return rollupStream({
input: entry,
plugins,
cache: rollupCache[entry],
output: {
sourcemap: true,
name: 'Tagify', // used only for UMD: https://rollupjs.org/configuration-options/#output-name
format: format
}
})
.on('bundle', function(bundle) {
rollupCache[entry] = bundle;
})
// give the file the name you want to output with
.pipe($.vinylSourceStream(outputName))
.pipe(buffer())
.on('error', handleError)
// NOTE - `$.sourcemaps` only works with Rollup v2. not 3 or 4!!! I've wasted a whole day over this
.pipe($.sourcemaps.init({ loadMaps: true }))
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest('./dist'));
}
function handleError(err) {
console.log( err.toString() );
this.emit('end');
}
/**
* Bumping version number and tagging the repository with it.
* Please read http://semver.org/
*
* You can use the commands
*
* gulp patch # makes v0.1.0 → v0.1.1
* gulp feature # makes v0.1.1 → v0.2.0
* gulp release # makes v0.2.1 → v1.0.0
*
* To bump the version numbers accordingly after you did a patch,
* introduced a feature or made a backwards-incompatible release.
*/
const inc = importance => () =>
// get all the files to bump version in
gulp.src('./package.json')
// bump the version number in those files
.pipe($.bump({type: importance}))
// save it back to filesystem
.pipe(gulp.dest('./'))
function gitTag(){
return gulp.src('./package.json')
// commit the changed version number
.pipe($.git.commit('bumps package version'))
.pipe($.tagVersion());
}
function addBanner(){
var packageJson = JSON.parse(fs.readFileSync('./package.json'))
var banner = `Tagify (v${packageJson.version}) - tags input component
By ${pkg.author.name}
${pkg.homepage}
${LICENSE}`;
return gulp.src('dist/*.js')
.pipe($.headerComment(banner))
.pipe(gulp.dest('./dist/'))
}
function compileAllExamples(done) {
// iterate all folders at ".\docs\examples\src"
fs.readdir('docs/examples/src', { withFileTypes: true }, (err, examples) => {
const subfolders = examples
.filter(file => file.isDirectory())
.map(folder => folder.name);
// generate an example html file from each subfolder
subfolders.forEach(compileExample)
})
typeof done == 'function' && done()
}
// compiles a specific example demo
function compileExample(exampleName) {
gulp.src('docs/examples/src/example-template.html')
.pipe($.replace('{{NAME}}', exampleName))
.pipe($.replace(/<!--\s*include:(.*?)\s*-->/g, (match, type) => {
try {
// Read the contents of the file specified in the comment
const fileContent = fs.readFileSync(`docs/examples/src/${exampleName}/${exampleName.replace(' ', '-')}.${type}`, 'utf8')
return fileContent;
} catch (err) {
return ''
}
}))
.pipe($.rename(exampleName + '.html'))
.pipe(gulp.dest('docs/examples/dist/'))
}
function onExampleFileChange(path) {
const normalizedPath = path.replace(/\\/g, '/')
const lastFolderName = normalizedPath.match(/\/([^\/]+)\/[^\/]+$/)[1]
compileExample(lastFolderName)
}
// creates the main `index.html` page which showcases all the examples
async function compileHomepage() {
const { nunjucksCompile } = await import('gulp-nunjucks');
// https://github.com/sindresorhus/gulp-nunjucks/issues/14
return gulp.src('./docs/homepage/index.html', {base: './docs'})
.pipe(nunjucksCompile()) // null, {path: [path.join(__dirname, '..')]}
.pipe($.rename('index.html'))
.pipe(gulp.dest('.'))
}
function watchExamples() {
gulp.watch(['./docs/examples/src/**/*.*', '!./docs/examples/src/*.*']).on('change', onExampleFileChange)
gulp.watch(['./docs/examples/src/*.*']).on('change', compileAllExamples)
}
function watch(){
gulp.watch('./src/*.scss', scss)
gulp.watch(['./src/tagify.js', './src/parts/*.js'], gulp.series([js]))
// gulp.watch('./src/react.tagify.jsx', react)
}
// remove the "react" task as it was unneeded because the react-wrapper is served unbundled
const build = gulp.series(gulp.parallel(js, scss, polyfills), esm, compileAllExamples, compileHomepage) // deprecated the "react" task as i believe it's not needed to consume a pre-bundled version.
exports.default = gulp.parallel(build, watch, watchExamples)
exports.js = js
exports.esm = esm
exports.build = build
// exports.react = react
exports.patch = gulp.series(inc('patch'), addBanner, gitTag) // () => inc('patch')
exports.feature = gulp.series(inc('minor'), addBanner, gitTag) // () => inc('minor')
exports.release = gulp.series(inc('major'), addBanner, gitTag) // () => inc('major')
exports.compileAllExamples = compileAllExamples