dev.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Using esbuild for faster dev builds.
  2. // We are still using Rollup for production builds because it generates
  3. // smaller files w/ better tree-shaking.
  4. // @ts-check
  5. const { build } = require('esbuild')
  6. const nodePolyfills = require('@esbuild-plugins/node-modules-polyfill')
  7. const { resolve, relative } = require('path')
  8. const args = require('minimist')(process.argv.slice(2))
  9. const target = args._[0] || 'vue'
  10. const format = args.f || 'global'
  11. const inlineDeps = args.i || args.inline
  12. const pkg = require(resolve(__dirname, `../packages/${target}/package.json`))
  13. // resolve output
  14. const outputFormat = format.startsWith('global')
  15. ? 'iife'
  16. : format === 'cjs'
  17. ? 'cjs'
  18. : 'esm'
  19. const postfix = format.endsWith('-runtime')
  20. ? `runtime.${format.replace(/-runtime$/, '')}`
  21. : format
  22. const outfile = resolve(
  23. __dirname,
  24. `../packages/${target}/dist/${target}.${postfix}.js`
  25. )
  26. const relativeOutfile = relative(process.cwd(), outfile)
  27. // resolve externals
  28. // TODO this logic is largely duplicated from rollup.config.js
  29. let external = []
  30. if (!inlineDeps) {
  31. // cjs & esm-bundler: external all deps
  32. if (format === 'cjs' || format.includes('esm-bundler')) {
  33. external = [
  34. ...external,
  35. ...Object.keys(pkg.dependencies || {}),
  36. ...Object.keys(pkg.peerDependencies || {}),
  37. // for @vue/compiler-sfc / server-renderer
  38. 'path',
  39. 'url',
  40. 'stream'
  41. ]
  42. }
  43. if (target === 'compiler-sfc') {
  44. const consolidateDeps = require.resolve('@vue/consolidate/package.json', {
  45. paths: [resolve(__dirname, `../packages/${target}/`)]
  46. })
  47. external = [
  48. ...external,
  49. ...Object.keys(require(consolidateDeps).devDependencies),
  50. 'fs',
  51. 'vm',
  52. 'crypto',
  53. 'react-dom/server',
  54. 'teacup/lib/express',
  55. 'arc-templates/dist/es5',
  56. 'then-pug',
  57. 'then-jade'
  58. ]
  59. }
  60. }
  61. build({
  62. entryPoints: [resolve(__dirname, `../packages/${target}/src/index.ts`)],
  63. outfile,
  64. bundle: true,
  65. external,
  66. sourcemap: true,
  67. format: outputFormat,
  68. globalName: pkg.buildOptions?.name,
  69. platform: format === 'cjs' ? 'node' : 'browser',
  70. plugins:
  71. format === 'cjs' || pkg.buildOptions?.enableNonBrowserBranches
  72. ? [nodePolyfills.default()]
  73. : undefined,
  74. define: {
  75. __COMMIT__: `"dev"`,
  76. __VERSION__: `"${pkg.version}"`,
  77. __DEV__: `true`,
  78. __TEST__: `false`,
  79. __BROWSER__: String(
  80. format !== 'cjs' && !pkg.buildOptions?.enableNonBrowserBranches
  81. ),
  82. __GLOBAL__: String(format === 'global'),
  83. __ESM_BUNDLER__: String(format.includes('esm-bundler')),
  84. __ESM_BROWSER__: String(format.includes('esm-browser')),
  85. __NODE_JS__: String(format === 'cjs'),
  86. __SSR__: String(format === 'cjs' || format.includes('esm-bundler')),
  87. __COMPAT__: `false`,
  88. __FEATURE_SUSPENSE__: `true`,
  89. __FEATURE_OPTIONS_API__: `true`,
  90. __FEATURE_PROD_DEVTOOLS__: `false`
  91. },
  92. watch: {
  93. onRebuild(error) {
  94. if (!error) console.log(`rebuilt: ${relativeOutfile}`)
  95. }
  96. }
  97. }).then(() => {
  98. console.log(`watching: ${relativeOutfile}`)
  99. })