forked from orchidjs/tom-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
346 lines (299 loc) · 7.98 KB
/
Gruntfile.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
var fs = require('fs');
var path = require('path');
var process = require('process');
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-replace');
grunt.loadNpmTasks('@lodder/grunt-postcss');
const sass = require('sass');
require('load-grunt-tasks')(grunt); //sass
grunt.registerTask('default', [
'build'
]);
grunt.registerTask('build', [
'clean:library',
'shell:buildjs',
'copy:scss',
'copy:scss_plugins',
'sass:build',
'postcss:prefix',
'postcss:min',
'replace:css_post',
'replace:scss_plugin_paths'
]);
grunt.registerTask('serve', [
'build',
'builddocs',
'connect',
'check_doc_links',
'watch'
])
grunt.registerTask('builddocs',[
'clean:builddocs',
'shell:builddocs',
'shell:rollupdocs',
'replace:builddocs',
'sass:builddocs',
'postcss:builddocs',
]);
/**
* Check generated docs for broken links
* https://www.npmjs.com/package/broken-link-checker
*/
grunt.registerTask('check_doc_links','',function(){
var done = this.async();
const {SiteChecker} = require('broken-link-checker');
const options = {
excludeExternalLinks: true,
cacheMaxAge:60,
};
var urls_checked = 0;
var links_checked = 0;
var failures = 0;
const handlers = {
error:function(error){
failures++;
console.log('error',error);
},
page:function(error, page_url, customData){
if( error ){
failures++;
console.log('error!',page_url);
}
urls_checked++;
},
junk:function( result, data ){
links_checked++;
if( result.broken ){
failures++;
console.log('broken junk found',result);
}
},
link:function(link){
if( link.broken ){
failures++;
console.log('broken link',link);
}
},
end:function(){
console.log('urls checked',urls_checked);
console.log('links checked',links_checked);
console.log('failures',failures);
done(failures==0);
}
};
const checker = new SiteChecker(options,handlers)
checker.enqueue('http://localhost:8000/', {});
});
// build tom-select.custom.js
var plugin_arg = grunt.option('plugins');
var custom_file = path.resolve( process.cwd(),'./src/tom-select.custom.ts');
var custom_content = ['/* this file is generated by grunt when calling `npm run build -- --plugins=<plugins> */','import TomSelect from "./tom-select";'];
if( fs.existsSync(custom_file) ){
fs.unlink(custom_file,err => {
if (err) {
console.error(err)
}
});
}
if( plugin_arg ){
var plugin_args = plugin_arg.split(/\s*,\s*/);
plugin_args.map((plugin_name) => {
custom_content.push(`import ${plugin_name} from './plugins/${plugin_name}/plugin.js';`);
});
plugin_args.map((plugin_name) => {
custom_content.push(`TomSelect.define('${plugin_name}', ${plugin_name});`);
});
custom_content.push('export default TomSelect;');
fs.writeFile(custom_file, custom_content.join("\n"),err => {
if (err) {
console.error(err)
}
});
}
// find all plugin scss files
var scss_plugin_files = [];
var matched_files = grunt.file.expand(['src/plugins/*/plugin.scss']);
for (var i = 0, n = matched_files.length; i < n; i++) {
var plugin_name = matched_files[i].match(/src\/plugins\/(.+?)\//)[1];
scss_plugin_files.push({src: matched_files[i], dest: 'build/scss/plugins/' + plugin_name + '.scss'});
}
// bootstrap browserlist https://github.com/twbs/bootstrap/blob/main/.browserslistrc
var autoprefixer = require('autoprefixer')();
var version_replace_options = {
prefix: '//@@',
variables: {
'version': '<%= pkg.version %>',
}
};
var scss_plugin_path_replace_options = {
patterns: [{
match: /\.\.\/plugins\/(.+?)\/plugin\.scss/g,
replacement: './plugins/$1.scss'
}],
usePrefix: false
};
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// delete old build files
clean: {
library: ['build/scss/*','build/js/*','build/esm/*','build/css/*','build/cjs/*'],
builddocs: ['build-docs/*']
},
// copy scss files to build folder
copy: {
scss:{
files: [{
'build/scss/tom-select.scss': ['src/scss/tom-select.scss'],
'build/scss/tom-select.default.scss': ['src/scss/tom-select.default.scss'],
'build/scss/tom-select.bootstrap4.scss': ['src/scss/tom-select.bootstrap4.scss'],
'build/scss/tom-select.bootstrap5.scss': ['src/scss/tom-select.bootstrap5.scss'],
'build/scss/_dropdown.scss': ['src/scss/_dropdown.scss'],
'build/scss/_items.scss': ['src/scss/_items.scss'],
}]
},
scss_plugins:{
files: scss_plugin_files
},
},
// replace //@@version with current package version
replace: {
// add version to css & scss headers
css_post: {
options: version_replace_options,
files: [
{expand: true, flatten: false, src: ['build/css/*.css'], dest: ''},
{expand: true, flatten: false, src: ['build/scss/*.scss'], dest: ''},
]
},
builddocs:{
options: version_replace_options,
files:[
{src:['build-docs/js/index.bundle.js'],dest:'build-docs/js/index.bundle.js'},
{src:['build-docs/index.html'],dest:'build-docs/index.html'}
]
},
scss_plugin_paths: {
options: scss_plugin_path_replace_options,
files: [{expand: true, flatten: false, src: ['build/scss/tom-select.scss'], dest: ''}]
},
},
// compile css from scss
sass: {
options:{
implementation: sass,
style:'expanded',
},
build: {
files: [{
'build/css/tom-select.css': ['src/scss/tom-select.scss'],
'build/css/tom-select.default.css': ['src/scss/tom-select.default.scss'],
'build/css/tom-select.bootstrap4.css': ['src/scss/-tom-select.bootstrap4.scss'],
'build/css/tom-select.bootstrap5.css': ['src/scss/-tom-select.bootstrap5.scss'],
}]
},
builddocs: {
files: [{
expand: true,
flatten: true,
ext: '.css',
src: ['doc_src/css/*.scss'],
dest: 'build-docs/css'
}],
}
},
// autoprefix && cssnanao
postcss: {
prefix: {
options:{
map: {
inline: false, // save all sourcemaps as separate files...
},
processors: [
//require('pixrem')(), // add fallbacks for rem units
autoprefixer,
]
},
files: [{expand: true, flatten: false, src: ['build/css/*.css'], dest: ''}],
},
min: {
options: {
map: {
inline: false, // save all sourcemaps as separate files...
},
processors: [
require('cssnano')() // minify the result
]
},
files: [{
'build/css/tom-select.min.css': ['build/css/tom-select.css'],
'build/css/tom-select.default.min.css': ['build/css/tom-select.default.css'],
'build/css/tom-select.bootstrap4.min.css': ['build/css/tom-select.bootstrap4.css'],
'build/css/tom-select.bootstrap5.min.css': ['build/css/tom-select.bootstrap5.css'],
}]
},
builddocs:{
options:{
map: {
inline: false, // save all sourcemaps as separate files...
},
processors: [
autoprefixer,
require('cssnano')() // minify the result
]
},
files: [{
expand: true,
flatten: true,
src: ['build-docs/css/*.css'],
dest: 'build-docs/css'
}],
},
},
// run server at http://localhost:8000 to view documentation and run examples
connect: {
server:{
options: {
base: 'build-docs',
}
}
},
// generate /build-docs
shell: {
builddocs: {
command: 'npx @11ty/eleventy --config=.config/eleventy.js',
},
rollupdocs: {
command: 'npx rollup -c .config/rollup.docs.mjs',
},
buildjs: {
command: 'npx rollup -c .config/rollup.config.mjs',
},
},
watch: {
// changes to files in /doc_src: rebuild all of documentation
docs:{
files:[
'doc_src/**',
],
tasks:[
'builddocs',
'check_doc_links',
]
},
// changes to files in /src: rebuild library and copy to docs
src:{
files: [
'src/**',
],
tasks: [
'build',
'shell:builddocs',
]
}
}
});
};