rollup.config.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import fs from 'fs'
  2. import path from 'path'
  3. import ts from 'rollup-plugin-typescript2'
  4. import replace from '@rollup/plugin-replace'
  5. import json from '@rollup/plugin-json'
  6. if (!process.env.TARGET) {
  7. throw new Error('TARGET package must be specified via --environment flag.')
  8. }
  9. const masterVersion = require('./package.json').version
  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. const knownExternals = fs.readdirSync(packagesDir).filter(p => {
  17. return p !== '@vue/shared'
  18. })
  19. // ensure TS checks only once for each build
  20. let hasTSChecked = false
  21. const outputConfigs = {
  22. 'esm-bundler': {
  23. file: resolve(`dist/${name}.esm-bundler.js`),
  24. format: `es`
  25. },
  26. // main "vue" package only
  27. 'esm-bundler-runtime': {
  28. file: resolve(`dist/${name}.runtime.esm-bundler.js`),
  29. format: `es`
  30. },
  31. cjs: {
  32. file: resolve(`dist/${name}.cjs.js`),
  33. format: `cjs`
  34. },
  35. global: {
  36. file: resolve(`dist/${name}.global.js`),
  37. format: `iife`
  38. },
  39. esm: {
  40. file: resolve(`dist/${name}.esm.js`),
  41. format: `es`
  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 (format === 'global' || 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 = format === 'global'
  71. const isRawESMBuild = format === 'esm'
  72. const isNodeBuild = format === 'cjs'
  73. const isBundlerESMBuild = /esm-bundler/.test(format)
  74. const isRuntimeCompileBuild = /vue\./.test(output.file)
  75. if (isGlobalBuild) {
  76. output.name = packageOptions.name
  77. }
  78. const shouldEmitDeclarations = process.env.TYPES != null && !hasTSChecked
  79. const tsPlugin = ts({
  80. check: process.env.NODE_ENV === 'production' && !hasTSChecked,
  81. tsconfig: path.resolve(__dirname, 'tsconfig.json'),
  82. cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
  83. tsconfigOverride: {
  84. compilerOptions: {
  85. sourceMap: output.sourcemap,
  86. declaration: shouldEmitDeclarations,
  87. declarationMap: shouldEmitDeclarations
  88. },
  89. exclude: ['**/__tests__', 'test-dts']
  90. }
  91. })
  92. // we only need to check TS and generate declarations once for each build.
  93. // it also seems to run into weird issues when checking multiple times
  94. // during a single build.
  95. hasTSChecked = true
  96. const entryFile =
  97. format === 'esm-bundler-runtime' ? `src/runtime.ts` : `src/index.ts`
  98. const external =
  99. isGlobalBuild || isRawESMBuild
  100. ? []
  101. : knownExternals.concat(Object.keys(pkg.dependencies || []))
  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. ...plugins
  123. ],
  124. output,
  125. onwarn: (msg, warn) => {
  126. if (!/Circular/.test(msg)) {
  127. warn(msg)
  128. }
  129. }
  130. }
  131. }
  132. function createReplacePlugin(
  133. isProduction,
  134. isBundlerESMBuild,
  135. isBrowserBuild,
  136. isRuntimeCompileBuild,
  137. isGlobalBuild,
  138. isNodeBuild
  139. ) {
  140. const replacements = {
  141. __COMMIT__: `"${process.env.COMMIT}"`,
  142. __VERSION__: `"${masterVersion}"`,
  143. __DEV__: isBundlerESMBuild
  144. ? // preserve to be handled by bundlers
  145. `(process.env.NODE_ENV !== 'production')`
  146. : // hard coded dev/prod builds
  147. !isProduction,
  148. // this is only used during tests
  149. __TEST__: isBundlerESMBuild ? `(process.env.NODE_ENV === 'test')` : false,
  150. // If the build is expected to run directly in the browser (global / esm builds)
  151. __BROWSER__: isBrowserBuild,
  152. // is targeting bundlers?
  153. __BUNDLER__: isBundlerESMBuild,
  154. // support compile in browser?
  155. __RUNTIME_COMPILE__: isRuntimeCompileBuild,
  156. __GLOBAL__: isGlobalBuild,
  157. // is targeting Node (SSR)?
  158. __NODE_JS__: isNodeBuild,
  159. // support options?
  160. // the lean build drops options related code with buildOptions.lean: true
  161. __FEATURE_OPTIONS__: !packageOptions.lean && !process.env.LEAN,
  162. __FEATURE_SUSPENSE__: true,
  163. ...(isProduction && isBrowserBuild
  164. ? {
  165. 'context.onError(': `/*#__PURE__*/ context.onError(`,
  166. 'emitError(': `/*#__PURE__*/ emitError(`,
  167. 'createCompilerError(': `/*#__PURE__*/ createCompilerError(`,
  168. 'createDOMCompilerError(': `/*#__PURE__*/ createDOMCompilerError(`
  169. }
  170. : {})
  171. }
  172. // allow inline overrides like
  173. //__RUNTIME_COMPILE__=true yarn build runtime-core
  174. Object.keys(replacements).forEach(key => {
  175. if (key in process.env) {
  176. replacements[key] = process.env[key]
  177. }
  178. })
  179. return replace(replacements)
  180. }
  181. function createProductionConfig(format) {
  182. return createConfig(format, {
  183. file: resolve(`dist/${name}.${format}.prod.js`),
  184. format: outputConfigs[format].format
  185. })
  186. }
  187. function createMinifiedConfig(format) {
  188. const { terser } = require('rollup-plugin-terser')
  189. return createConfig(
  190. format,
  191. {
  192. file: resolve(`dist/${name}.${format}.prod.js`),
  193. format: outputConfigs[format].format
  194. },
  195. [
  196. terser({
  197. module: /^esm/.test(format),
  198. compress: {
  199. ecma: 2015,
  200. pure_getters: true
  201. }
  202. })
  203. ]
  204. )
  205. }