rollup.config.js 6.5 KB

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