dev.js 3.4 KB

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