rollup.config.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 alias from 'rollup-plugin-alias'
  6. import json from 'rollup-plugin-json'
  7. import lernaJson from './lerna.json'
  8. if (!process.env.TARGET) {
  9. throw new Error('TARGET package must be specified via --environment flag.')
  10. }
  11. const packagesDir = path.resolve(__dirname, 'packages')
  12. const packageDir = path.resolve(packagesDir, process.env.TARGET)
  13. const name = path.basename(packageDir)
  14. const resolve = p => path.resolve(packageDir, p)
  15. const pkg = require(resolve(`package.json`))
  16. const packageOptions = pkg.buildOptions || {}
  17. // build aliases dynamically
  18. const aliasOptions = { resolve: ['.ts'] }
  19. fs.readdirSync(packagesDir).forEach(dir => {
  20. if (dir === 'vue') {
  21. return
  22. }
  23. if (fs.statSync(path.resolve(packagesDir, dir)).isDirectory()) {
  24. aliasOptions[`@vue/${dir}`] = path.resolve(packagesDir, `${dir}/src/index`)
  25. }
  26. })
  27. const aliasPlugin = alias(aliasOptions)
  28. // ensure TS checks only once for each build
  29. let hasTSChecked = false
  30. const configs = {
  31. esm: {
  32. file: resolve(`dist/${name}.esm-bundler.js`),
  33. format: `es`
  34. },
  35. cjs: {
  36. file: resolve(`dist/${name}.cjs.js`),
  37. format: `cjs`
  38. },
  39. global: {
  40. file: resolve(`dist/${name}.global.js`),
  41. format: `iife`
  42. },
  43. 'esm-browser': {
  44. file: resolve(`dist/${name}.esm-browser.js`),
  45. format: `es`
  46. }
  47. }
  48. const defaultFormats = ['esm', 'cjs']
  49. const inlineFormats = process.env.FORMATS && process.env.FORMATS.split(',')
  50. const packageFormats = inlineFormats || packageOptions.formats || defaultFormats
  51. const packageConfigs = process.env.PROD_ONLY
  52. ? []
  53. : packageFormats.map(format => createConfig(configs[format]))
  54. if (process.env.NODE_ENV === 'production') {
  55. packageFormats.forEach(format => {
  56. if (format === 'cjs') {
  57. packageConfigs.push(createProductionConfig(format))
  58. }
  59. if (format === 'global' || format === 'esm-browser') {
  60. packageConfigs.push(createMinifiedConfig(format))
  61. }
  62. })
  63. }
  64. export default packageConfigs
  65. function createConfig(output, plugins = []) {
  66. const isProductionBuild =
  67. process.env.__DEV__ === 'false' || /\.prod\.js$/.test(output.file)
  68. const isGlobalBuild = /\.global(\.prod)?\.js$/.test(output.file)
  69. const isBundlerESMBuild = /\.esm-bundler\.js$/.test(output.file)
  70. const isBrowserESMBuild = /esm-browser(\.prod)?\.js$/.test(output.file)
  71. const isRuntimeCompileBuild = /vue\./.test(output.file)
  72. if (isGlobalBuild) {
  73. output.name = packageOptions.name
  74. }
  75. const shouldEmitDeclarations =
  76. process.env.TYPES != null &&
  77. process.env.NODE_ENV === 'production' &&
  78. !hasTSChecked
  79. const tsPlugin = ts({
  80. check: process.env.NODE_ENV === 'production' && !hasTSChecked,
  81. tsconfig: path.resolve(__dirname, 'tsconfig.json'),
  82. cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
  83. tsconfigOverride: {
  84. compilerOptions: {
  85. declaration: shouldEmitDeclarations,
  86. declarationMap: shouldEmitDeclarations
  87. },
  88. exclude: ['**/__tests__', 'test-dts']
  89. }
  90. })
  91. // we only need to check TS and generate declarations once for each build.
  92. // it also seems to run into weird issues when checking multiple times
  93. // during a single build.
  94. hasTSChecked = true
  95. const externals = Object.keys(aliasOptions).filter(p => p !== '@vue/shared')
  96. return {
  97. input: resolve(`src/index.ts`),
  98. // Global and Browser ESM builds inlines everything so that they can be
  99. // used alone.
  100. external: isGlobalBuild || isBrowserESMBuild ? [] : externals,
  101. plugins: [
  102. json({
  103. namedExports: false
  104. }),
  105. tsPlugin,
  106. aliasPlugin,
  107. createReplacePlugin(
  108. isProductionBuild,
  109. isBundlerESMBuild,
  110. (isGlobalBuild || isBrowserESMBuild) &&
  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. return replace({
  131. __COMMIT__: `"${process.env.COMMIT}"`,
  132. __VERSION__: `"${lernaJson.version}"`,
  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-browser builds)
  141. __BROWSER__: isBrowserBuild,
  142. // support compile in browser?
  143. __RUNTIME_COMPILE__: isRuntimeCompileBuild,
  144. // support options?
  145. // the lean build drops options related code with buildOptions.lean: true
  146. __FEATURE_OPTIONS__: !packageOptions.lean && !process.env.LEAN,
  147. __FEATURE_SUSPENSE__: true
  148. })
  149. }
  150. function createProductionConfig(format) {
  151. return createConfig({
  152. file: resolve(`dist/${name}.${format}.prod.js`),
  153. format: configs[format].format
  154. })
  155. }
  156. function createMinifiedConfig(format) {
  157. const { terser } = require('rollup-plugin-terser')
  158. return createConfig(
  159. {
  160. file: resolve(`dist/${name}.${format}.prod.js`),
  161. format: configs[format].format
  162. },
  163. [
  164. terser({
  165. module: /^esm/.test(format)
  166. })
  167. ]
  168. )
  169. }