build.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. var fs = require('fs')
  2. var zlib = require('zlib')
  3. var rollup = require('rollup')
  4. var uglify = require('uglify-js')
  5. var babel = require('rollup-plugin-babel')
  6. var replace = require('rollup-plugin-replace')
  7. var aliasPlugin = require('rollup-plugin-alias')
  8. var baseAlias = require('./alias')
  9. var version = process.env.VERSION || require('../package.json').version
  10. if (!fs.existsSync('dist')) {
  11. fs.mkdirSync('dist')
  12. }
  13. var banner =
  14. '/*!\n' +
  15. ' * Vue.js v' + version + '\n' +
  16. ' * (c) 2014-' + new Date().getFullYear() + ' Evan You\n' +
  17. ' * Released under the MIT License.\n' +
  18. ' */'
  19. // Update main file
  20. var main = fs
  21. .readFileSync('src/core/index.js', 'utf-8')
  22. .replace(/Vue\.version = '[\d\.]+'/, "Vue.version = '" + version + "'")
  23. fs.writeFileSync('src/core/index.js', main)
  24. var builds = [
  25. // Runtime only (CommonJS). Used by bundlers e.g. Webpack & Browserify
  26. {
  27. entry: 'src/entries/web-runtime.js',
  28. format: 'cjs',
  29. out: 'dist/vue.common.js'
  30. },
  31. // Minified runtime, only for filze size monitoring
  32. {
  33. entry: 'src/entries/web-runtime.js',
  34. format: 'umd',
  35. env: 'production',
  36. out: 'dist/vue.common.min.js'
  37. },
  38. // Runtime+compiler standalone developement build.
  39. {
  40. entry: 'src/entries/web-runtime-with-compiler.js',
  41. format: 'umd',
  42. env: 'development',
  43. out: 'dist/vue.js',
  44. banner: true,
  45. alias: {
  46. entities: './entity-decoder'
  47. }
  48. },
  49. // Runtime+compiler standalone production build.
  50. {
  51. entry: 'src/entries/web-runtime-with-compiler.js',
  52. format: 'umd',
  53. env: 'production',
  54. out: 'dist/vue.min.js',
  55. banner: true,
  56. alias: {
  57. entities: './entity-decoder'
  58. }
  59. },
  60. // Web compiler (CommonJS).
  61. {
  62. entry: 'src/entries/web-compiler.js',
  63. format: 'cjs',
  64. external: ['entities'],
  65. out: 'dist/compiler.js'
  66. },
  67. // Web server renderer (CommonJS).
  68. {
  69. entry: 'src/entries/web-server-renderer.js',
  70. format: 'cjs',
  71. external: ['stream'],
  72. out: 'dist/server-renderer.js'
  73. }
  74. ]
  75. // filter builds via command line arg
  76. if (process.argv[2]) {
  77. var filters = process.argv[2].split(',')
  78. builds = builds.filter(b => {
  79. return filters.some(f => b.out.indexOf(f) > -1)
  80. })
  81. }
  82. build(builds)
  83. function build (builds) {
  84. var built = 0
  85. var total = builds.length
  86. next()
  87. function next () {
  88. buildEntry(builds[built]).then(function () {
  89. built++
  90. if (built < total) {
  91. next()
  92. }
  93. }).catch(logError)
  94. }
  95. }
  96. function buildEntry (opts) {
  97. var plugins = [babel()]
  98. if (opts.env) {
  99. plugins.push(replace({
  100. 'process.env.NODE_ENV': JSON.stringify(opts.env),
  101. 'process.env.VUE_ENV': JSON.stringify('client')
  102. }))
  103. }
  104. var alias = baseAlias
  105. if (opts.alias) {
  106. alias = Object.assign({}, baseAlias, opts.alias)
  107. }
  108. plugins.push(aliasPlugin(alias))
  109. return rollup.rollup({
  110. entry: opts.entry,
  111. plugins: plugins,
  112. external: opts.external
  113. }).then(function (bundle) {
  114. var code = bundle.generate({
  115. format: opts.format,
  116. moduleName: 'Vue',
  117. banner: opts.banner ? banner : null
  118. }).code
  119. if (opts.env === 'production') {
  120. var minified = (opts.banner ? banner + '\n' : '') + uglify.minify(code, {
  121. fromString: true,
  122. output: {
  123. screw_ie8: true,
  124. ascii_only: true
  125. },
  126. compress: {
  127. pure_funcs: ['makeMap']
  128. }
  129. }).code
  130. return write(opts.out, minified).then(zip(opts.out))
  131. } else {
  132. return write(opts.out, code)
  133. }
  134. })
  135. }
  136. function write (dest, code) {
  137. return new Promise(function (resolve, reject) {
  138. fs.writeFile(dest, code, function (err) {
  139. if (err) return reject(err)
  140. console.log(blue(dest) + ' ' + getSize(code))
  141. resolve()
  142. })
  143. })
  144. }
  145. function zip (file) {
  146. return function () {
  147. return new Promise(function (resolve, reject) {
  148. fs.readFile(file, function (err, buf) {
  149. if (err) return reject(err)
  150. zlib.gzip(buf, function (err, buf) {
  151. if (err) return reject(err)
  152. write(file + '.gz', buf).then(resolve)
  153. })
  154. })
  155. })
  156. }
  157. }
  158. function getSize (code) {
  159. return (code.length / 1024).toFixed(2) + 'kb'
  160. }
  161. function logError (e) {
  162. console.log(e)
  163. }
  164. function blue (str) {
  165. return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m'
  166. }