gruntfile.js 8.4 KB

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