gruntfile.js 8.2 KB

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