Gruntfile.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var fs = require("fs")
  2. var path = require("path")
  3. module.exports = function(grunt) {
  4. grunt.initConfig({
  5. pkg: grunt.file.readJSON('package.json'),
  6. cssnano: {
  7. options: {},
  8. dist: {
  9. files: {
  10. 'build/octicons.min.css': 'build/octicons.css'
  11. }
  12. }
  13. },
  14. svgmin: {
  15. dist: {
  16. options: {
  17. plugins: [
  18. {removeTitle: true},
  19. {removeStyleElement: true},
  20. {removeAttrs: { attrs: ['id', 'class', 'data-name', 'fill'] }},
  21. {removeEmptyContainers: true},
  22. {sortAttrs: true},
  23. {removeUselessDefs: true},
  24. {removeEmptyText: true},
  25. {removeEditorsNSData: true},
  26. {removeEmptyAttrs: true},
  27. {removeHiddenElems: true}
  28. ]
  29. },
  30. files: [{
  31. expand: true,
  32. cwd: 'lib/svg',
  33. src: ['*.svg'],
  34. dest: 'build/svg'
  35. }]
  36. }
  37. },
  38. svgstore: {
  39. options: {
  40. includeTitleElement: false,
  41. inheritviewbox: true,
  42. includedemo: function(arg) {
  43. var octicons = require("./index.js")
  44. var icons = function() {
  45. var result = []
  46. Object.keys(octicons).forEach(function(key){
  47. result.push("<div style=\"width: 10%;min-width: 100px;flex: 0 0 auto;box-sizing:border-box;padding:1em;text-align:center;\">" + octicons[key].toSVGUse({ height: 32 }) + "<div>" + key + "</div></div>")
  48. })
  49. return result.join("\n")
  50. }
  51. return `
  52. <!doctype html>
  53. <html>
  54. <head>
  55. <meta charset="utf-8">
  56. <title>Octicons Spritesheet test</title>
  57. <link rel="stylesheet" href="./octicons.css" media="screen" title="no title">
  58. <style>
  59. body {
  60. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  61. color: #222;
  62. font-size: 15px;
  63. }
  64. </style>
  65. </head>
  66. <body>
  67. ${arg.svg}
  68. <div style="font-size: 2.2em;padding-left: 20px;">Octicons SVG Spritesheet demo</div>
  69. <div style="font-size: 1.2em;margin: 1em 0;padding-left: 20px;">All the icons rendered below use the svg spriteheet located in the <code>/build/</code> directory.</div>
  70. <div style="flex: 0 1 auto;display:flex;flex-wrap: wrap; flex-direction: row;">
  71. ${icons()}
  72. </div>
  73. </body>
  74. </html>
  75. `
  76. }
  77. },
  78. default: {
  79. files: {
  80. "build/sprite.octicons.svg": ['build/svg/*.svg']
  81. }
  82. },
  83. },
  84. clean: {
  85. build: [
  86. 'build/*'
  87. ]
  88. },
  89. copy: {
  90. css: {
  91. src: "lib/octicons.css",
  92. dest: "build/octicons.css"
  93. }
  94. }
  95. });
  96. grunt.loadNpmTasks('grunt-contrib-clean');
  97. grunt.loadNpmTasks('grunt-contrib-copy');
  98. grunt.loadNpmTasks('grunt-svgstore');
  99. grunt.loadNpmTasks('grunt-svgmin');
  100. grunt.loadNpmTasks('grunt-cssnano');
  101. // build tasks
  102. grunt.registerTask('css', ['copy', 'cssnano']);
  103. grunt.registerTask('svg', ['clean', 'svgmin']);
  104. // default task, build /dist/
  105. grunt.registerTask('default', [ 'svg', 'css', 'json:svg', 'svgstore']);
  106. grunt.registerTask('json:svg', 'add svg string to data.json build', function() {
  107. var files = fs.readdirSync("./build/svg/")
  108. var data = JSON.parse(fs.readFileSync("./lib/data.json"))
  109. files.forEach(function(file) {
  110. var svg = fs.readFileSync(path.resolve("./build/svg", file))
  111. var key = path.basename(file, ".svg")
  112. if (data[key]) {
  113. var raw = svg.toString()
  114. data[key].path = /<path.+\/>/g.exec(raw)[0]
  115. data[key].height = /height="(\d+)"/g.exec(raw)[1]
  116. data[key].width = /width="(\d+)"/g.exec(raw)[1]
  117. }
  118. })
  119. fs.writeFileSync("build/data.json", JSON.stringify(data));
  120. })
  121. };