rollup.config.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import fs from 'fs'
  2. import path from 'path'
  3. import ts from 'rollup-plugin-typescript2'
  4. import replace from '@rollup/plugin-replace'
  5. import json from '@rollup/plugin-json'
  6. if (!process.env.TARGET) {
  7. throw new Error('TARGET package must be specified via --environment flag.')
  8. }
  9. const masterVersion = require('./package.json').version
  10. const packagesDir = path.resolve(__dirname, 'packages')
  11. const packageDir = path.resolve(packagesDir, process.env.TARGET)
  12. const name = path.basename(packageDir)
  13. const resolve = p => path.resolve(packageDir, p)
  14. const pkg = require(resolve(`package.json`))
  15. const packageOptions = pkg.buildOptions || {}
  16. const knownExternals = fs.readdirSync(packagesDir).filter(p => {
  17. return p !== '@vue/shared'
  18. })
  19. // ensure TS checks only once for each build
  20. let hasTSChecked = false
  21. const outputConfigs = {
  22. 'esm-bundler': {
  23. file: resolve(`dist/${name}.esm-bundler.js`),
  24. format: `es`
  25. },
  26. // main "vue" package only
  27. 'esm-bundler-runtime': {
  28. file: resolve(`dist/${name}.runtime.esm-bundler.js`),
  29. format: `es`
  30. },
  31. cjs: {
  32. file: resolve(`dist/${name}.cjs.js`),
  33. format: `cjs`
  34. },
  35. global: {
  36. file: resolve(`dist/${name}.global.js`),
  37. format: `iife`
  38. },
  39. esm: {
  40. file: resolve(`dist/${name}.esm.js`),
  41. format: `es`
  42. }
  43. }
  44. const defaultFormats = ['esm-bundler', 'cjs']
  45. const inlineFormats = process.env.FORMATS && process.env.FORMATS.split(',')
  46. const packageFormats = inlineFormats || packageOptions.formats || defaultFormats
  47. const packageConfigs = process.env.PROD_ONLY
  48. ? []
  49. : packageFormats.map(format => createConfig(format, outputConfigs[format]))
  50. if (process.env.NODE_ENV === 'production') {
  51. packageFormats.forEach(format => {
  52. if (format === 'cjs' && packageOptions.prod !== false) {
  53. packageConfigs.push(createProductionConfig(format))
  54. }
  55. if (format === 'global' || format === 'esm') {
  56. packageConfigs.push(createMinifiedConfig(format))
  57. }
  58. })
  59. }
  60. export default packageConfigs
  61. function createConfig(format, output, plugins = []) {
  62. output.externalLiveBindings = false
  63. const isProductionBuild =
  64. process.env.__DEV__ === 'false' || /\.prod\.js$/.test(output.file)
  65. const isGlobalBuild = format === 'global'
  66. const isRawESMBuild = format === 'esm'
  67. const isBundlerESMBuild = /esm-bundler/.test(format)
  68. const isRuntimeCompileBuild = /vue\./.test(output.file)
  69. if (isGlobalBuild) {
  70. output.name = packageOptions.name
  71. }
  72. const shouldEmitDeclarations =
  73. process.env.TYPES != null &&
  74. process.env.NODE_ENV === 'production' &&
  75. !hasTSChecked
  76. const tsPlugin = ts({
  77. check: process.env.NODE_ENV === 'production' && !hasTSChecked,
  78. tsconfig: path.resolve(__dirname, 'tsconfig.json'),
  79. cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
  80. tsconfigOverride: {
  81. compilerOptions: {
  82. declaration: shouldEmitDeclarations,
  83. declarationMap: shouldEmitDeclarations
  84. },
  85. exclude: ['**/__tests__', 'test-dts']
  86. }
  87. })
  88. // we only need to check TS and generate declarations once for each build.
  89. // it also seems to run into weird issues when checking multiple times
  90. // during a single build.
  91. hasTSChecked = true
  92. const entryFile =
  93. format === 'esm-bundler-runtime' ? `src/runtime.ts` : `src/index.ts`
  94. return {
  95. input: resolve(entryFile),
  96. // Global and Browser ESM builds inlines everything so that they can be
  97. // used alone.
  98. external:
  99. isGlobalBuild || isRawESMBuild
  100. ? []
  101. : knownExternals.concat(Object.keys(pkg.dependencies || [])),
  102. plugins: [
  103. json({
  104. namedExports: false
  105. }),
  106. tsPlugin,
  107. createReplacePlugin(
  108. isProductionBuild,
  109. isBundlerESMBuild,
  110. (isGlobalBuild || isRawESMBuild || isBundlerESMBuild) &&
  111. !packageOptions.enableNonBrowserBranches,
  112. isRuntimeCompileBuild
  113. ),
  114. ...plugins
  115. ],
  116. output,
  117. onwarn: (msg, warn) => {
  118. if (!/Circular/.test(msg)) {
  119. warn(msg)
  120. }
  121. }
  122. }
  123. }
  124. function createReplacePlugin(
  125. isProduction,
  126. isBundlerESMBuild,
  127. isBrowserBuild,
  128. isRuntimeCompileBuild
  129. ) {
  130. const replacements = {
  131. __COMMIT__: `"${process.env.COMMIT}"`,
  132. __VERSION__: `"${masterVersion}"`,
  133. __DEV__: isBundlerESMBuild
  134. ? // preserve to be handled by bundlers
  135. `(process.env.NODE_ENV !== 'production')`
  136. : // hard coded dev/prod builds
  137. !isProduction,
  138. // this is only used during tests
  139. __TEST__: isBundlerESMBuild ? `(process.env.NODE_ENV === 'test')` : false,
  140. // If the build is expected to run directly in the browser (global / esm builds)
  141. __BROWSER__: isBrowserBuild,
  142. // is targeting bundlers?
  143. __BUNDLER__: isBundlerESMBuild,
  144. // support compile in browser?
  145. __RUNTIME_COMPILE__: isRuntimeCompileBuild,
  146. // support options?
  147. // the lean build drops options related code with buildOptions.lean: true
  148. __FEATURE_OPTIONS__: !packageOptions.lean && !process.env.LEAN,
  149. __FEATURE_SUSPENSE__: true
  150. }
  151. // allow inline overrides like
  152. //__RUNTIME_COMPILE__=true yarn build runtime-core
  153. Object.keys(replacements).forEach(key => {
  154. if (key in process.env) {
  155. replacements[key] = process.env[key]
  156. }
  157. })
  158. return replace(replacements)
  159. }
  160. function createProductionConfig(format) {
  161. return createConfig(format, {
  162. file: resolve(`dist/${name}.${format}.prod.js`),
  163. format: outputConfigs[format].format
  164. })
  165. }
  166. function createMinifiedConfig(format) {
  167. const { terser } = require('rollup-plugin-terser')
  168. return createConfig(
  169. format,
  170. {
  171. file: resolve(`dist/${name}.${format}.prod.js`),
  172. format: outputConfigs[format].format
  173. },
  174. [
  175. terser({
  176. module: /^esm/.test(format)
  177. })
  178. ]
  179. )
  180. }