gruntfile.js 8.2 KB

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