gruntfile.js 8.8 KB

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