rollup.config.js 6.5 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. const isRuntimeCompileBuild = packageOptions.isRuntimeCompileBuild
  71. if (isGlobalBuild) {
  72. output.name = packageOptions.name
  73. }
  74. const shouldEmitDeclarations = process.env.TYPES != null && !hasTSChecked
  75. const tsPlugin = ts({
  76. check: process.env.NODE_ENV === 'production' && !hasTSChecked,
  77. tsconfig: path.resolve(__dirname, 'tsconfig.json'),
  78. cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
  79. tsconfigOverride: {
  80. compilerOptions: {
  81. sourceMap: output.sourcemap,
  82. declaration: shouldEmitDeclarations,
  83. declarationMap: shouldEmitDeclarations
  84. },
  85. exclude: ['**/__tests__', 'test-dts']
  86. }
  87. })
  88. // we only need to check TS and generate declarations once for each build.
  89. // it also seems to run into weird issues when checking multiple times
  90. // during a single build.
  91. hasTSChecked = true
  92. const entryFile =
  93. format === 'esm-bundler-runtime' ? `src/runtime.ts` : `src/index.ts`
  94. const external =
  95. isGlobalBuild || isRawESMBuild ? [] : Object.keys(pkg.dependencies || {})
  96. const nodePlugins = packageOptions.enableNonBrowserBranches
  97. ? [
  98. require('@rollup/plugin-node-resolve')(),
  99. require('@rollup/plugin-commonjs')()
  100. ]
  101. : []
  102. return {
  103. input: resolve(entryFile),
  104. // Global and Browser ESM builds inlines everything so that they can be
  105. // used alone.
  106. external,
  107. plugins: [
  108. json({
  109. namedExports: false
  110. }),
  111. tsPlugin,
  112. createReplacePlugin(
  113. isProductionBuild,
  114. isBundlerESMBuild,
  115. // isBrowserBuild?
  116. (isGlobalBuild || isRawESMBuild || isBundlerESMBuild) &&
  117. !packageOptions.enableNonBrowserBranches,
  118. isRuntimeCompileBuild,
  119. isGlobalBuild,
  120. isNodeBuild
  121. ),
  122. ...nodePlugins,
  123. ...plugins
  124. ],
  125. output,
  126. onwarn: (msg, warn) => {
  127. if (!/Circular/.test(msg)) {
  128. warn(msg)
  129. }
  130. }
  131. }
  132. }
  133. function createReplacePlugin(
  134. isProduction,
  135. isBundlerESMBuild,
  136. isBrowserBuild,
  137. isRuntimeCompileBuild,
  138. isGlobalBuild,
  139. isNodeBuild
  140. ) {
  141. const replacements = {
  142. __COMMIT__: `"${process.env.COMMIT}"`,
  143. __VERSION__: `"${masterVersion}"`,
  144. __DEV__: isBundlerESMBuild
  145. ? // preserve to be handled by bundlers
  146. `(process.env.NODE_ENV !== 'production')`
  147. : // hard coded dev/prod builds
  148. !isProduction,
  149. // this is only used during tests
  150. __TEST__: isBundlerESMBuild ? `(process.env.NODE_ENV === 'test')` : false,
  151. // If the build is expected to run directly in the browser (global / esm builds)
  152. __BROWSER__: isBrowserBuild,
  153. // is targeting bundlers?
  154. __BUNDLER__: isBundlerESMBuild,
  155. // support compile in browser?
  156. __RUNTIME_COMPILE__: isRuntimeCompileBuild,
  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. }