shared-build.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Shared build function
  3. */
  4. var resolve = require('component-resolver')
  5. var build = require('component-builder')
  6. module.exports = function (grunt, cb) {
  7. var license =
  8. '/**\n' +
  9. ' * Vue.js v' + grunt.config.get('version') + '\n' +
  10. ' * (c) ' + new Date().getFullYear() + ' Evan You\n' +
  11. ' * Released under the MIT License.\n' +
  12. ' */\n'
  13. // build with component-builder
  14. resolve(process.cwd(), {}, function (err, tree) {
  15. build.scripts(tree)
  16. .use('scripts', build.plugins.js())
  17. .end(function (err, js) {
  18. // wrap with umd
  19. js = umd(js)
  20. // replace require paths with numbers for file size
  21. js = shortenPaths(js)
  22. // add license
  23. js = license + js
  24. // done
  25. cb(js)
  26. })
  27. })
  28. }
  29. /**
  30. * component's umd wrapper throws error in strict mode
  31. * so we have to roll our own
  32. */
  33. function umd (js) {
  34. return '\n;(function(){\n\n'
  35. + '"use strict"\n\n'
  36. + build.scripts.require
  37. + js
  38. + 'if (typeof exports == "object") {\n'
  39. + ' module.exports = require("vue");\n'
  40. + '} else if (typeof define == "function" && define.amd) {\n'
  41. +' define([], function(){ return require("vue"); });\n'
  42. + '} else {\n'
  43. + ' window.Vue = require("vue");\n'
  44. + '}\n'
  45. + '})()\n';
  46. }
  47. /**
  48. * Shorten require() paths for smaller file size
  49. */
  50. function shortenPaths (js) {
  51. var seen = {}
  52. var count = 0
  53. return js.replace(/'vue\/src\/(.+?)'|"vue\/src\/(.+?)"/g, function (path) {
  54. path = path.slice(1, -1)
  55. if (!seen[path]) {
  56. seen[path] = ++count
  57. }
  58. return seen[path]
  59. })
  60. }