build.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. const pkg = require(`${pkgDir}/package.json`)
  40. await fs.remove(`${pkgDir}/dist`)
  41. await execa(
  42. 'rollup',
  43. [
  44. '-c',
  45. '--environment',
  46. `NODE_ENV:production,` +
  47. `TARGET:${target}` +
  48. (formats ? `,FORMATS:${formats}` : ``)
  49. ],
  50. { stdio: 'inherit' }
  51. )
  52. if (pkg.types) {
  53. const dtsOptions = {
  54. name: target === 'vue' ? target : `@vue/${target}`,
  55. main: `${pkgDir}/dist/packages/${target}/src/index.d.ts`,
  56. out: `${pkgDir}/${pkg.types}`
  57. }
  58. dts.bundle(dtsOptions)
  59. console.log()
  60. console.log(
  61. chalk.blue(chalk.bold(`generated typings at ${dtsOptions.out}`))
  62. )
  63. await fs.remove(`${pkgDir}/dist/packages`)
  64. }
  65. }
  66. function checkAllSizes(targets) {
  67. console.log()
  68. for (const target of targets) {
  69. checkSize(target)
  70. }
  71. console.log()
  72. }
  73. function checkSize(target) {
  74. const pkgDir = path.resolve(`packages/${target}`)
  75. const esmProdBuild = `${pkgDir}/dist/${target}.esm-browser.prod.js`
  76. if (fs.existsSync(esmProdBuild)) {
  77. const file = fs.readFileSync(esmProdBuild)
  78. const minSize = (file.length / 1024).toFixed(2) + 'kb'
  79. const gzipped = zlib.gzipSync(file)
  80. const gzipSize = (gzipped.length / 1024).toFixed(2) + 'kb'
  81. console.log(
  82. `${chalk.gray(chalk.bold(target))} min:${minSize} / gzip:${gzipSize}`
  83. )
  84. }
  85. }