rollup.config.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 tsPlugin = ts({
  74. check: process.env.NODE_ENV === 'production' && !hasTSChecked,
  75. tsconfig: path.resolve(__dirname, 'tsconfig.json'),
  76. cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
  77. tsconfigOverride: {
  78. compilerOptions: {
  79. declaration: process.env.NODE_ENV === 'production' && !hasTSChecked
  80. }
  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. const externals = Object.keys(aliasOptions).filter(p => p !== '@vue/shared')
  88. return {
  89. input: resolve(`src/index.ts`),
  90. // Global and Browser ESM builds inlines everything so that they can be
  91. // used alone.
  92. external: isGlobalBuild || isBrowserESMBuild ? [] : externals,
  93. plugins: [
  94. tsPlugin,
  95. aliasPlugin,
  96. createReplacePlugin(isProductionBuild, isBunlderESMBuild, isCompat),
  97. ...plugins
  98. ],
  99. output,
  100. onwarn: (msg, warn) => {
  101. if (!/Circular/.test(msg)) {
  102. warn(msg)
  103. }
  104. }
  105. }
  106. }
  107. function createReplacePlugin(isProduction, isBunlderESMBuild, isCompat) {
  108. return replace({
  109. __DEV__: isBunlderESMBuild
  110. ? // preserve to be handled by bundlers
  111. `process.env.NODE_ENV !== 'production'`
  112. : // hard coded dev/prod builds
  113. !isProduction,
  114. // compatibility builds
  115. __COMPAT__: !!packageOptions.compat,
  116. // this is only used during tests
  117. __JSDOM__: false
  118. })
  119. }
  120. function createProductionConfig(format) {
  121. return createConfig({
  122. file: resolve(`dist/${name}.${format}.prod.js`),
  123. format: configs[format].format
  124. })
  125. }
  126. function createMinifiedConfig(format) {
  127. const { terser } = require('rollup-plugin-terser')
  128. return createConfig(
  129. {
  130. file: resolve(`dist/${name}.${format}.prod.js`),
  131. format: configs[format].format
  132. },
  133. [
  134. terser({
  135. module: /^esm/.test(format)
  136. })
  137. ]
  138. )
  139. }