rollup.config.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. const json = require('rollup-plugin-json')
  7. if (!process.env.TARGET) {
  8. throw new Error('TARGET package must be specified via --environment flag.')
  9. }
  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. // build aliases dynamically
  17. const aliasOptions = { resolve: ['.ts'] }
  18. fs.readdirSync(packagesDir).forEach(dir => {
  19. if (
  20. !dir.startsWith('vue') &&
  21. fs.statSync(path.resolve(packagesDir, dir)).isDirectory()
  22. ) {
  23. aliasOptions[`@vue/${dir}`] = path.resolve(packagesDir, `${dir}/src/index`)
  24. }
  25. })
  26. const aliasPlugin = alias(aliasOptions)
  27. // ensure TS checks only once for each build
  28. let hasTSChecked = false
  29. const configs = {
  30. esm: {
  31. file: resolve(`dist/${name}.esm-bundler.js`),
  32. format: `es`
  33. },
  34. cjs: {
  35. file: resolve(`dist/${name}.cjs.js`),
  36. format: `cjs`
  37. },
  38. global: {
  39. file: resolve(`dist/${name}.global.js`),
  40. format: `iife`
  41. },
  42. 'esm-browser': {
  43. file: resolve(`dist/${name}.esm-browser.js`),
  44. format: `es`
  45. }
  46. }
  47. const defaultFormats = ['esm', 'cjs']
  48. const inlineFromats = process.env.FORMATS && process.env.FORMATS.split(',')
  49. const packageFormats = inlineFromats || packageOptions.formats || defaultFormats
  50. const packageConfigs = process.env.PROD_ONLY
  51. ? []
  52. : packageFormats.map(format => createConfig(configs[format]))
  53. if (process.env.NODE_ENV === 'production') {
  54. packageFormats.forEach(format => {
  55. if (format === 'cjs') {
  56. packageConfigs.push(createProductionConfig(format))
  57. }
  58. if (format === 'global' || format === 'esm-browser') {
  59. packageConfigs.push(createMinifiedConfig(format))
  60. }
  61. })
  62. }
  63. module.exports = packageConfigs
  64. function createConfig(output, plugins = []) {
  65. const isProductionBuild =
  66. process.env.__DEV__ === 'false' || /\.prod\.js$/.test(output.file)
  67. const isGlobalBuild = /\.global(\.prod)?\.js$/.test(output.file)
  68. const isBunlderESMBuild = /\.esm\.js$/.test(output.file)
  69. const isBrowserESMBuild = /esm-browser(\.prod)?\.js$/.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. exclude: ['**/__tests__']
  87. }
  88. })
  89. // we only need to check TS and generate declarations once for each build.
  90. // it also seems to run into weird issues when checking multiple times
  91. // during a single build.
  92. hasTSChecked = true
  93. const externals = Object.keys(aliasOptions).filter(p => p !== '@vue/shared')
  94. return {
  95. input: resolve(`src/index.ts`),
  96. // Global and Browser ESM builds inlines everything so that they can be
  97. // used alone.
  98. external: isGlobalBuild || isBrowserESMBuild ? [] : externals,
  99. plugins: [
  100. json({
  101. namedExports: false
  102. }),
  103. tsPlugin,
  104. aliasPlugin,
  105. createReplacePlugin(
  106. isProductionBuild,
  107. isBunlderESMBuild,
  108. isGlobalBuild || isBrowserESMBuild
  109. ),
  110. ...plugins
  111. ],
  112. output,
  113. onwarn: (msg, warn) => {
  114. if (!/Circular/.test(msg)) {
  115. warn(msg)
  116. }
  117. }
  118. }
  119. }
  120. function createReplacePlugin(isProduction, isBunlderESMBuild, isBrowserBuild) {
  121. return replace({
  122. __DEV__: isBunlderESMBuild
  123. ? // preserve to be handled by bundlers
  124. `process.env.NODE_ENV !== 'production'`
  125. : // hard coded dev/prod builds
  126. !isProduction,
  127. // If the build is expected to run directly in the browser (global / esm-browser builds)
  128. __BROWSER__: isBrowserBuild,
  129. // support options?
  130. // the lean build drops options related code with buildOptions.lean: true
  131. __FEATURE_OPTIONS__: !packageOptions.lean,
  132. __FEATURE_SUSPENSE__: true,
  133. // this is only used during tests
  134. __JSDOM__: false
  135. })
  136. }
  137. function createProductionConfig(format) {
  138. return createConfig({
  139. file: resolve(`dist/${name}.${format}.prod.js`),
  140. format: configs[format].format
  141. })
  142. }
  143. function createMinifiedConfig(format) {
  144. const { terser } = require('rollup-plugin-terser')
  145. return createConfig(
  146. {
  147. file: resolve(`dist/${name}.${format}.prod.js`),
  148. format: configs[format].format
  149. },
  150. [
  151. terser({
  152. module: /^esm/.test(format)
  153. })
  154. ]
  155. )
  156. }