build.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const path = require('path')
  2. const rollup = require('rollup')
  3. const babel = require('rollup-plugin-babel')
  4. const replace = require('@rollup/plugin-replace')
  5. const { terser } = require('rollup-plugin-terser')
  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 (options, _outputOptions) {
  16. try {
  17. const bundle = await rollup.rollup(options)
  18. const outputOptions = {
  19. format: _outputOptions.format,
  20. exports: 'named',
  21. banner: banner,
  22. file: path.resolve(__dirname, _outputOptions.filename),
  23. name: 'VueLazyload'
  24. }
  25. const { output } = await bundle.generate(outputOptions)
  26. await bundle.write(outputOptions)
  27. const code = output[0].code
  28. console.log(blue(outputOptions.file) + ' ' + getSize(code))
  29. } catch (e) {
  30. console.error(e)
  31. }
  32. }
  33. function getSize (code) {
  34. return (Buffer.byteLength(code, 'utf8') / 1024).toFixed(2) + 'kb'
  35. }
  36. function blue (str) {
  37. return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m'
  38. }
  39. build({
  40. input: path.resolve(__dirname, 'src/index.js'),
  41. plugins: [
  42. resolve(),
  43. commonjs(),
  44. babel({ runtimeHelpers: true }),
  45. replace({
  46. '__VUE_LAZYLOAD_VERSION__': JSON.stringify(version)
  47. }),
  48. terser()
  49. ]
  50. }, {
  51. format: 'umd',
  52. filename: 'vue-lazyload.js'
  53. })
  54. build({
  55. input: path.resolve(__dirname, 'src/index.js'),
  56. plugins: [
  57. resolve(),
  58. commonjs(),
  59. replace({
  60. '__VUE_LAZYLOAD_VERSION__': JSON.stringify(version)
  61. }),
  62. babel({ runtimeHelpers: true })
  63. ]
  64. }, {
  65. format: 'esm',
  66. filename: 'vue-lazyload.esm.js'
  67. })