rollup.config.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // @ts-check
  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 resolve = p => path.resolve(packageDir, p)
  13. const pkg = require(resolve(`package.json`))
  14. const packageOptions = pkg.buildOptions || {}
  15. const name = packageOptions.filename || path.basename(packageDir)
  16. // ensure TS checks only once for each build
  17. let hasTSChecked = false
  18. const outputConfigs = {
  19. 'esm-bundler': {
  20. file: resolve(`dist/${name}.esm-bundler.js`),
  21. format: `es`
  22. },
  23. 'esm-browser': {
  24. file: resolve(`dist/${name}.esm-browser.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. // runtime-only builds, for main "vue" package only
  36. 'esm-bundler-runtime': {
  37. file: resolve(`dist/${name}.runtime.esm-bundler.js`),
  38. format: `es`
  39. },
  40. 'esm-browser-runtime': {
  41. file: resolve(`dist/${name}.runtime.esm-browser.js`),
  42. format: 'es'
  43. },
  44. 'global-runtime': {
  45. file: resolve(`dist/${name}.runtime.global.js`),
  46. format: 'iife'
  47. }
  48. }
  49. const defaultFormats = ['esm-bundler', 'cjs']
  50. const inlineFormats = process.env.FORMATS && process.env.FORMATS.split(',')
  51. const packageFormats = inlineFormats || packageOptions.formats || defaultFormats
  52. const packageConfigs = process.env.PROD_ONLY
  53. ? []
  54. : packageFormats.map(format => createConfig(format, outputConfigs[format]))
  55. if (process.env.NODE_ENV === 'production') {
  56. packageFormats.forEach(format => {
  57. if (packageOptions.prod === false) {
  58. return
  59. }
  60. if (format === 'cjs') {
  61. packageConfigs.push(createProductionConfig(format))
  62. }
  63. if (/^(global|esm-browser)(-runtime)?/.test(format)) {
  64. packageConfigs.push(createMinifiedConfig(format))
  65. }
  66. })
  67. }
  68. export default packageConfigs
  69. function createConfig(format, output, plugins = []) {
  70. if (!output) {
  71. console.log(require('chalk').yellow(`invalid format: "${format}"`))
  72. process.exit(1)
  73. }
  74. output.sourcemap = !!process.env.SOURCE_MAP
  75. output.externalLiveBindings = false
  76. const isProductionBuild =
  77. process.env.__DEV__ === 'false' || /\.prod\.js$/.test(output.file)
  78. const isBundlerESMBuild = /esm-bundler/.test(format)
  79. const isBrowserESMBuild = /esm-browser/.test(format)
  80. const isNodeBuild = format === 'cjs'
  81. const isGlobalBuild = /global/.test(format)
  82. const isCompatBuild = !!packageOptions.compat
  83. if (isGlobalBuild) {
  84. output.name = packageOptions.name
  85. }
  86. const shouldEmitDeclarations = process.env.TYPES != null && !hasTSChecked
  87. const tsPlugin = ts({
  88. check: process.env.NODE_ENV === 'production' && !hasTSChecked,
  89. tsconfig: path.resolve(__dirname, 'tsconfig.json'),
  90. cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
  91. tsconfigOverride: {
  92. compilerOptions: {
  93. sourceMap: output.sourcemap,
  94. declaration: shouldEmitDeclarations,
  95. declarationMap: shouldEmitDeclarations
  96. },
  97. exclude: ['**/__tests__', 'test-dts']
  98. }
  99. })
  100. // we only need to check TS and generate declarations once for each build.
  101. // it also seems to run into weird issues when checking multiple times
  102. // during a single build.
  103. hasTSChecked = true
  104. const entryFile = /runtime$/.test(format) ? `src/runtime.ts` : `src/index.ts`
  105. let external = []
  106. if (isGlobalBuild || isBrowserESMBuild || isCompatBuild) {
  107. if (!packageOptions.enableNonBrowserBranches) {
  108. // normal browser builds - non-browser only imports are tree-shaken,
  109. // they are only listed here to suppress warnings.
  110. external = ['source-map', '@babel/parser', 'estree-walker']
  111. }
  112. } else {
  113. // Node / esm-bundler builds.
  114. // externalize all deps unless it's the compat build.
  115. external = [
  116. ...Object.keys(pkg.dependencies || {}),
  117. ...Object.keys(pkg.peerDependencies || {}),
  118. ...['path', 'url', 'stream'] // for @vue/compiler-sfc / server-renderer
  119. ]
  120. }
  121. // the browser builds of @vue/compiler-sfc requires postcss to be available
  122. // as a global (e.g. http://wzrd.in/standalone/postcss)
  123. output.globals = {
  124. postcss: 'postcss'
  125. }
  126. const nodePlugins =
  127. packageOptions.enableNonBrowserBranches && format !== 'cjs'
  128. ? [
  129. // @ts-ignore
  130. require('@rollup/plugin-commonjs')({
  131. sourceMap: false
  132. }),
  133. // @ts-ignore
  134. require('rollup-plugin-polyfill-node')(),
  135. require('@rollup/plugin-node-resolve').nodeResolve()
  136. ]
  137. : []
  138. return {
  139. input: resolve(entryFile),
  140. // Global and Browser ESM builds inlines everything so that they can be
  141. // used alone.
  142. external,
  143. plugins: [
  144. json({
  145. namedExports: false
  146. }),
  147. tsPlugin,
  148. createReplacePlugin(
  149. isProductionBuild,
  150. isBundlerESMBuild,
  151. isBrowserESMBuild,
  152. // isBrowserBuild?
  153. (isGlobalBuild || isBrowserESMBuild || isBundlerESMBuild) &&
  154. !packageOptions.enableNonBrowserBranches,
  155. isGlobalBuild,
  156. isNodeBuild,
  157. isCompatBuild
  158. ),
  159. ...nodePlugins,
  160. ...plugins
  161. ],
  162. output,
  163. onwarn: (msg, warn) => {
  164. if (!/Circular/.test(msg)) {
  165. warn(msg)
  166. }
  167. },
  168. treeshake: {
  169. moduleSideEffects: false
  170. }
  171. }
  172. }
  173. function createReplacePlugin(
  174. isProduction,
  175. isBundlerESMBuild,
  176. isBrowserESMBuild,
  177. isBrowserBuild,
  178. isGlobalBuild,
  179. isNodeBuild,
  180. isCompatBuild
  181. ) {
  182. const replacements = {
  183. __COMMIT__: `"${process.env.COMMIT}"`,
  184. __VERSION__: `"${masterVersion}"`,
  185. __DEV__: isBundlerESMBuild
  186. ? // preserve to be handled by bundlers
  187. `(process.env.NODE_ENV !== 'production')`
  188. : // hard coded dev/prod builds
  189. !isProduction,
  190. // this is only used during Vue's internal tests
  191. __TEST__: false,
  192. // If the build is expected to run directly in the browser (global / esm builds)
  193. __BROWSER__: isBrowserBuild,
  194. __GLOBAL__: isGlobalBuild,
  195. __ESM_BUNDLER__: isBundlerESMBuild,
  196. __ESM_BROWSER__: isBrowserESMBuild,
  197. // is targeting Node (SSR)?
  198. __NODE_JS__: isNodeBuild,
  199. // 2.x compat build
  200. __COMPAT__: isCompatBuild,
  201. // feature flags
  202. __FEATURE_SUSPENSE__: true,
  203. __FEATURE_OPTIONS_API__: isBundlerESMBuild ? `__VUE_OPTIONS_API__` : true,
  204. __FEATURE_PROD_DEVTOOLS__: isBundlerESMBuild
  205. ? `__VUE_PROD_DEVTOOLS__`
  206. : false,
  207. ...(isProduction && isBrowserBuild
  208. ? {
  209. 'context.onError(': `/*#__PURE__*/ context.onError(`,
  210. 'emitError(': `/*#__PURE__*/ emitError(`,
  211. 'createCompilerError(': `/*#__PURE__*/ createCompilerError(`,
  212. 'createDOMCompilerError(': `/*#__PURE__*/ createDOMCompilerError(`
  213. }
  214. : {})
  215. }
  216. // allow inline overrides like
  217. //__RUNTIME_COMPILE__=true yarn build runtime-core
  218. Object.keys(replacements).forEach(key => {
  219. if (key in process.env) {
  220. replacements[key] = process.env[key]
  221. }
  222. })
  223. return replace({
  224. // @ts-ignore
  225. values: replacements,
  226. preventAssignment: true
  227. })
  228. }
  229. function createProductionConfig(format) {
  230. return createConfig(format, {
  231. file: resolve(`dist/${name}.${format}.prod.js`),
  232. format: outputConfigs[format].format
  233. })
  234. }
  235. function createMinifiedConfig(format) {
  236. const { terser } = require('rollup-plugin-terser')
  237. return createConfig(
  238. format,
  239. {
  240. file: outputConfigs[format].file.replace(/\.js$/, '.prod.js'),
  241. format: outputConfigs[format].format
  242. },
  243. [
  244. terser({
  245. module: /^esm/.test(format),
  246. compress: {
  247. ecma: 2015,
  248. pure_getters: true
  249. },
  250. safari10: true
  251. })
  252. ]
  253. )
  254. }