rollup.config.js 5.1 KB

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