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 =
  79. process.env.TYPES != null &&
  80. process.env.NODE_ENV === 'production' &&
  81. !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 =
  100. format === 'esm-bundler-runtime' ? `src/runtime.ts` : `src/index.ts`
  101. const external =
  102. isGlobalBuild || isRawESMBuild
  103. ? []
  104. : knownExternals.concat(Object.keys(pkg.dependencies || []))
  105. return {
  106. input: resolve(entryFile),
  107. // Global and Browser ESM builds inlines everything so that they can be
  108. // used alone.
  109. external,
  110. plugins: [
  111. json({
  112. namedExports: false
  113. }),
  114. tsPlugin,
  115. createReplacePlugin(
  116. isProductionBuild,
  117. isBundlerESMBuild,
  118. // isBrowserBuild?
  119. (isGlobalBuild || isRawESMBuild || isBundlerESMBuild) &&
  120. !packageOptions.enableNonBrowserBranches,
  121. isRuntimeCompileBuild,
  122. isNodeBuild
  123. ),
  124. ...plugins
  125. ],
  126. output,
  127. onwarn: (msg, warn) => {
  128. if (!/Circular/.test(msg)) {
  129. warn(msg)
  130. }
  131. }
  132. }
  133. }
  134. function createReplacePlugin(
  135. isProduction,
  136. isBundlerESMBuild,
  137. isBrowserBuild,
  138. isRuntimeCompileBuild,
  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. // 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. }