dev.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 { dirname, relative, resolve } from 'node:path'
  7. import { fileURLToPath } from 'node:url'
  8. import { createRequire } from 'node:module'
  9. import minimist from 'minimist'
  10. import { polyfillNode } from 'esbuild-plugin-polyfill-node'
  11. const require = createRequire(import.meta.url)
  12. const __dirname = dirname(fileURLToPath(import.meta.url))
  13. const args = minimist(process.argv.slice(2))
  14. const targets = args._.length ? args._ : ['vue']
  15. const format = args.f || 'global'
  16. const prod = args.p || false
  17. const inlineDeps = args.i || args.inline
  18. // resolve output
  19. const outputFormat = format.startsWith('global')
  20. ? 'iife'
  21. : format === 'cjs'
  22. ? 'cjs'
  23. : 'esm'
  24. const postfix = format.endsWith('-runtime')
  25. ? `runtime.${format.replace(/-runtime$/, '')}`
  26. : format
  27. for (const target of targets) {
  28. const pkg = require(`../packages/${target}/package.json`)
  29. const outfile = resolve(
  30. __dirname,
  31. `../packages/${target}/dist/${
  32. target === 'vue-compat' ? `vue` : target
  33. }.${postfix}.${prod ? `prod.` : ``}js`,
  34. )
  35. const relativeOutfile = relative(process.cwd(), outfile)
  36. // resolve externals
  37. // TODO this logic is largely duplicated from rollup.config.js
  38. /** @type {string[]} */
  39. let external = []
  40. if (!inlineDeps) {
  41. // cjs & esm-bundler: external all deps
  42. if (format === 'cjs' || format.includes('esm-bundler')) {
  43. external = [
  44. ...external,
  45. ...Object.keys(pkg.dependencies || {}),
  46. ...Object.keys(pkg.peerDependencies || {}),
  47. // for @vue/compiler-sfc / server-renderer
  48. 'path',
  49. 'url',
  50. 'stream',
  51. ]
  52. }
  53. if (target === 'compiler-sfc') {
  54. const consolidatePkgPath = require.resolve(
  55. '@vue/consolidate/package.json',
  56. {
  57. paths: [resolve(__dirname, `../packages/${target}/`)],
  58. },
  59. )
  60. const consolidateDeps = Object.keys(
  61. require(consolidatePkgPath).devDependencies,
  62. )
  63. external = [
  64. ...external,
  65. ...consolidateDeps,
  66. 'fs',
  67. 'vm',
  68. 'crypto',
  69. 'react-dom/server',
  70. 'teacup/lib/express',
  71. 'arc-templates/dist/es5',
  72. 'then-pug',
  73. 'then-jade',
  74. ]
  75. }
  76. }
  77. /** @type {Array<import('esbuild').Plugin>} */
  78. const plugins = [
  79. {
  80. name: 'log-rebuild',
  81. setup(build) {
  82. build.onEnd(() => {
  83. console.log(`built: ${relativeOutfile}`)
  84. })
  85. },
  86. },
  87. ]
  88. if (format !== 'cjs' && pkg.buildOptions?.enableNonBrowserBranches) {
  89. plugins.push(polyfillNode())
  90. }
  91. esbuild
  92. .context({
  93. entryPoints: [resolve(__dirname, `../packages/${target}/src/index.ts`)],
  94. outfile,
  95. bundle: true,
  96. external,
  97. sourcemap: true,
  98. format: outputFormat,
  99. globalName: pkg.buildOptions?.name,
  100. platform: format === 'cjs' ? 'node' : 'browser',
  101. plugins,
  102. define: {
  103. __COMMIT__: `"dev"`,
  104. __VERSION__: `"${pkg.version}"`,
  105. __DEV__: prod ? `false` : `true`,
  106. __TEST__: `false`,
  107. __BROWSER__: String(
  108. format !== 'cjs' && !pkg.buildOptions?.enableNonBrowserBranches,
  109. ),
  110. __GLOBAL__: String(format === 'global'),
  111. __ESM_BUNDLER__: String(format.includes('esm-bundler')),
  112. __ESM_BROWSER__: String(format.includes('esm-browser')),
  113. __CJS__: String(format === 'cjs'),
  114. __SSR__: String(format === 'cjs' || format.includes('esm-bundler')),
  115. __COMPAT__: String(target === 'vue-compat'),
  116. __FEATURE_SUSPENSE__: `true`,
  117. __FEATURE_OPTIONS_API__: `true`,
  118. __FEATURE_PROD_DEVTOOLS__: `false`,
  119. __FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__: `false`,
  120. },
  121. })
  122. .then(ctx => ctx.watch())
  123. }