build.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const path = require('path');
  2. const fs = require('fs-extra');
  3. const rollup = require('rollup');
  4. const zlib = require('zlib');
  5. const config = require('./config');
  6. function build(config) {
  7. let output = config.output;
  8. let { file } = output;
  9. return rollup
  10. .rollup(config)
  11. .then(bundle => bundle.generate(output))
  12. .then(rst => {
  13. write(file, rst.code);
  14. })
  15. .catch(e => {
  16. throw e;
  17. });
  18. }
  19. function write(dest, code, zip) {
  20. return new Promise((resolve, reject) => {
  21. function report(extra) {
  22. // eslint-disable-next-line
  23. console.log(blue(path.relative(process.cwd(), dest)) + ' ' + getSize(code) + (extra || ''));
  24. resolve();
  25. }
  26. fs.outputFile(dest, code)
  27. .then(() => {
  28. if (zip) {
  29. zlib.gzip(code, (err, zipped) => {
  30. if (err) return reject(err);
  31. report(' (gzipped: ' + getSize(zipped) + ')');
  32. });
  33. } else {
  34. report();
  35. }
  36. })
  37. .catch(reject);
  38. });
  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. let configs = config.getAllBuilds();
  47. configs.forEach(config => {
  48. build(config);
  49. });