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. }
  164. }
  165. },
  166. svg2jinja: {
  167. all: {
  168. src: {
  169. 'warning': 'node_modules/ionicons/dist/svg/alert-outline.svg',
  170. 'close': 'node_modules/ionicons/dist/svg/close-outline.svg',
  171. 'chevron-up-outline': 'node_modules/ionicons/dist/svg/chevron-up-outline.svg',
  172. 'chevron-right': 'node_modules/ionicons/dist/svg/chevron-forward-outline.svg',
  173. 'chevron-left': 'node_modules/ionicons/dist/svg/chevron-back-outline.svg',
  174. 'menu-outline': 'node_modules/ionicons/dist/svg/menu-outline.svg',
  175. 'ellipsis-vertical-outline': 'node_modules/ionicons/dist/svg/ellipsis-vertical-outline.svg',
  176. 'magnet-outline': 'node_modules/ionicons/dist/svg/magnet-outline.svg',
  177. 'globe-outline': 'node_modules/ionicons/dist/svg/globe-outline.svg',
  178. 'search-outline': 'node_modules/ionicons/dist/svg/search-outline.svg',
  179. 'image-outline': 'node_modules/ionicons/dist/svg/image-outline.svg',
  180. 'play-outline': 'node_modules/ionicons/dist/svg/play-outline.svg',
  181. 'newspaper-outline': 'node_modules/ionicons/dist/svg/newspaper-outline.svg',
  182. 'location-outline': 'node_modules/ionicons/dist/svg/location-outline.svg',
  183. 'musical-notes-outline': 'node_modules/ionicons/dist/svg/musical-notes-outline.svg',
  184. 'layers-outline': 'node_modules/ionicons/dist/svg/layers-outline.svg',
  185. 'school-outline': 'node_modules/ionicons/dist/svg/school-outline.svg',
  186. 'file-tray-full-outline': 'node_modules/ionicons/dist/svg/file-tray-full-outline.svg',
  187. 'people-outline': 'node_modules/ionicons/dist/svg/people-outline.svg',
  188. },
  189. dest: '../../../templates/simple/icons.html',
  190. },
  191. },
  192. });
  193. grunt.registerMultiTask('svg2jinja', 'Create Jinja2 macro', function() {
  194. const ejs = require('ejs'), svgo = require('svgo');
  195. const icons = {}
  196. for(const iconName in this.data.src) {
  197. const svgFileName = this.data.src[iconName];
  198. try {
  199. const svgContent = grunt.file.read(svgFileName, { encoding: 'utf8' })
  200. const svgoResult = svgo.optimize(svgContent, {
  201. path: svgFileName,
  202. multipass: true,
  203. plugins: [
  204. {
  205. name: "removeTitle",
  206. },
  207. {
  208. name: "removeXMLNS",
  209. },
  210. {
  211. name: "addAttributesToSVGElement",
  212. params: {
  213. attributes: [
  214. { "aria-hidden": "true" }
  215. ]
  216. }
  217. }
  218. ]
  219. });
  220. icons[iconName] = svgoResult.data.replace("'", "\\'");
  221. } catch (err) {
  222. grunt.log.error(err);
  223. }
  224. }
  225. const template = `{# this file was generated by searx/static/themes/simple/gruntfile.js #}
  226. {%- set icons = {
  227. <% for (const iconName in icons) { %> '<%- iconName %>':'<%- icons[iconName] %>',
  228. <% } %>
  229. }
  230. -%}
  231. {% macro icon(action, alt) -%}
  232. {{ icons[action] | replace("ionicon", "ion-icon") | safe }}
  233. {%- endmacro %}
  234. {% macro icon_small(action) -%}
  235. {{ icons[action] | replace("ionicon", "ion-icon-small") | safe }}
  236. {%- endmacro %}
  237. {% macro icon_big(action, alt) -%}
  238. {{ icons[action] | replace("ionicon", "ion-icon-big") | safe }}
  239. {%- endmacro %}
  240. `;
  241. const result = ejs.render(template, { icons });
  242. grunt.file.write(this.data.dest, result, { encoding: 'utf8' });
  243. grunt.log.ok(this.data.dest + " created");
  244. });
  245. grunt.registerMultiTask('svg2png', 'Convert SVG to PNG', function () {
  246. const sharp = require('sharp'), done = this.async();
  247. eachAsync(this.files, async (file, _index, next) => {
  248. try {
  249. if (file.src.length != 1) {
  250. next("this task supports only one source per destination");
  251. }
  252. const info = await sharp(file.src[0])
  253. .png({
  254. force: true,
  255. compressionLevel: 9,
  256. palette: true,
  257. })
  258. .toFile(file.dest);
  259. grunt.log.ok(file.dest + ' created (' + info.size + ' bytes, ' + info.width + 'px * ' + info.height + 'px)');
  260. next();
  261. } catch (error) {
  262. grunt.fatal(error);
  263. next(error);
  264. }
  265. }, error => {
  266. if (error) {
  267. grunt.fatal(error);
  268. done(error);
  269. } else {
  270. done();
  271. }
  272. });
  273. });
  274. grunt.loadNpmTasks('grunt-contrib-watch');
  275. grunt.loadNpmTasks('grunt-contrib-copy');
  276. grunt.loadNpmTasks('grunt-contrib-uglify');
  277. grunt.loadNpmTasks('grunt-image');
  278. grunt.loadNpmTasks('grunt-contrib-jshint');
  279. grunt.loadNpmTasks('grunt-contrib-concat');
  280. grunt.loadNpmTasks('grunt-contrib-less');
  281. grunt.loadNpmTasks('grunt-contrib-cssmin');
  282. grunt.loadNpmTasks('grunt-stylelint');
  283. grunt.loadNpmTasks('grunt-eslint');
  284. grunt.registerTask('test', ['jshint']);
  285. grunt.registerTask('default', [
  286. 'eslint',
  287. 'stylelint',
  288. 'copy',
  289. 'concat',
  290. 'uglify',
  291. 'less:development',
  292. 'less:production',
  293. 'image',
  294. 'svg2png',
  295. 'svg2jinja',
  296. ]);
  297. };