rollup.config.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if (!output) {
  63. console.log(require('chalk').yellow(`invalid format: "${format}"`))
  64. process.exit(1)
  65. }
  66. output.externalLiveBindings = false
  67. const isProductionBuild =
  68. process.env.__DEV__ === 'false' || /\.prod\.js$/.test(output.file)
  69. const isGlobalBuild = format === 'global'
  70. const isRawESMBuild = format === 'esm'
  71. const isNodeBuild = format === 'cjs'
  72. const isBundlerESMBuild = /esm-bundler/.test(format)
  73. const isRuntimeCompileBuild = /vue\./.test(output.file)
  74. if (isGlobalBuild) {
  75. output.name = packageOptions.name
  76. }
  77. const shouldEmitDeclarations =
  78. process.env.TYPES != null &&
  79. process.env.NODE_ENV === 'production' &&
  80. !hasTSChecked
  81. const tsPlugin = ts({
  82. check: process.env.NODE_ENV === 'production' && !hasTSChecked,
  83. tsconfig: path.resolve(__dirname, 'tsconfig.json'),
  84. cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
  85. tsconfigOverride: {
  86. compilerOptions: {
  87. declaration: shouldEmitDeclarations,
  88. declarationMap: shouldEmitDeclarations
  89. },
  90. exclude: ['**/__tests__', 'test-dts']
  91. }
  92. })
  93. // we only need to check TS and generate declarations once for each build.
  94. // it also seems to run into weird issues when checking multiple times
  95. // during a single build.
  96. hasTSChecked = true
  97. const entryFile =
  98. format === 'esm-bundler-runtime' ? `src/runtime.ts` : `src/index.ts`
  99. const external =
  100. isGlobalBuild || isRawESMBuild
  101. ? []
  102. : knownExternals.concat(Object.keys(pkg.dependencies || []))
  103. return {
  104. input: resolve(entryFile),
  105. // Global and Browser ESM builds inlines everything so that they can be
  106. // used alone.
  107. external,
  108. plugins: [
  109. json({
  110. namedExports: false
  111. }),
  112. tsPlugin,
  113. createReplacePlugin(
  114. isProductionBuild,
  115. isBundlerESMBuild,
  116. // isBrowserBuild?
  117. (isGlobalBuild || isRawESMBuild || isBundlerESMBuild) &&
  118. !packageOptions.enableNonBrowserBranches,
  119. isRuntimeCompileBuild,
  120. isNodeBuild
  121. ),
  122. ...plugins
  123. ],
  124. output,
  125. onwarn: (msg, warn) => {
  126. if (!/Circular/.test(msg)) {
  127. warn(msg)
  128. }
  129. }
  130. }
  131. }
  132. function createReplacePlugin(
  133. isProduction,
  134. isBundlerESMBuild,
  135. isBrowserBuild,
  136. isRuntimeCompileBuild,
  137. isNodeBuild
  138. ) {
  139. const replacements = {
  140. __COMMIT__: `"${process.env.COMMIT}"`,
  141. __VERSION__: `"${masterVersion}"`,
  142. __DEV__: isBundlerESMBuild
  143. ? // preserve to be handled by bundlers
  144. `(process.env.NODE_ENV !== 'production')`
  145. : // hard coded dev/prod builds
  146. !isProduction,
  147. // this is only used during tests
  148. __TEST__: isBundlerESMBuild ? `(process.env.NODE_ENV === 'test')` : false,
  149. // If the build is expected to run directly in the browser (global / esm builds)
  150. __BROWSER__: isBrowserBuild,
  151. // is targeting bundlers?
  152. __BUNDLER__: isBundlerESMBuild,
  153. // support compile in browser?
  154. __RUNTIME_COMPILE__: isRuntimeCompileBuild,
  155. // is targeting Node (SSR)?
  156. __SSR__: isNodeBuild,
  157. // support options?
  158. // the lean build drops options related code with buildOptions.lean: true
  159. __FEATURE_OPTIONS__: !packageOptions.lean && !process.env.LEAN,
  160. __FEATURE_SUSPENSE__: true
  161. }
  162. // allow inline overrides like
  163. //__RUNTIME_COMPILE__=true yarn build runtime-core
  164. Object.keys(replacements).forEach(key => {
  165. if (key in process.env) {
  166. replacements[key] = process.env[key]
  167. }
  168. })
  169. return replace(replacements)
  170. }
  171. function createProductionConfig(format) {
  172. return createConfig(format, {
  173. file: resolve(`dist/${name}.${format}.prod.js`),
  174. format: outputConfigs[format].format
  175. })
  176. }
  177. function createMinifiedConfig(format) {
  178. const { terser } = require('rollup-plugin-terser')
  179. return createConfig(
  180. format,
  181. {
  182. file: resolve(`dist/${name}.${format}.prod.js`),
  183. format: outputConfigs[format].format
  184. },
  185. [
  186. terser({
  187. module: /^esm/.test(format)
  188. })
  189. ]
  190. )
  191. }