rollup.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. const fs = require('fs')
  2. const path = require('path')
  3. const ts = require('rollup-plugin-typescript2')
  4. const replace = require('rollup-plugin-replace')
  5. const alias = require('rollup-plugin-alias')
  6. if (!process.env.TARGET) {
  7. throw new Error('TARGET package must be specified via --environment flag.')
  8. }
  9. const packagesDir = path.resolve(__dirname, 'packages')
  10. const packageDir = path.resolve(packagesDir, process.env.TARGET)
  11. const name = path.basename(packageDir)
  12. const resolve = p => path.resolve(packageDir, p)
  13. const pkg = require(resolve(`package.json`))
  14. const packageOptions = pkg.buildOptions || {}
  15. // build aliases dynamically
  16. const aliasOptions = { resolve: ['.ts'] }
  17. fs.readdirSync(packagesDir).forEach(dir => {
  18. if (
  19. !dir.startsWith('vue') &&
  20. fs.statSync(path.resolve(packagesDir, dir)).isDirectory()
  21. ) {
  22. aliasOptions[`@vue/${dir}`] = path.resolve(packagesDir, `${dir}/src/index`)
  23. }
  24. })
  25. const aliasPlugin = alias(aliasOptions)
  26. // ensure TS checks only once for each build
  27. let hasTSChecked = false
  28. const configs = {
  29. esm: {
  30. file: resolve(`dist/${name}.esm-bundler.js`),
  31. format: `es`
  32. },
  33. cjs: {
  34. file: resolve(`dist/${name}.cjs.js`),
  35. format: `cjs`
  36. },
  37. global: {
  38. file: resolve(`dist/${name}.global.js`),
  39. format: `iife`
  40. },
  41. 'esm-browser': {
  42. file: resolve(`dist/${name}.esm-browser.js`),
  43. format: `es`
  44. }
  45. }
  46. const defaultFormats = ['esm', 'cjs']
  47. const inlineFromats = process.env.FORMATS && process.env.FORMATS.split(',')
  48. const packageFormats = inlineFromats || packageOptions.formats || defaultFormats
  49. const packageConfigs = packageFormats.map(format =>
  50. createConfig(configs[format])
  51. )
  52. if (process.env.NODE_ENV === 'production') {
  53. packageFormats.forEach(format => {
  54. if (format === 'cjs') {
  55. packageConfigs.push(createProductionConfig(format))
  56. }
  57. if (format === 'global' || format === 'esm-browser') {
  58. packageConfigs.push(createMinifiedConfig(format))
  59. }
  60. })
  61. }
  62. module.exports = packageConfigs
  63. function createConfig(output, plugins = []) {
  64. const isProductionBuild =
  65. process.env.__DEV__ === 'false' || /\.prod\.js$/.test(output.file)
  66. const isGlobalBuild = /\.global(\.prod)?\.js$/.test(output.file)
  67. const isBunlderESMBuild = /\.esm\.js$/.test(output.file)
  68. const isBrowserESMBuild = /esm-browser(\.prod)?\.js$/.test(output.file)
  69. const isCompat = /dist\/vue\./.test(output.file)
  70. if (isGlobalBuild) {
  71. output.name = packageOptions.name
  72. }
  73. const shouldEmitDeclarations =
  74. process.env.TYPES != null &&
  75. process.env.NODE_ENV === 'production' &&
  76. !hasTSChecked
  77. const tsPlugin = ts({
  78. check: process.env.NODE_ENV === 'production' && !hasTSChecked,
  79. tsconfig: path.resolve(__dirname, 'tsconfig.json'),
  80. cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
  81. tsconfigOverride: {
  82. compilerOptions: {
  83. declaration: shouldEmitDeclarations,
  84. declarationMap: shouldEmitDeclarations
  85. }
  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 externals = Object.keys(aliasOptions).filter(p => p !== '@vue/shared')
  93. return {
  94. input: resolve(`src/index.ts`),
  95. // Global and Browser ESM builds inlines everything so that they can be
  96. // used alone.
  97. external: isGlobalBuild || isBrowserESMBuild ? [] : externals,
  98. plugins: [
  99. tsPlugin,
  100. aliasPlugin,
  101. createReplacePlugin(isProductionBuild, isBunlderESMBuild, isCompat),
  102. ...plugins
  103. ],
  104. output,
  105. onwarn: (msg, warn) => {
  106. if (!/Circular/.test(msg)) {
  107. warn(msg)
  108. }
  109. }
  110. }
  111. }
  112. function createReplacePlugin(isProduction, isBunlderESMBuild, isCompat) {
  113. return replace({
  114. __DEV__: isBunlderESMBuild
  115. ? // preserve to be handled by bundlers
  116. `process.env.NODE_ENV !== 'production'`
  117. : // hard coded dev/prod builds
  118. !isProduction,
  119. // compatibility builds
  120. __COMPAT__: !!packageOptions.compat,
  121. // this is only used during tests
  122. __JSDOM__: false
  123. })
  124. }
  125. function createProductionConfig(format) {
  126. return createConfig({
  127. file: resolve(`dist/${name}.${format}.prod.js`),
  128. format: configs[format].format
  129. })
  130. }
  131. function createMinifiedConfig(format) {
  132. const { terser } = require('rollup-plugin-terser')
  133. return createConfig(
  134. {
  135. file: resolve(`dist/${name}.${format}.prod.js`),
  136. format: configs[format].format
  137. },
  138. [
  139. terser({
  140. module: /^esm/.test(format)
  141. })
  142. ]
  143. )
  144. }