build.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const fs = require('fs')
  2. const path = require('path')
  3. const rollup = require('rollup')
  4. const babel = require('rollup-plugin-babel')
  5. const uglify = require('rollup-plugin-uglify')
  6. const resolve = require('rollup-plugin-node-resolve')
  7. const commonjs = require('rollup-plugin-commonjs')
  8. const version = process.env.VERSION || require('./package.json').version
  9. const banner =
  10. '/*!\n' +
  11. ' * Vue-Lazyload.js v' + version + '\n' +
  12. ' * (c) ' + new Date().getFullYear() + ' Awe <hilongjw@gmail.com>\n' +
  13. ' * Released under the MIT License.\n' +
  14. ' */\n'
  15. async function build () {
  16. try {
  17. const bundle = await rollup.rollup({
  18. input: path.resolve(__dirname, 'src/index.js'),
  19. plugins: [
  20. resolve(),
  21. commonjs(),
  22. babel({ runtimeHelpers: true }),
  23. uglify()
  24. ]
  25. })
  26. let { code } = await bundle.generate({
  27. format: 'umd',
  28. name: 'VueLazyload'
  29. })
  30. code = rewriteVersion(code)
  31. await write(path.resolve(__dirname, 'vue-lazyload.js'), code)
  32. console.log('Vue-Lazyload.js v' + version + ' builded')
  33. } catch (e) {
  34. console.log(e)
  35. }
  36. }
  37. function rewriteVersion (code) {
  38. return code.replace('__VUE_LAZYLOAD_VERSION__', version)
  39. }
  40. function getSize (code) {
  41. return (code.length / 1024).toFixed(2) + 'kb'
  42. }
  43. function blue (str) {
  44. return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m'
  45. }
  46. function write (dest, code) {
  47. return new Promise(function (resolve, reject) {
  48. code = banner + code
  49. fs.writeFile(dest, code, function (err) {
  50. if (err) return reject(err)
  51. console.log(blue(dest) + ' ' + getSize(code))
  52. resolve()
  53. })
  54. })
  55. }
  56. build()