build.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Produce prodcution builds and stitch toegether d.ts files.
  3. To specific the package to build, simply pass its name and the desired build
  4. formats to output (defaults to `buildOptions.formats` specified in that package,
  5. or "esm,cjs"):
  6. ```
  7. # name supports fuzzy match. will build all packages with name containing "dom":
  8. yarn build dom
  9. # specify the format to output
  10. yarn build core --formats cjs
  11. ```
  12. */
  13. const fs = require('fs-extra')
  14. const path = require('path')
  15. const zlib = require('zlib')
  16. const chalk = require('chalk')
  17. const execa = require('execa')
  18. const dts = require('dts-bundle')
  19. const { targets, fuzzyMatchTarget } = require('./utils')
  20. const args = require('minimist')(process.argv.slice(2))
  21. const target = args._[0]
  22. const formats = args.formats || args.f
  23. const buildAllMatching = args.all || args.a
  24. ;(async () => {
  25. if (!target) {
  26. await buildAll(targets)
  27. checkAllSizes(targets)
  28. } else {
  29. await buildAll(fuzzyMatchTarget(target, buildAllMatching))
  30. checkAllSizes(fuzzyMatchTarget(target, buildAllMatching))
  31. }
  32. })()
  33. async function buildAll(targets) {
  34. for (const target of targets) {
  35. await build(target)
  36. }
  37. }
  38. async function build(target) {
  39. const pkgDir = path.resolve(`packages/${target}`)
  40. const pkg = require(`${pkgDir}/package.json`)
  41. await fs.remove(`${pkgDir}/dist`)
  42. await execa(
  43. 'rollup',
  44. [
  45. '-c',
  46. '--environment',
  47. `NODE_ENV:production,` +
  48. `TARGET:${target}` +
  49. (formats ? `,FORMATS:${formats}` : ``)
  50. ],
  51. { stdio: 'inherit' }
  52. )
  53. if (pkg.types) {
  54. const dtsOptions = {
  55. name: target === 'vue' ? target : `@vue/${target}`,
  56. main: `${pkgDir}/dist/packages/${target}/src/index.d.ts`,
  57. out: `${pkgDir}/${pkg.types}`
  58. }
  59. dts.bundle(dtsOptions)
  60. console.log()
  61. console.log(
  62. chalk.blue(chalk.bold(`generated typings at ${dtsOptions.out}`))
  63. )
  64. await fs.remove(`${pkgDir}/dist/packages`)
  65. }
  66. }
  67. function checkAllSizes(targets) {
  68. console.log()
  69. for (const target of targets) {
  70. checkSize(target)
  71. }
  72. console.log()
  73. }
  74. function checkSize(target) {
  75. const pkgDir = path.resolve(`packages/${target}`)
  76. const esmProdBuild = `${pkgDir}/dist/${target}.esm-browser.prod.js`
  77. if (fs.existsSync(esmProdBuild)) {
  78. const file = fs.readFileSync(esmProdBuild)
  79. const minSize = (file.length / 1024).toFixed(2) + 'kb'
  80. const gzipped = zlib.gzipSync(file)
  81. const gzipSize = (gzipped.length / 1024).toFixed(2) + 'kb'
  82. console.log(
  83. `${chalk.gray(chalk.bold(target))} min:${minSize} / gzip:${gzipSize}`
  84. )
  85. }
  86. }