gruntfile.js 8.6 KB

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