gruntfile.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*jshint esversion: 6 */
  2. module.exports = function(grunt) {
  3. const path = require('path');
  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: ['eslint', 'copy', 'concat', 'svg2jinja', 'uglify', 'image', 'less:development', 'less:production']
  12. }
  13. },
  14. eslint: {
  15. options: {
  16. overrideConfigFile: '.eslintrc.json',
  17. failOnError: false
  18. },
  19. target: [
  20. 'svg4web.svgo.js',
  21. 'src/js/main/*.js',
  22. 'src/js/head/*.js',
  23. '../__common__/js/*.js'
  24. ],
  25. },
  26. stylelint: {
  27. options: {
  28. formatter: 'unix',
  29. },
  30. src: [
  31. 'src/less/**/*.less',
  32. ]
  33. },
  34. copy: {
  35. js: {
  36. expand: true,
  37. cwd: './node_modules',
  38. dest: './js/',
  39. flatten: true,
  40. filter: 'isFile',
  41. timestamp: true,
  42. src: [
  43. './leaflet/dist/leaflet.js',
  44. ]
  45. },
  46. css: {
  47. expand: true,
  48. cwd: './node_modules',
  49. dest: './css/',
  50. flatten: true,
  51. filter: 'isFile',
  52. timestamp: true,
  53. src: [
  54. './leaflet/dist/leaflet.css',
  55. ]
  56. },
  57. leaflet_images: {
  58. expand: true,
  59. cwd: './node_modules',
  60. dest: './css/images/',
  61. flatten: true,
  62. filter: 'isFile',
  63. timestamp: true,
  64. src: [
  65. './leaflet/dist/images/*.png',
  66. ]
  67. },
  68. },
  69. concat: {
  70. head_and_body: {
  71. options: {
  72. separator: ';'
  73. },
  74. files: {
  75. 'js/searxng.head.js': ['src/js/head/*.js'],
  76. 'js/searxng.js': ['src/js/main/*.js', '../__common__/js/*.js', './node_modules/autocomplete-js/dist/autocomplete.js']
  77. }
  78. }
  79. },
  80. uglify: {
  81. options: {
  82. output: {
  83. comments: 'some'
  84. },
  85. ie8: false,
  86. warnings: true,
  87. compress: false,
  88. mangle: true,
  89. sourceMap: true
  90. },
  91. dist: {
  92. files: {
  93. 'js/searxng.head.min.js': ['js/searxng.head.js'],
  94. 'js/searxng.min.js': ['js/searxng.js']
  95. }
  96. }
  97. },
  98. image: {
  99. svg4web: {
  100. options: {
  101. svgo: ['--config', 'svg4web.svgo.js']
  102. },
  103. files: {
  104. '<%= _templates %>/__common__/searxng-wordmark.min.svg': '<%= _brand %>/searxng-wordmark.svg'
  105. }
  106. }
  107. },
  108. less: {
  109. development: {
  110. options: {
  111. paths: ["less"],
  112. },
  113. files: {
  114. "css/searxng.css": "src/less/style.less",
  115. "css/searxng-rtl.css": "src/less/style-rtl.less"
  116. }
  117. },
  118. production: {
  119. options: {
  120. paths: ["less"],
  121. plugins: [
  122. new (require('less-plugin-clean-css'))()
  123. ],
  124. sourceMap: true,
  125. sourceMapURL: (name) => { const s = name.split('/'); return s[s.length - 1] + '.map';},
  126. outputSourceFiles: false,
  127. sourceMapRootpath: '../',
  128. },
  129. files: {
  130. "css/searxng.min.css": "src/less/style.less",
  131. "css/searxng-rtl.min.css": "src/less/style-rtl.less"
  132. }
  133. },
  134. },
  135. svg2jinja: {
  136. all: {
  137. src: {
  138. 'warning': 'node_modules/ionicons/dist/svg/alert-outline.svg',
  139. 'close': 'node_modules/ionicons/dist/svg/close-outline.svg',
  140. 'chevron-up-outline': 'node_modules/ionicons/dist/svg/chevron-up-outline.svg',
  141. 'chevron-right': 'node_modules/ionicons/dist/svg/chevron-forward-outline.svg',
  142. 'chevron-left': 'node_modules/ionicons/dist/svg/chevron-back-outline.svg',
  143. 'menu-outline': 'node_modules/ionicons/dist/svg/menu-outline.svg',
  144. 'ellipsis-vertical-outline': 'node_modules/ionicons/dist/svg/ellipsis-vertical-outline.svg',
  145. 'magnet-outline': 'node_modules/ionicons/dist/svg/magnet-outline.svg',
  146. 'globe-outline': 'node_modules/ionicons/dist/svg/globe-outline.svg',
  147. 'search-outline': 'node_modules/ionicons/dist/svg/search-outline.svg',
  148. 'image-outline': 'node_modules/ionicons/dist/svg/image-outline.svg',
  149. 'play-outline': 'node_modules/ionicons/dist/svg/play-outline.svg',
  150. 'newspaper-outline': 'node_modules/ionicons/dist/svg/newspaper-outline.svg',
  151. 'location-outline': 'node_modules/ionicons/dist/svg/location-outline.svg',
  152. 'musical-notes-outline': 'node_modules/ionicons/dist/svg/musical-notes-outline.svg',
  153. 'layers-outline': 'node_modules/ionicons/dist/svg/layers-outline.svg',
  154. 'school-outline': 'node_modules/ionicons/dist/svg/school-outline.svg',
  155. 'file-tray-full-outline': 'node_modules/ionicons/dist/svg/file-tray-full-outline.svg',
  156. 'people-outline': 'node_modules/ionicons/dist/svg/people-outline.svg',
  157. },
  158. dest: '../../../templates/simple/icons.html',
  159. },
  160. },
  161. });
  162. grunt.registerMultiTask('svg2jinja', 'Create Jinja2 macro', function() {
  163. const ejs = require('ejs'), svgo = require('svgo');
  164. const icons = {}
  165. for(const iconName in this.data.src) {
  166. const svgFileName = this.data.src[iconName];
  167. try {
  168. const svgContent = grunt.file.read(svgFileName, { encoding: 'utf8' })
  169. const svgoResult = svgo.optimize(svgContent, {
  170. path: svgFileName,
  171. multipass: true,
  172. plugins: [
  173. {
  174. name: "removeTitle",
  175. },
  176. {
  177. name: "removeXMLNS",
  178. },
  179. {
  180. name: "addAttributesToSVGElement",
  181. params: {
  182. attributes: [
  183. { "aria-hidden": "true" }
  184. ]
  185. }
  186. }
  187. ]
  188. });
  189. icons[iconName] = svgoResult.data.replace("'", "\\'");
  190. } catch (err) {
  191. grunt.log.error(err);
  192. }
  193. }
  194. const template = `{# this file was generated by searx/static/themes/simple/gruntfile.js #}
  195. {%- set icons = {
  196. <% for (const iconName in icons) { %> '<%- iconName %>':'<%- icons[iconName] %>',
  197. <% } %>
  198. }
  199. -%}
  200. {% macro icon(action, alt) -%}
  201. {{ icons[action] | replace("ionicon", "ion-icon") | safe }}
  202. {%- endmacro %}
  203. {% macro icon_small(action) -%}
  204. {{ icons[action] | replace("ionicon", "ion-icon-small") | safe }}
  205. {%- endmacro %}
  206. {% macro icon_big(action, alt) -%}
  207. {{ icons[action] | replace("ionicon", "ion-icon-big") | safe }}
  208. {%- endmacro %}
  209. `;
  210. const result = ejs.render(template, { icons });
  211. grunt.file.write(this.data.dest, result, { encoding: 'utf8' });
  212. grunt.log.ok(this.data.dest + " created");
  213. });
  214. grunt.loadNpmTasks('grunt-contrib-watch');
  215. grunt.loadNpmTasks('grunt-contrib-copy');
  216. grunt.loadNpmTasks('grunt-contrib-uglify');
  217. grunt.loadNpmTasks('grunt-image');
  218. grunt.loadNpmTasks('grunt-contrib-jshint');
  219. grunt.loadNpmTasks('grunt-contrib-concat');
  220. grunt.loadNpmTasks('grunt-contrib-less');
  221. grunt.loadNpmTasks('grunt-contrib-cssmin');
  222. grunt.loadNpmTasks('grunt-stylelint');
  223. grunt.loadNpmTasks('grunt-eslint');
  224. grunt.registerTask('test', ['jshint']);
  225. grunt.registerTask('default', [
  226. 'eslint',
  227. 'stylelint',
  228. 'copy',
  229. 'concat',
  230. 'svg2jinja',
  231. 'uglify',
  232. 'image',
  233. 'less:development',
  234. 'less:production'
  235. ]);
  236. };