rollup.config.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 (fs.statSync(path.resolve(packagesDir, dir)).isDirectory()) {
  19. aliasOptions[`@vue/${dir}`] = path.resolve(packagesDir, `${dir}/src/index`)
  20. }
  21. })
  22. const aliasPlugin = alias(aliasOptions)
  23. // ensure TS checks only once for each build
  24. let hasTSChecked = false
  25. const configs = {
  26. esm: {
  27. file: resolve(`dist/${name}.esm-bundler.js`),
  28. format: `es`
  29. },
  30. cjs: {
  31. file: resolve(`dist/${name}.cjs.js`),
  32. format: `cjs`
  33. },
  34. global: {
  35. file: resolve(`dist/${name}.global.js`),
  36. format: `iife`
  37. },
  38. 'esm-browser': {
  39. file: resolve(`dist/${name}.esm-browser.js`),
  40. format: `es`
  41. }
  42. }
  43. const defaultFormats = ['esm', 'cjs']
  44. const inlineFromats = process.env.FORMATS && process.env.FORMATS.split(',')
  45. const packageFormats = inlineFromats || packageOptions.formats || defaultFormats
  46. const packageConfigs = packageFormats.map(format =>
  47. createConfig(configs[format])
  48. )
  49. if (process.env.NODE_ENV === 'production') {
  50. packageFormats.forEach(format => {
  51. if (format === 'cjs') {
  52. packageConfigs.push(createProductionConfig(format))
  53. }
  54. if (format === 'global' || format === 'esm-browser') {
  55. packageConfigs.push(createMinifiedConfig(format))
  56. }
  57. })
  58. }
  59. module.exports = packageConfigs
  60. function createConfig(output, plugins = []) {
  61. const isProductionBuild = /\.prod\.js$/.test(output.file)
  62. const isGlobalBuild = /\.global(\.prod)?\.js$/.test(output.file)
  63. const isBunlderESMBuild = /\.esm\.js$/.test(output.file)
  64. const isBrowserESMBuild = /esm-browser(\.prod)?\.js$/.test(output.file)
  65. if (isGlobalBuild) {
  66. output.name = packageOptions.name
  67. }
  68. const tsPlugin = ts({
  69. check: process.env.NODE_ENV === 'production' && !hasTSChecked,
  70. tsconfig: path.resolve(__dirname, 'tsconfig.json'),
  71. cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
  72. tsconfigOverride: {
  73. compilerOptions: {
  74. declaration: process.env.NODE_ENV === 'production' && !hasTSChecked
  75. }
  76. }
  77. })
  78. // we only need to check TS and generate declarations once for each build.
  79. // it also seems to run into weird issues when checking multiple times
  80. // during a single build.
  81. hasTSChecked = true
  82. return {
  83. input: resolve(`src/index.ts`),
  84. // Global and Browser ESM builds inlines everything so that they can be
  85. // used alone.
  86. external:
  87. isGlobalBuild || isBrowserESMBuild ? [] : Object.keys(aliasOptions),
  88. plugins: [
  89. tsPlugin,
  90. aliasPlugin,
  91. createReplacePlugin(isProductionBuild, isBunlderESMBuild),
  92. ...plugins
  93. ],
  94. output,
  95. onwarn: (msg, warn) => {
  96. if (!/Circular/.test(msg)) {
  97. warn(msg)
  98. }
  99. }
  100. }
  101. }
  102. function createReplacePlugin(isProduction, isBunlderESMBuild) {
  103. return replace({
  104. __DEV__: isBunlderESMBuild
  105. ? // preserve to be handled by bundlers
  106. `process.env.NODE_ENV !== 'production'`
  107. : // hard coded dev/prod builds
  108. !isProduction,
  109. // compatibility builds
  110. __COMPAT__: !!process.env.COMPAT
  111. })
  112. }
  113. function createProductionConfig(format) {
  114. return createConfig({
  115. file: resolve(`dist/${name}.${format}.prod.js`),
  116. format: configs[format].format
  117. })
  118. }
  119. function createMinifiedConfig(format) {
  120. const { terser } = require('rollup-plugin-terser')
  121. return createConfig(
  122. {
  123. file: resolve(`dist/${name}.${format}.prod.js`),
  124. format: configs[format].format
  125. },
  126. [
  127. terser({
  128. module: /^esm/.test(format)
  129. })
  130. ]
  131. )
  132. }