dev.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // @ts-check
  2. import fs from 'node:fs'
  3. import { dirname, relative, resolve } from 'node:path'
  4. import { fileURLToPath } from 'node:url'
  5. import { createRequire } from 'node:module'
  6. import { parseArgs } from 'node:util'
  7. import { watch } from 'rolldown'
  8. import polyfillNode from '@rolldown/plugin-node-polyfills'
  9. const require = createRequire(import.meta.url)
  10. const __dirname = dirname(fileURLToPath(import.meta.url))
  11. const fsShimPath = resolve(__dirname, 'shim-fs.js')
  12. const {
  13. values: { format: rawFormat, prod, inline: inlineDeps },
  14. positionals,
  15. } = parseArgs({
  16. allowPositionals: true,
  17. options: {
  18. format: {
  19. type: 'string',
  20. short: 'f',
  21. default: 'global',
  22. },
  23. prod: {
  24. type: 'boolean',
  25. short: 'p',
  26. default: false,
  27. },
  28. inline: {
  29. type: 'boolean',
  30. short: 'i',
  31. default: false,
  32. },
  33. },
  34. })
  35. const format = rawFormat || 'global'
  36. const targets = positionals.length ? positionals : ['vue']
  37. const outputFormat = format.startsWith('global')
  38. ? 'iife'
  39. : format === 'cjs'
  40. ? 'cjs'
  41. : 'es'
  42. const postfix = format.endsWith('-runtime')
  43. ? `runtime.${format.replace(/-runtime$/, '')}`
  44. : format
  45. const privatePackages = fs.readdirSync('packages-private')
  46. for (const target of targets) {
  47. const pkgBase = privatePackages.includes(target)
  48. ? `packages-private`
  49. : `packages`
  50. const pkgBasePath = `../${pkgBase}/${target}`
  51. const pkg = require(`${pkgBasePath}/package.json`)
  52. const outfile = resolve(
  53. __dirname,
  54. `${pkgBasePath}/dist/${
  55. target === 'vue-compat' ? `vue` : target
  56. }.${postfix}.${prod ? `prod.` : ``}js`,
  57. )
  58. const relativeOutfile = relative(process.cwd(), outfile)
  59. /** @type {string[]} */
  60. let external = []
  61. if (!inlineDeps) {
  62. if (format === 'cjs' || format.includes('esm-bundler')) {
  63. external = [
  64. ...external,
  65. ...Object.keys(pkg.dependencies || {}),
  66. ...Object.keys(pkg.peerDependencies || {}),
  67. 'path',
  68. 'url',
  69. 'stream',
  70. ]
  71. }
  72. if (target === 'compiler-sfc') {
  73. const consolidatePkgPath = require.resolve(
  74. '@vue/consolidate/package.json',
  75. {
  76. paths: [resolve(__dirname, `../packages/${target}/`)],
  77. },
  78. )
  79. const consolidateDeps = Object.keys(
  80. require(consolidatePkgPath).devDependencies,
  81. )
  82. external = [
  83. ...external,
  84. ...consolidateDeps,
  85. 'react-dom/server',
  86. 'teacup/lib/express',
  87. 'arc-templates/dist/es5',
  88. 'then-pug',
  89. 'then-jade',
  90. ]
  91. if (format === 'cjs' || format.includes('esm-bundler')) {
  92. external.push('fs', 'vm', 'crypto')
  93. }
  94. }
  95. }
  96. /** @type {import('rolldown').Plugin[]} */
  97. const plugins = []
  98. if (format !== 'cjs' && pkg.buildOptions?.enableNonBrowserBranches) {
  99. plugins.push(polyfillNode())
  100. }
  101. const platform = format === 'cjs' ? 'node' : 'browser'
  102. const resolveOptions =
  103. format === 'cjs'
  104. ? undefined
  105. : {
  106. alias: {
  107. // Alias built-in fs to a shim for browser builds.
  108. fs: fsShimPath,
  109. },
  110. }
  111. /** @type {import('rolldown').WatchOptions} */
  112. const config = {
  113. input: resolve(__dirname, `${pkgBasePath}/src/index.ts`),
  114. output: {
  115. file: outfile,
  116. format: outputFormat,
  117. sourcemap: true,
  118. name: format.startsWith('global') ? pkg.buildOptions?.name : undefined,
  119. },
  120. external,
  121. platform,
  122. resolve: resolveOptions,
  123. treeshake: {
  124. moduleSideEffects: false,
  125. },
  126. plugins,
  127. transform: {
  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. }
  149. console.log(`watching: ${relativeOutfile}`)
  150. watch(config).on('event', event => {
  151. if (event.code === 'BUNDLE_END') {
  152. // @ts-expect-error
  153. console.log(`built ${config.output.file} in ${event.duration}ms`)
  154. }
  155. })
  156. }