dev.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 w/ better tree-shaking.
  5. import esbuild from 'esbuild'
  6. import { resolve, relative, dirname } 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._ || ['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. let external = []
  39. if (!inlineDeps) {
  40. // cjs & esm-bundler: external all deps
  41. if (format === 'cjs' || format.includes('esm-bundler')) {
  42. external = [
  43. ...external,
  44. ...Object.keys(pkg.dependencies || {}),
  45. ...Object.keys(pkg.peerDependencies || {}),
  46. // for @vue/compiler-sfc / server-renderer
  47. 'path',
  48. 'url',
  49. 'stream'
  50. ]
  51. }
  52. if (target === 'compiler-sfc') {
  53. const consolidatePkgPath = require.resolve(
  54. '@vue/consolidate/package.json',
  55. {
  56. paths: [resolve(__dirname, `../packages/${target}/`)]
  57. }
  58. )
  59. const consolidateDeps = Object.keys(
  60. require(consolidatePkgPath).devDependencies
  61. )
  62. external = [
  63. ...external,
  64. ...consolidateDeps,
  65. 'fs',
  66. 'vm',
  67. 'crypto',
  68. 'react-dom/server',
  69. 'teacup/lib/express',
  70. 'arc-templates/dist/es5',
  71. 'then-pug',
  72. 'then-jade'
  73. ]
  74. }
  75. }
  76. const plugins = [
  77. {
  78. name: 'log-rebuild',
  79. setup(build) {
  80. build.onEnd(() => {
  81. console.log(`built: ${relativeOutfile}`)
  82. })
  83. }
  84. }
  85. ]
  86. if (format !== 'cjs' && pkg.buildOptions?.enableNonBrowserBranches) {
  87. plugins.push(polyfillNode())
  88. }
  89. esbuild
  90. .context({
  91. entryPoints: [resolve(__dirname, `../packages/${target}/src/index.ts`)],
  92. outfile,
  93. bundle: true,
  94. external,
  95. sourcemap: true,
  96. format: outputFormat,
  97. globalName: pkg.buildOptions?.name,
  98. platform: format === 'cjs' ? 'node' : 'browser',
  99. plugins,
  100. define: {
  101. __COMMIT__: `"dev"`,
  102. __VERSION__: `"${pkg.version}"`,
  103. __DEV__: prod ? `false` : `true`,
  104. __TEST__: `false`,
  105. __BROWSER__: String(
  106. format !== 'cjs' && !pkg.buildOptions?.enableNonBrowserBranches
  107. ),
  108. __GLOBAL__: String(format === 'global'),
  109. __ESM_BUNDLER__: String(format.includes('esm-bundler')),
  110. __ESM_BROWSER__: String(format.includes('esm-browser')),
  111. __NODE_JS__: String(format === 'cjs'),
  112. __SSR__: String(format === 'cjs' || format.includes('esm-bundler')),
  113. __COMPAT__: String(target === 'vue-compat'),
  114. __FEATURE_SUSPENSE__: `true`,
  115. __FEATURE_OPTIONS_API__: `true`,
  116. __FEATURE_PROD_DEVTOOLS__: `false`
  117. }
  118. })
  119. .then(ctx => ctx.watch())
  120. }