dev-esbuild.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // @ts-check
  2. // Using esbuild for faster dev builds.
  3. // We are still using Rollup for production builds because it generates
  4. // smaller files and provides better tree-shaking.
  5. import esbuild from 'esbuild'
  6. import fs from 'node:fs'
  7. import { dirname, relative, resolve } from 'node:path'
  8. import { fileURLToPath } from 'node:url'
  9. import { createRequire } from 'node:module'
  10. import { parseArgs } from 'node:util'
  11. import { polyfillNode } from 'esbuild-plugin-polyfill-node'
  12. const require = createRequire(import.meta.url)
  13. const __dirname = dirname(fileURLToPath(import.meta.url))
  14. const {
  15. values: { format: rawFormat, prod, inline: inlineDeps },
  16. positionals,
  17. } = parseArgs({
  18. allowPositionals: true,
  19. options: {
  20. format: {
  21. type: 'string',
  22. short: 'f',
  23. default: 'global',
  24. },
  25. prod: {
  26. type: 'boolean',
  27. short: 'p',
  28. default: false,
  29. },
  30. inline: {
  31. type: 'boolean',
  32. short: 'i',
  33. default: false,
  34. },
  35. },
  36. })
  37. const format = rawFormat || 'global'
  38. const targets = positionals.length ? positionals : ['vue']
  39. // resolve output
  40. const outputFormat = format.startsWith('global')
  41. ? 'iife'
  42. : format === 'cjs'
  43. ? 'cjs'
  44. : 'esm'
  45. const postfix = format.endsWith('-runtime')
  46. ? `runtime.${format.replace(/-runtime$/, '')}`
  47. : format
  48. const privatePackages = fs.readdirSync('packages-private')
  49. for (const target of targets) {
  50. const pkgBase = privatePackages.includes(target)
  51. ? `packages-private`
  52. : `packages`
  53. const pkgBasePath = `../${pkgBase}/${target}`
  54. const pkg = require(`${pkgBasePath}/package.json`)
  55. const outfile = resolve(
  56. __dirname,
  57. `${pkgBasePath}/dist/${
  58. target === 'vue-compat' ? `vue` : target
  59. }.${postfix}.${prod ? `prod.` : ``}js`,
  60. )
  61. const relativeOutfile = relative(process.cwd(), outfile)
  62. // resolve externals
  63. // TODO this logic is largely duplicated from rollup.config.js
  64. /** @type {string[]} */
  65. let external = []
  66. if (!inlineDeps) {
  67. // cjs & esm-bundler: external all deps
  68. if (format === 'cjs' || format.includes('esm-bundler')) {
  69. external = [
  70. ...external,
  71. ...Object.keys(pkg.dependencies || {}),
  72. ...Object.keys(pkg.peerDependencies || {}),
  73. // for @vue/compiler-sfc / server-renderer
  74. 'path',
  75. 'url',
  76. 'stream',
  77. ]
  78. }
  79. if (target === 'compiler-sfc') {
  80. const consolidatePkgPath = require.resolve(
  81. '@vue/consolidate/package.json',
  82. {
  83. paths: [resolve(__dirname, `../packages/${target}/`)],
  84. },
  85. )
  86. const consolidateDeps = Object.keys(
  87. require(consolidatePkgPath).devDependencies,
  88. )
  89. external = [
  90. ...external,
  91. ...consolidateDeps,
  92. 'fs',
  93. 'vm',
  94. 'crypto',
  95. 'react-dom/server',
  96. 'teacup/lib/express',
  97. 'arc-templates/dist/es5',
  98. 'then-pug',
  99. 'then-jade',
  100. ]
  101. }
  102. }
  103. /** @type {Array<import('esbuild').Plugin>} */
  104. const plugins = [
  105. {
  106. name: 'log-rebuild',
  107. setup(build) {
  108. build.onEnd(() => {
  109. console.log(`built: ${relativeOutfile}`)
  110. })
  111. },
  112. },
  113. ]
  114. if (format !== 'cjs' && pkg.buildOptions?.enableNonBrowserBranches) {
  115. plugins.push(polyfillNode())
  116. }
  117. esbuild
  118. .context({
  119. entryPoints: [resolve(__dirname, `${pkgBasePath}/src/index.ts`)],
  120. outfile,
  121. bundle: true,
  122. external,
  123. sourcemap: true,
  124. format: outputFormat,
  125. globalName: pkg.buildOptions?.name,
  126. platform: format === 'cjs' ? 'node' : 'browser',
  127. plugins,
  128. define: {
  129. __COMMIT__: `"dev"`,
  130. __VERSION__: `"${pkg.version}"`,
  131. __DEV__: prod ? `false` : `true`,
  132. __TEST__: `false`,
  133. __BROWSER__: String(
  134. format !== 'cjs' && !pkg.buildOptions?.enableNonBrowserBranches,
  135. ),
  136. __GLOBAL__: String(format === 'global'),
  137. __ESM_BUNDLER__: String(format.includes('esm-bundler')),
  138. __ESM_BROWSER__: String(format.includes('esm-browser')),
  139. __CJS__: String(format === 'cjs'),
  140. __SSR__: String(format !== 'global'),
  141. __COMPAT__: String(target === 'vue-compat'),
  142. __FEATURE_SUSPENSE__: `true`,
  143. __FEATURE_OPTIONS_API__: `true`,
  144. __FEATURE_PROD_DEVTOOLS__: `false`,
  145. __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__: `true`,
  146. },
  147. })
  148. .then(ctx => ctx.watch())
  149. }