shared-build.js 1.4 KB

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