gruntfile.js 8.2 KB

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