rollup.config.js 6.3 KB

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