gruntfile.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* SPDX-License-Identifier: AGPL-3.0-or-later */
  2. module.exports = function (grunt) {
  3. const eachAsync = require('each-async');
  4. function file_exists (filepath) {
  5. // filter function to exit grunt task with error if a (src) file not exists
  6. if (!grunt.file.exists(filepath)) {
  7. grunt.fail.fatal('Could not find: ' + filepath, 42);
  8. } else {
  9. return true;
  10. }
  11. }
  12. grunt.initConfig({
  13. _brand: '../../../../src/brand',
  14. _templates: '../../../templates',
  15. pkg: grunt.file.readJSON('package.json'),
  16. watch: {
  17. scripts: {
  18. files: ['gruntfile.js', 'eslint.config.mjs', '.stylelintrc.json', 'src/**'],
  19. tasks: [
  20. 'eslint',
  21. 'stylelint',
  22. 'copy',
  23. 'uglify',
  24. 'less',
  25. 'image',
  26. 'svg2png',
  27. 'svg2jinja'
  28. ]
  29. }
  30. },
  31. eslint: {
  32. options: {
  33. overrideConfigFile: 'eslint.config.mjs',
  34. failOnError: true,
  35. fix: grunt.option('fix')
  36. },
  37. target: [
  38. 'gruntfile.js',
  39. 'svg4web.svgo.js',
  40. 'src/js/main/*.js',
  41. 'src/js/head/*.js',
  42. ],
  43. },
  44. stylelint: {
  45. options: {
  46. formatter: 'unix',
  47. fix: grunt.option('fix')
  48. },
  49. src: [
  50. 'src/less/**/*.less',
  51. ]
  52. },
  53. copy: {
  54. js: {
  55. expand: true,
  56. cwd: './node_modules',
  57. dest: './js/',
  58. flatten: true,
  59. filter: 'isFile',
  60. timestamp: true,
  61. src: [
  62. './leaflet/dist/leaflet.js',
  63. ]
  64. },
  65. css: {
  66. expand: true,
  67. cwd: './node_modules',
  68. dest: './css/',
  69. flatten: true,
  70. filter: 'isFile',
  71. timestamp: true,
  72. src: [
  73. './leaflet/dist/leaflet.css',
  74. ]
  75. },
  76. leaflet_images: {
  77. expand: true,
  78. cwd: './node_modules',
  79. dest: './css/images/',
  80. flatten: true,
  81. filter: 'isFile',
  82. timestamp: true,
  83. src: [
  84. './leaflet/dist/images/*.png',
  85. ]
  86. },
  87. },
  88. uglify: {
  89. options: {
  90. output: {
  91. comments: 'some'
  92. },
  93. ie8: false,
  94. warnings: true,
  95. compress: false,
  96. mangle: true,
  97. sourceMap: {
  98. includeSources: true
  99. }
  100. },
  101. dist: {
  102. files: {
  103. 'js/searxng.head.min.js': ['src/js/head/*.js'],
  104. 'js/searxng.min.js': [
  105. 'src/js/main/*.js',
  106. './node_modules/autocomplete-js/dist/autocomplete.js',
  107. './node_modules/swiped-events/src/swiped-events.js'
  108. ]
  109. }
  110. }
  111. },
  112. less: {
  113. production: {
  114. options: {
  115. paths: ["less"],
  116. sourceMap: true,
  117. sourceMapURL: (name) => { const s = name.split('/'); return s[s.length - 1] + '.map'; },
  118. outputSourceFiles: true,
  119. },
  120. files: [
  121. {
  122. src: ['src/less/style-ltr.less'],
  123. dest: 'css/searxng.min.css',
  124. nonull: true,
  125. filter: file_exists,
  126. },
  127. {
  128. src: ['src/less/style-rtl.less'],
  129. dest: 'css/searxng-rtl.min.css',
  130. nonull: true,
  131. filter: file_exists,
  132. },
  133. {
  134. src: ['src/less/rss.less'],
  135. dest: 'css/rss.min.css',
  136. nonull: true,
  137. filter: file_exists,
  138. },
  139. ],
  140. },
  141. },
  142. image: {
  143. svg4web: {
  144. options: {
  145. svgo: ['--config', 'svg4web.svgo.js']
  146. },
  147. files: {
  148. '<%= _templates %>/simple/searxng-wordmark.min.svg': '<%= _brand %>/searxng-wordmark.svg',
  149. 'img/searxng.svg': '<%= _brand %>/searxng.svg',
  150. 'img/img_load_error.svg': '<%= _brand %>/img_load_error.svg'
  151. }
  152. },
  153. favicon: {
  154. options: {
  155. svgo: ['--config', 'svg4favicon.svgo.js']
  156. },
  157. files: {
  158. 'img/favicon.svg': '<%= _brand %>/searxng-wordmark.svg'
  159. }
  160. },
  161. },
  162. svg2png: {
  163. favicon: {
  164. files: {
  165. 'img/favicon.png': '<%= _brand %>/searxng-wordmark.svg',
  166. 'img/searxng.png': '<%= _brand %>/searxng.svg',
  167. }
  168. }
  169. },
  170. svg2jinja: {
  171. all: {
  172. src: {
  173. 'warning': 'node_modules/ionicons/dist/svg/alert-outline.svg',
  174. 'close': 'node_modules/ionicons/dist/svg/close-outline.svg',
  175. 'chevron-up-outline': 'node_modules/ionicons/dist/svg/chevron-up-outline.svg',
  176. 'chevron-right': 'node_modules/ionicons/dist/svg/chevron-forward-outline.svg',
  177. 'chevron-left': 'node_modules/ionicons/dist/svg/chevron-back-outline.svg',
  178. 'menu-outline': 'node_modules/ionicons/dist/svg/settings-outline.svg',
  179. 'ellipsis-vertical-outline': 'node_modules/ionicons/dist/svg/ellipsis-vertical-outline.svg',
  180. 'magnet-outline': 'node_modules/ionicons/dist/svg/magnet-outline.svg',
  181. 'globe-outline': 'node_modules/ionicons/dist/svg/globe-outline.svg',
  182. 'search-outline': 'node_modules/ionicons/dist/svg/search-outline.svg',
  183. 'image-outline': 'node_modules/ionicons/dist/svg/image-outline.svg',
  184. 'play-outline': 'node_modules/ionicons/dist/svg/play-outline.svg',
  185. 'newspaper-outline': 'node_modules/ionicons/dist/svg/newspaper-outline.svg',
  186. 'location-outline': 'node_modules/ionicons/dist/svg/location-outline.svg',
  187. 'musical-notes-outline': 'node_modules/ionicons/dist/svg/musical-notes-outline.svg',
  188. 'layers-outline': 'node_modules/ionicons/dist/svg/layers-outline.svg',
  189. 'school-outline': 'node_modules/ionicons/dist/svg/school-outline.svg',
  190. 'file-tray-full-outline': 'node_modules/ionicons/dist/svg/file-tray-full-outline.svg',
  191. 'people-outline': 'node_modules/ionicons/dist/svg/people-outline.svg',
  192. 'heart-outline': 'node_modules/ionicons/dist/svg/heart-outline.svg',
  193. 'information-circle-outline': 'src/svg/information-circle-outline.svg',
  194. },
  195. dest: '../../../templates/simple/icons.html',
  196. },
  197. },
  198. });
  199. grunt.registerMultiTask('svg2jinja', 'Create Jinja2 macro', function () {
  200. const ejs = require('ejs'), svgo = require('svgo');
  201. const icons = {}
  202. for (const iconName in this.data.src) {
  203. const svgFileName = this.data.src[iconName];
  204. try {
  205. const svgContent = grunt.file.read(svgFileName, { encoding: 'utf8' })
  206. const svgoResult = svgo.optimize(svgContent, {
  207. path: svgFileName,
  208. multipass: true,
  209. plugins: [
  210. {
  211. name: "removeTitle",
  212. },
  213. {
  214. name: "removeXMLNS",
  215. },
  216. {
  217. name: "addAttributesToSVGElement",
  218. params: {
  219. attributes: [
  220. { "class": "ionicon", "aria-hidden": "true" }
  221. ]
  222. }
  223. }
  224. ]
  225. });
  226. icons[iconName] = svgoResult.data.replace("'", "\\'");
  227. } catch (err) {
  228. grunt.log.error(err);
  229. }
  230. }
  231. const template = `{# this file was generated by searx/static/themes/simple/gruntfile.js #}
  232. {%- set icons = {
  233. <% for (const iconName in icons) { %> '<%- iconName %>':'<%- icons[iconName] %>',
  234. <% } %>
  235. }
  236. -%}
  237. {% macro icon(action, alt) -%}
  238. {{ icons[action] | replace("ionicon", "ion-icon") | safe }}
  239. {%- endmacro %}
  240. {% macro icon_small(action) -%}
  241. {{ icons[action] | replace("ionicon", "ion-icon-small") | safe }}
  242. {%- endmacro %}
  243. {% macro icon_big(action, alt) -%}
  244. {{ icons[action] | replace("ionicon", "ion-icon-big") | safe }}
  245. {%- endmacro %}
  246. `;
  247. const result = ejs.render(template, { icons });
  248. grunt.file.write(this.data.dest, result, { encoding: 'utf8' });
  249. grunt.log.ok(this.data.dest + " created");
  250. });
  251. grunt.registerMultiTask('svg2png', 'Convert SVG to PNG', function () {
  252. const sharp = require('sharp'), done = this.async();
  253. eachAsync(this.files, async (file, _index, next) => {
  254. try {
  255. if (file.src.length != 1) {
  256. next("this task supports only one source per destination");
  257. }
  258. const info = await sharp(file.src[0])
  259. .png({
  260. force: true,
  261. compressionLevel: 9,
  262. palette: true,
  263. })
  264. .toFile(file.dest);
  265. grunt.log.ok(file.dest + ' created (' + info.size + ' bytes, ' + info.width + 'px * ' + info.height + 'px)');
  266. next();
  267. } catch (error) {
  268. grunt.fatal(error);
  269. next(error);
  270. }
  271. }, error => {
  272. if (error) {
  273. grunt.fatal(error);
  274. done(error);
  275. } else {
  276. done();
  277. }
  278. });
  279. });
  280. grunt.loadNpmTasks('grunt-contrib-watch');
  281. grunt.loadNpmTasks('grunt-contrib-copy');
  282. grunt.loadNpmTasks('grunt-contrib-uglify');
  283. grunt.loadNpmTasks('grunt-image');
  284. grunt.loadNpmTasks('grunt-contrib-less');
  285. grunt.loadNpmTasks('grunt-contrib-cssmin');
  286. grunt.loadNpmTasks('grunt-stylelint');
  287. grunt.loadNpmTasks('grunt-eslint');
  288. grunt.registerTask('test', ['eslint', 'stylelint']);
  289. grunt.registerTask('default', [
  290. 'eslint',
  291. 'stylelint',
  292. 'copy',
  293. 'uglify',
  294. 'less',
  295. 'image',
  296. 'svg2png',
  297. 'svg2jinja',
  298. ]);
  299. };