build.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * Build, update component.json, uglify, and report size.
  3. */
  4. module.exports = function (grunt) {
  5. grunt.registerTask('build', function () {
  6. var done = this.async()
  7. var fs = require('fs')
  8. var zlib = require('zlib')
  9. var build = require('../shared-build')
  10. var uglifyjs = require('uglify-js')
  11. // update component.json first
  12. var jsRE = /\.js$/
  13. var component = grunt.file.readJSON('component.json')
  14. component.scripts = []
  15. grunt.file.recurse('src', function (file) {
  16. if (jsRE.test(file)) {
  17. component.scripts.push(file)
  18. }
  19. })
  20. grunt.file.write('component.json', JSON.stringify(component, null, 2))
  21. // then build
  22. build(grunt, function (err) {
  23. if (err) return done(err)
  24. var js = fs.readFileSync('dist/vue.js', 'utf-8')
  25. report('dist/vue.js', js)
  26. // uglify
  27. var result = uglifyjs.minify(js, {
  28. fromString: true,
  29. output: {
  30. comments: /License/
  31. },
  32. compress: {
  33. pure_funcs: [
  34. 'require',
  35. '_.log',
  36. '_.warn',
  37. '_.assertAsset',
  38. 'enableDebug'
  39. ]
  40. }
  41. })
  42. // var min = grunt.config.get('banner') + result.code
  43. write('dist/vue.min.js', result.code)
  44. // report gzip size
  45. zlib.gzip(result.code, function (err, buf) {
  46. write('dist/vue.min.js.gz', buf)
  47. done(err)
  48. })
  49. })
  50. function write (path, file) {
  51. fs.writeFileSync(path, file)
  52. report(path, file)
  53. }
  54. function report (path, file) {
  55. console.log(
  56. blue(path + ': ') +
  57. (file.length / 1024).toFixed(2) + 'kb'
  58. )
  59. }
  60. function blue (str) {
  61. return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m'
  62. }
  63. })
  64. }