gruntfile.js 7.2 KB

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