rollup.config.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import path from 'path'
  2. import ts from 'rollup-plugin-typescript2'
  3. import replace from '@rollup/plugin-replace'
  4. import json from '@rollup/plugin-json'
  5. if (!process.env.TARGET) {
  6. throw new Error('TARGET package must be specified via --environment flag.')
  7. }
  8. const masterVersion = require('./package.json').version
  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. // ensure TS checks only once for each build
  16. let hasTSChecked = false
  17. const outputConfigs = {
  18. 'esm-bundler': {
  19. file: resolve(`dist/${name}.esm-bundler.js`),
  20. format: `es`
  21. },
  22. // main "vue" package only
  23. 'esm-bundler-runtime': {
  24. file: resolve(`dist/${name}.runtime.esm-bundler.js`),
  25. format: `es`
  26. },
  27. cjs: {
  28. file: resolve(`dist/${name}.cjs.js`),
  29. format: `cjs`
  30. },
  31. global: {
  32. file: resolve(`dist/${name}.global.js`),
  33. format: `iife`
  34. },
  35. esm: {
  36. file: resolve(`dist/${name}.esm.js`),
  37. format: `es`
  38. }
  39. }
  40. const defaultFormats = ['esm-bundler', 'cjs']
  41. const inlineFormats = process.env.FORMATS && process.env.FORMATS.split(',')
  42. const packageFormats = inlineFormats || packageOptions.formats || defaultFormats
  43. const packageConfigs = process.env.PROD_ONLY
  44. ? []
  45. : packageFormats.map(format => createConfig(format, outputConfigs[format]))
  46. if (process.env.NODE_ENV === 'production') {
  47. packageFormats.forEach(format => {
  48. if (format === 'cjs' && packageOptions.prod !== false) {
  49. packageConfigs.push(createProductionConfig(format))
  50. }
  51. if (format === 'global' || format === 'esm') {
  52. packageConfigs.push(createMinifiedConfig(format))
  53. }
  54. })
  55. }
  56. export default packageConfigs
  57. function createConfig(format, output, plugins = []) {
  58. if (!output) {
  59. console.log(require('chalk').yellow(`invalid format: "${format}"`))
  60. process.exit(1)
  61. }
  62. output.sourcemap = !!process.env.SOURCE_MAP
  63. output.externalLiveBindings = false
  64. const isProductionBuild =
  65. process.env.__DEV__ === 'false' || /\.prod\.js$/.test(output.file)
  66. const isGlobalBuild = format === 'global'
  67. const isRawESMBuild = format === 'esm'
  68. const isNodeBuild = format === 'cjs'
  69. const isBundlerESMBuild = /esm-bundler/.test(format)
  70. if (isGlobalBuild) {
  71. output.name = packageOptions.name
  72. }
  73. const shouldEmitDeclarations = process.env.TYPES != null && !hasTSChecked
  74. const tsPlugin = ts({
  75. check: process.env.NODE_ENV === 'production' && !hasTSChecked,
  76. tsconfig: path.resolve(__dirname, 'tsconfig.json'),
  77. cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
  78. tsconfigOverride: {
  79. compilerOptions: {
  80. sourceMap: output.sourcemap,
  81. declaration: shouldEmitDeclarations,
  82. declarationMap: shouldEmitDeclarations
  83. },
  84. exclude: ['**/__tests__', 'test-dts']
  85. }
  86. })
  87. // we only need to check TS and generate declarations once for each build.
  88. // it also seems to run into weird issues when checking multiple times
  89. // during a single build.
  90. hasTSChecked = true
  91. const entryFile =
  92. format === 'esm-bundler-runtime' ? `src/runtime.ts` : `src/index.ts`
  93. const external =
  94. isGlobalBuild || isRawESMBuild
  95. ? []
  96. : [
  97. ...Object.keys(pkg.dependencies || {}),
  98. ...Object.keys(pkg.peerDependencies || {})
  99. ]
  100. const nodePlugins = packageOptions.enableNonBrowserBranches
  101. ? [
  102. require('@rollup/plugin-node-resolve')(),
  103. require('@rollup/plugin-commonjs')()
  104. ]
  105. : []
  106. return {
  107. input: resolve(entryFile),
  108. // Global and Browser ESM builds inlines everything so that they can be
  109. // used alone.
  110. external,
  111. plugins: [
  112. json({
  113. namedExports: false
  114. }),
  115. tsPlugin,
  116. createReplacePlugin(
  117. isProductionBuild,
  118. isBundlerESMBuild,
  119. // isBrowserBuild?
  120. (isGlobalBuild || isRawESMBuild || isBundlerESMBuild) &&
  121. !packageOptions.enableNonBrowserBranches,
  122. isGlobalBuild,
  123. isNodeBuild
  124. ),
  125. ...nodePlugins,
  126. ...plugins
  127. ],
  128. output,
  129. onwarn: (msg, warn) => {
  130. if (!/Circular/.test(msg)) {
  131. warn(msg)
  132. }
  133. }
  134. }
  135. }
  136. function createReplacePlugin(
  137. isProduction,
  138. isBundlerESMBuild,
  139. isBrowserBuild,
  140. isGlobalBuild,
  141. isNodeBuild
  142. ) {
  143. const replacements = {
  144. __COMMIT__: `"${process.env.COMMIT}"`,
  145. __VERSION__: `"${masterVersion}"`,
  146. __DEV__: isBundlerESMBuild
  147. ? // preserve to be handled by bundlers
  148. `(process.env.NODE_ENV !== 'production')`
  149. : // hard coded dev/prod builds
  150. !isProduction,
  151. // this is only used during tests
  152. __TEST__: isBundlerESMBuild ? `(process.env.NODE_ENV === 'test')` : false,
  153. // If the build is expected to run directly in the browser (global / esm builds)
  154. __BROWSER__: isBrowserBuild,
  155. // is targeting bundlers?
  156. __BUNDLER__: isBundlerESMBuild,
  157. __GLOBAL__: isGlobalBuild,
  158. // is targeting Node (SSR)?
  159. __NODE_JS__: isNodeBuild,
  160. // support options?
  161. // the lean build drops options related code with buildOptions.lean: true
  162. __FEATURE_OPTIONS__: !packageOptions.lean && !process.env.LEAN,
  163. __FEATURE_SUSPENSE__: true,
  164. ...(isProduction && isBrowserBuild
  165. ? {
  166. 'context.onError(': `/*#__PURE__*/ context.onError(`,
  167. 'emitError(': `/*#__PURE__*/ emitError(`,
  168. 'createCompilerError(': `/*#__PURE__*/ createCompilerError(`,
  169. 'createDOMCompilerError(': `/*#__PURE__*/ createDOMCompilerError(`
  170. }
  171. : {})
  172. }
  173. // allow inline overrides like
  174. //__RUNTIME_COMPILE__=true yarn build runtime-core
  175. Object.keys(replacements).forEach(key => {
  176. if (key in process.env) {
  177. replacements[key] = process.env[key]
  178. }
  179. })
  180. return replace(replacements)
  181. }
  182. function createProductionConfig(format) {
  183. return createConfig(format, {
  184. file: resolve(`dist/${name}.${format}.prod.js`),
  185. format: outputConfigs[format].format
  186. })
  187. }
  188. function createMinifiedConfig(format) {
  189. const { terser } = require('rollup-plugin-terser')
  190. return createConfig(
  191. format,
  192. {
  193. file: resolve(`dist/${name}.${format}.prod.js`),
  194. format: outputConfigs[format].format
  195. },
  196. [
  197. terser({
  198. module: /^esm/.test(format),
  199. compress: {
  200. ecma: 2015,
  201. pure_getters: true
  202. }
  203. })
  204. ]
  205. )
  206. }