build.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. ;(async () => {
  24. if (!target) {
  25. await buildAll(targets)
  26. checkAllSizes(targets)
  27. } else {
  28. await buildAll(fuzzyMatchTarget(target))
  29. checkAllSizes(fuzzyMatchTarget(target))
  30. }
  31. })()
  32. async function buildAll(targets) {
  33. for (const target of targets) {
  34. await build(target)
  35. }
  36. }
  37. async function build(target) {
  38. const pkgDir = path.resolve(`packages/${target}`)
  39. await fs.remove(`${pkgDir}/dist`)
  40. await execa(
  41. 'rollup',
  42. [
  43. '-c',
  44. '--environment',
  45. `NODE_ENV:production,` +
  46. `TARGET:${target}` +
  47. (formats ? `,FORMATS:${formats}` : ``)
  48. ],
  49. { stdio: 'inherit' }
  50. )
  51. const dtsOptions = {
  52. name: target === 'vue' ? target : `@vue/${target}`,
  53. main: `${pkgDir}/dist/packages/${target}/src/index.d.ts`,
  54. out: `${pkgDir}/dist/index.d.ts`
  55. }
  56. dts.bundle(dtsOptions)
  57. console.log()
  58. console.log(chalk.blue(chalk.bold(`generated typings at ${dtsOptions.out}`)))
  59. await fs.remove(`${pkgDir}/dist/packages`)
  60. }
  61. function checkAllSizes(targets) {
  62. console.log()
  63. for (const target of targets) {
  64. checkSize(target)
  65. }
  66. console.log()
  67. }
  68. function checkSize(target) {
  69. const pkgDir = path.resolve(`packages/${target}`)
  70. const esmProdBuild = `${pkgDir}/dist/${target}.esm-browser.prod.js`
  71. if (fs.existsSync(esmProdBuild)) {
  72. const file = fs.readFileSync(esmProdBuild)
  73. const minSize = (file.length / 1024).toFixed(2) + 'kb'
  74. const gzipped = zlib.gzipSync(file)
  75. const gzipSize = (gzipped.length / 1024).toFixed(2) + 'kb'
  76. console.log(
  77. `${chalk.gray(chalk.bold(target))} min:${minSize} / gzip:${gzipSize}`
  78. )
  79. }
  80. }