rollup.config.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 = packageOptions.isRuntimeCompileBuild
  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. const nodePlugins = packageOptions.enableNonBrowserBranches
  103. ? [
  104. require('@rollup/plugin-node-resolve')(),
  105. require('@rollup/plugin-commonjs')()
  106. ]
  107. : []
  108. return {
  109. input: resolve(entryFile),
  110. // Global and Browser ESM builds inlines everything so that they can be
  111. // used alone.
  112. external,
  113. plugins: [
  114. json({
  115. namedExports: false
  116. }),
  117. tsPlugin,
  118. createReplacePlugin(
  119. isProductionBuild,
  120. isBundlerESMBuild,
  121. // isBrowserBuild?
  122. (isGlobalBuild || isRawESMBuild || isBundlerESMBuild) &&
  123. !packageOptions.enableNonBrowserBranches,
  124. isRuntimeCompileBuild,
  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. isRuntimeCompileBuild,
  144. isGlobalBuild,
  145. isNodeBuild
  146. ) {
  147. const replacements = {
  148. __COMMIT__: `"${process.env.COMMIT}"`,
  149. __VERSION__: `"${masterVersion}"`,
  150. __DEV__: isBundlerESMBuild
  151. ? // preserve to be handled by bundlers
  152. `(process.env.NODE_ENV !== 'production')`
  153. : // hard coded dev/prod builds
  154. !isProduction,
  155. // this is only used during tests
  156. __TEST__: isBundlerESMBuild ? `(process.env.NODE_ENV === 'test')` : false,
  157. // If the build is expected to run directly in the browser (global / esm builds)
  158. __BROWSER__: isBrowserBuild,
  159. // is targeting bundlers?
  160. __BUNDLER__: isBundlerESMBuild,
  161. // support compile in browser?
  162. __RUNTIME_COMPILE__: isRuntimeCompileBuild,
  163. __GLOBAL__: isGlobalBuild,
  164. // is targeting Node (SSR)?
  165. __NODE_JS__: isNodeBuild,
  166. // support options?
  167. // the lean build drops options related code with buildOptions.lean: true
  168. __FEATURE_OPTIONS__: !packageOptions.lean && !process.env.LEAN,
  169. __FEATURE_SUSPENSE__: true,
  170. ...(isProduction && isBrowserBuild
  171. ? {
  172. 'context.onError(': `/*#__PURE__*/ context.onError(`,
  173. 'emitError(': `/*#__PURE__*/ emitError(`,
  174. 'createCompilerError(': `/*#__PURE__*/ createCompilerError(`,
  175. 'createDOMCompilerError(': `/*#__PURE__*/ createDOMCompilerError(`
  176. }
  177. : {})
  178. }
  179. // allow inline overrides like
  180. //__RUNTIME_COMPILE__=true yarn build runtime-core
  181. Object.keys(replacements).forEach(key => {
  182. if (key in process.env) {
  183. replacements[key] = process.env[key]
  184. }
  185. })
  186. return replace(replacements)
  187. }
  188. function createProductionConfig(format) {
  189. return createConfig(format, {
  190. file: resolve(`dist/${name}.${format}.prod.js`),
  191. format: outputConfigs[format].format
  192. })
  193. }
  194. function createMinifiedConfig(format) {
  195. const { terser } = require('rollup-plugin-terser')
  196. return createConfig(
  197. format,
  198. {
  199. file: resolve(`dist/${name}.${format}.prod.js`),
  200. format: outputConfigs[format].format
  201. },
  202. [
  203. terser({
  204. module: /^esm/.test(format),
  205. compress: {
  206. ecma: 2015,
  207. pure_getters: true
  208. }
  209. })
  210. ]
  211. )
  212. }