gruntfile.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. ]
  108. }
  109. }
  110. },
  111. less: {
  112. production: {
  113. options: {
  114. paths: ["less"],
  115. plugins: [
  116. new (require('less-plugin-clean-css'))()
  117. ],
  118. sourceMap: true,
  119. sourceMapURL: (name) => { const s = name.split('/'); return s[s.length - 1] + '.map'; },
  120. outputSourceFiles: true,
  121. },
  122. files: [
  123. {
  124. src: ['src/less/style-ltr.less'],
  125. dest: 'css/searxng.min.css',
  126. nonull: true,
  127. filter: file_exists,
  128. },
  129. {
  130. src: ['src/less/style-rtl.less'],
  131. dest: 'css/searxng-rtl.min.css',
  132. nonull: true,
  133. filter: file_exists,
  134. },
  135. {
  136. src: ['src/less/rss.less'],
  137. dest: 'css/rss.min.css',
  138. nonull: true,
  139. filter: file_exists,
  140. },
  141. ],
  142. },
  143. },
  144. image: {
  145. svg4web: {
  146. options: {
  147. svgo: ['--config', 'svg4web.svgo.js']
  148. },
  149. files: {
  150. '<%= _templates %>/simple/searxng-wordmark.min.svg': '<%= _brand %>/searxng-wordmark.svg',
  151. 'img/searxng.svg': '<%= _brand %>/searxng.svg',
  152. 'img/img_load_error.svg': '<%= _brand %>/img_load_error.svg'
  153. }
  154. },
  155. favicon: {
  156. options: {
  157. svgo: ['--config', 'svg4favicon.svgo.js']
  158. },
  159. files: {
  160. 'img/favicon.svg': '<%= _brand %>/searxng-wordmark.svg'
  161. }
  162. },
  163. },
  164. svg2png: {
  165. favicon: {
  166. files: {
  167. 'img/favicon.png': '<%= _brand %>/searxng-wordmark.svg',
  168. 'img/searxng.png': '<%= _brand %>/searxng.svg',
  169. }
  170. }
  171. },
  172. svg2jinja: {
  173. all: {
  174. src: {
  175. 'warning': 'node_modules/ionicons/dist/svg/alert-outline.svg',
  176. 'close': 'node_modules/ionicons/dist/svg/close-outline.svg',
  177. 'chevron-up-outline': 'node_modules/ionicons/dist/svg/chevron-up-outline.svg',
  178. 'chevron-right': 'node_modules/ionicons/dist/svg/chevron-forward-outline.svg',
  179. 'chevron-left': 'node_modules/ionicons/dist/svg/chevron-back-outline.svg',
  180. 'menu-outline': 'node_modules/ionicons/dist/svg/settings-outline.svg',
  181. 'ellipsis-vertical-outline': 'node_modules/ionicons/dist/svg/ellipsis-vertical-outline.svg',
  182. 'magnet-outline': 'node_modules/ionicons/dist/svg/magnet-outline.svg',
  183. 'globe-outline': 'node_modules/ionicons/dist/svg/globe-outline.svg',
  184. 'search-outline': 'node_modules/ionicons/dist/svg/search-outline.svg',
  185. 'image-outline': 'node_modules/ionicons/dist/svg/image-outline.svg',
  186. 'play-outline': 'node_modules/ionicons/dist/svg/play-outline.svg',
  187. 'newspaper-outline': 'node_modules/ionicons/dist/svg/newspaper-outline.svg',
  188. 'location-outline': 'node_modules/ionicons/dist/svg/location-outline.svg',
  189. 'musical-notes-outline': 'node_modules/ionicons/dist/svg/musical-notes-outline.svg',
  190. 'layers-outline': 'node_modules/ionicons/dist/svg/layers-outline.svg',
  191. 'school-outline': 'node_modules/ionicons/dist/svg/school-outline.svg',
  192. 'file-tray-full-outline': 'node_modules/ionicons/dist/svg/file-tray-full-outline.svg',
  193. 'people-outline': 'node_modules/ionicons/dist/svg/people-outline.svg',
  194. 'heart-outline': 'node_modules/ionicons/dist/svg/heart-outline.svg',
  195. 'information-circle-outline': 'src/svg/information-circle-outline.svg',
  196. },
  197. dest: '../../../templates/simple/icons.html',
  198. },
  199. },
  200. });
  201. grunt.registerMultiTask('svg2jinja', 'Create Jinja2 macro', function () {
  202. const ejs = require('ejs'), svgo = require('svgo');
  203. const icons = {}
  204. for (const iconName in this.data.src) {
  205. const svgFileName = this.data.src[iconName];
  206. try {
  207. const svgContent = grunt.file.read(svgFileName, { encoding: 'utf8' })
  208. const svgoResult = svgo.optimize(svgContent, {
  209. path: svgFileName,
  210. multipass: true,
  211. plugins: [
  212. {
  213. name: "removeTitle",
  214. },
  215. {
  216. name: "removeXMLNS",
  217. },
  218. {
  219. name: "addAttributesToSVGElement",
  220. params: {
  221. attributes: [
  222. { "class": "ionicon", "aria-hidden": "true" }
  223. ]
  224. }
  225. }
  226. ]
  227. });
  228. icons[iconName] = svgoResult.data.replace("'", "\\'");
  229. } catch (err) {
  230. grunt.log.error(err);
  231. }
  232. }
  233. const template = `{# this file was generated by searx/static/themes/simple/gruntfile.js #}
  234. {%- set icons = {
  235. <% for (const iconName in icons) { %> '<%- iconName %>':'<%- icons[iconName] %>',
  236. <% } %>
  237. }
  238. -%}
  239. {% macro icon(action, alt) -%}
  240. {{ icons[action] | replace("ionicon", "ion-icon") | safe }}
  241. {%- endmacro %}
  242. {% macro icon_small(action) -%}
  243. {{ icons[action] | replace("ionicon", "ion-icon-small") | safe }}
  244. {%- endmacro %}
  245. {% macro icon_big(action, alt) -%}
  246. {{ icons[action] | replace("ionicon", "ion-icon-big") | safe }}
  247. {%- endmacro %}
  248. `;
  249. const result = ejs.render(template, { icons });
  250. grunt.file.write(this.data.dest, result, { encoding: 'utf8' });
  251. grunt.log.ok(this.data.dest + " created");
  252. });
  253. grunt.registerMultiTask('svg2png', 'Convert SVG to PNG', function () {
  254. const sharp = require('sharp'), done = this.async();
  255. eachAsync(this.files, async (file, _index, next) => {
  256. try {
  257. if (file.src.length != 1) {
  258. next("this task supports only one source per destination");
  259. }
  260. const info = await sharp(file.src[0])
  261. .png({
  262. force: true,
  263. compressionLevel: 9,
  264. palette: true,
  265. })
  266. .toFile(file.dest);
  267. grunt.log.ok(file.dest + ' created (' + info.size + ' bytes, ' + info.width + 'px * ' + info.height + 'px)');
  268. next();
  269. } catch (error) {
  270. grunt.fatal(error);
  271. next(error);
  272. }
  273. }, error => {
  274. if (error) {
  275. grunt.fatal(error);
  276. done(error);
  277. } else {
  278. done();
  279. }
  280. });
  281. });
  282. grunt.loadNpmTasks('grunt-contrib-watch');
  283. grunt.loadNpmTasks('grunt-contrib-copy');
  284. grunt.loadNpmTasks('grunt-contrib-uglify');
  285. grunt.loadNpmTasks('grunt-image');
  286. grunt.loadNpmTasks('grunt-contrib-less');
  287. grunt.loadNpmTasks('grunt-contrib-cssmin');
  288. grunt.loadNpmTasks('grunt-stylelint');
  289. grunt.loadNpmTasks('grunt-eslint');
  290. grunt.registerTask('test', ['eslint', 'stylelint']);
  291. grunt.registerTask('default', [
  292. 'eslint',
  293. 'stylelint',
  294. 'copy',
  295. 'uglify',
  296. 'less',
  297. 'image',
  298. 'svg2png',
  299. 'svg2jinja',
  300. ]);
  301. };