gruntfile.js 8.7 KB

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