gruntfile.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*jshint esversion: 6 */
  2. module.exports = function(grunt) {
  3. grunt.initConfig({
  4. _brand: '../../../../src/brand',
  5. _templates: '../../../templates',
  6. pkg: grunt.file.readJSON('package.json'),
  7. watch: {
  8. scripts: {
  9. files: ['gruntfile.js', 'src/**'],
  10. tasks: [
  11. 'eslint',
  12. 'copy',
  13. 'concat',
  14. 'uglify',
  15. 'less:development',
  16. 'less:production',
  17. 'image',
  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. concat: {
  78. head_and_body: {
  79. options: {
  80. separator: ';'
  81. },
  82. files: {
  83. 'js/searxng.head.js': ['src/js/head/*.js'],
  84. 'js/searxng.js': [
  85. 'src/js/main/*.js',
  86. '../__common__/js/*.js',
  87. './node_modules/autocomplete-js/dist/autocomplete.js'
  88. ]
  89. }
  90. }
  91. },
  92. uglify: {
  93. options: {
  94. output: {
  95. comments: 'some'
  96. },
  97. ie8: false,
  98. warnings: true,
  99. compress: false,
  100. mangle: true,
  101. sourceMap: true
  102. },
  103. dist: {
  104. files: {
  105. 'js/searxng.head.min.js': ['js/searxng.head.js'],
  106. 'js/searxng.min.js': ['js/searxng.js']
  107. }
  108. }
  109. },
  110. less: {
  111. development: {
  112. options: {
  113. paths: ["less"],
  114. },
  115. files: {
  116. "css/searxng.css": "src/less/style.less",
  117. "css/searxng-rtl.css": "src/less/style-rtl.less"
  118. }
  119. },
  120. production: {
  121. options: {
  122. paths: ["less"],
  123. plugins: [
  124. new (require('less-plugin-clean-css'))()
  125. ],
  126. sourceMap: true,
  127. sourceMapURL: (name) => { const s = name.split('/'); return s[s.length - 1] + '.map';},
  128. outputSourceFiles: false,
  129. sourceMapRootpath: '../',
  130. },
  131. files: {
  132. "css/searxng.min.css": "src/less/style.less",
  133. "css/searxng-rtl.min.css": "src/less/style-rtl.less"
  134. }
  135. },
  136. },
  137. image: {
  138. svg4web: {
  139. options: {
  140. svgo: ['--config', 'svg4web.svgo.js']
  141. },
  142. files: {
  143. '<%= _templates %>/__common__/searxng-wordmark.min.svg': '<%= _brand %>/searxng-wordmark.svg'
  144. }
  145. }
  146. },
  147. svg2jinja: {
  148. all: {
  149. src: {
  150. 'warning': 'node_modules/ionicons/dist/svg/alert-outline.svg',
  151. 'close': 'node_modules/ionicons/dist/svg/close-outline.svg',
  152. 'chevron-up-outline': 'node_modules/ionicons/dist/svg/chevron-up-outline.svg',
  153. 'chevron-right': 'node_modules/ionicons/dist/svg/chevron-forward-outline.svg',
  154. 'chevron-left': 'node_modules/ionicons/dist/svg/chevron-back-outline.svg',
  155. 'menu-outline': 'node_modules/ionicons/dist/svg/menu-outline.svg',
  156. 'ellipsis-vertical-outline': 'node_modules/ionicons/dist/svg/ellipsis-vertical-outline.svg',
  157. 'magnet-outline': 'node_modules/ionicons/dist/svg/magnet-outline.svg',
  158. 'globe-outline': 'node_modules/ionicons/dist/svg/globe-outline.svg',
  159. 'search-outline': 'node_modules/ionicons/dist/svg/search-outline.svg',
  160. 'image-outline': 'node_modules/ionicons/dist/svg/image-outline.svg',
  161. 'play-outline': 'node_modules/ionicons/dist/svg/play-outline.svg',
  162. 'newspaper-outline': 'node_modules/ionicons/dist/svg/newspaper-outline.svg',
  163. 'location-outline': 'node_modules/ionicons/dist/svg/location-outline.svg',
  164. 'musical-notes-outline': 'node_modules/ionicons/dist/svg/musical-notes-outline.svg',
  165. 'layers-outline': 'node_modules/ionicons/dist/svg/layers-outline.svg',
  166. 'school-outline': 'node_modules/ionicons/dist/svg/school-outline.svg',
  167. 'file-tray-full-outline': 'node_modules/ionicons/dist/svg/file-tray-full-outline.svg',
  168. 'people-outline': 'node_modules/ionicons/dist/svg/people-outline.svg',
  169. },
  170. dest: '../../../templates/simple/icons.html',
  171. },
  172. },
  173. });
  174. grunt.registerMultiTask('svg2jinja', 'Create Jinja2 macro', function() {
  175. const ejs = require('ejs'), svgo = require('svgo');
  176. const icons = {}
  177. for(const iconName in this.data.src) {
  178. const svgFileName = this.data.src[iconName];
  179. try {
  180. const svgContent = grunt.file.read(svgFileName, { encoding: 'utf8' })
  181. const svgoResult = svgo.optimize(svgContent, {
  182. path: svgFileName,
  183. multipass: true,
  184. plugins: [
  185. {
  186. name: "removeTitle",
  187. },
  188. {
  189. name: "removeXMLNS",
  190. },
  191. {
  192. name: "addAttributesToSVGElement",
  193. params: {
  194. attributes: [
  195. { "aria-hidden": "true" }
  196. ]
  197. }
  198. }
  199. ]
  200. });
  201. icons[iconName] = svgoResult.data.replace("'", "\\'");
  202. } catch (err) {
  203. grunt.log.error(err);
  204. }
  205. }
  206. const template = `{# this file was generated by searx/static/themes/simple/gruntfile.js #}
  207. {%- set icons = {
  208. <% for (const iconName in icons) { %> '<%- iconName %>':'<%- icons[iconName] %>',
  209. <% } %>
  210. }
  211. -%}
  212. {% macro icon(action, alt) -%}
  213. {{ icons[action] | replace("ionicon", "ion-icon") | safe }}
  214. {%- endmacro %}
  215. {% macro icon_small(action) -%}
  216. {{ icons[action] | replace("ionicon", "ion-icon-small") | safe }}
  217. {%- endmacro %}
  218. {% macro icon_big(action, alt) -%}
  219. {{ icons[action] | replace("ionicon", "ion-icon-big") | safe }}
  220. {%- endmacro %}
  221. `;
  222. const result = ejs.render(template, { icons });
  223. grunt.file.write(this.data.dest, result, { encoding: 'utf8' });
  224. grunt.log.ok(this.data.dest + " created");
  225. });
  226. grunt.loadNpmTasks('grunt-contrib-watch');
  227. grunt.loadNpmTasks('grunt-contrib-copy');
  228. grunt.loadNpmTasks('grunt-contrib-uglify');
  229. grunt.loadNpmTasks('grunt-image');
  230. grunt.loadNpmTasks('grunt-contrib-jshint');
  231. grunt.loadNpmTasks('grunt-contrib-concat');
  232. grunt.loadNpmTasks('grunt-contrib-less');
  233. grunt.loadNpmTasks('grunt-contrib-cssmin');
  234. grunt.loadNpmTasks('grunt-stylelint');
  235. grunt.loadNpmTasks('grunt-eslint');
  236. grunt.registerTask('test', ['jshint']);
  237. grunt.registerTask('default', [
  238. 'eslint',
  239. 'stylelint',
  240. 'copy',
  241. 'concat',
  242. 'uglify',
  243. 'less:development',
  244. 'less:production',
  245. 'image',
  246. 'svg2jinja',
  247. ]);
  248. };