dev.js 3.4 KB

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