rollup.config.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. let entryFile = /runtime$/.test(format) ? `src/runtime.ts` : `src/index.ts`
  105. // the compat build needs both default AND named exports. This will cause
  106. // Rollup to complain for non-ESM targets, so we use separate entries for
  107. // esm vs. non-esm builds.
  108. if (isCompatBuild && (isBrowserESMBuild || isBundlerESMBuild)) {
  109. entryFile = /runtime$/.test(format)
  110. ? `src/esm-runtime.ts`
  111. : `src/esm-index.ts`
  112. }
  113. let external = []
  114. if (isGlobalBuild || isBrowserESMBuild || isCompatBuild) {
  115. if (!packageOptions.enableNonBrowserBranches) {
  116. // normal browser builds - non-browser only imports are tree-shaken,
  117. // they are only listed here to suppress warnings.
  118. external = ['source-map', '@babel/parser', 'estree-walker']
  119. }
  120. } else {
  121. // Node / esm-bundler builds.
  122. // externalize all deps unless it's the compat build.
  123. external = [
  124. ...Object.keys(pkg.dependencies || {}),
  125. ...Object.keys(pkg.peerDependencies || {}),
  126. ...['path', 'url', 'stream'] // for @vue/compiler-sfc / server-renderer
  127. ]
  128. }
  129. // the browser builds of @vue/compiler-sfc requires postcss to be available
  130. // as a global (e.g. http://wzrd.in/standalone/postcss)
  131. output.globals = {
  132. postcss: 'postcss'
  133. }
  134. const nodePlugins =
  135. packageOptions.enableNonBrowserBranches && format !== 'cjs'
  136. ? [
  137. // @ts-ignore
  138. require('@rollup/plugin-commonjs')({
  139. sourceMap: false
  140. }),
  141. // @ts-ignore
  142. require('rollup-plugin-polyfill-node')(),
  143. require('@rollup/plugin-node-resolve').nodeResolve()
  144. ]
  145. : []
  146. return {
  147. input: resolve(entryFile),
  148. // Global and Browser ESM builds inlines everything so that they can be
  149. // used alone.
  150. external,
  151. plugins: [
  152. json({
  153. namedExports: false
  154. }),
  155. tsPlugin,
  156. createReplacePlugin(
  157. isProductionBuild,
  158. isBundlerESMBuild,
  159. isBrowserESMBuild,
  160. // isBrowserBuild?
  161. (isGlobalBuild || isBrowserESMBuild || isBundlerESMBuild) &&
  162. !packageOptions.enableNonBrowserBranches,
  163. isGlobalBuild,
  164. isNodeBuild,
  165. isCompatBuild
  166. ),
  167. ...nodePlugins,
  168. ...plugins
  169. ],
  170. output,
  171. onwarn: (msg, warn) => {
  172. if (!/Circular/.test(msg)) {
  173. warn(msg)
  174. }
  175. },
  176. treeshake: {
  177. moduleSideEffects: false
  178. }
  179. }
  180. }
  181. function createReplacePlugin(
  182. isProduction,
  183. isBundlerESMBuild,
  184. isBrowserESMBuild,
  185. isBrowserBuild,
  186. isGlobalBuild,
  187. isNodeBuild,
  188. isCompatBuild
  189. ) {
  190. const replacements = {
  191. __COMMIT__: `"${process.env.COMMIT}"`,
  192. __VERSION__: `"${masterVersion}"`,
  193. __DEV__: isBundlerESMBuild
  194. ? // preserve to be handled by bundlers
  195. `(process.env.NODE_ENV !== 'production')`
  196. : // hard coded dev/prod builds
  197. !isProduction,
  198. // this is only used during Vue's internal tests
  199. __TEST__: false,
  200. // If the build is expected to run directly in the browser (global / esm builds)
  201. __BROWSER__: isBrowserBuild,
  202. __GLOBAL__: isGlobalBuild,
  203. __ESM_BUNDLER__: isBundlerESMBuild,
  204. __ESM_BROWSER__: isBrowserESMBuild,
  205. // is targeting Node (SSR)?
  206. __NODE_JS__: isNodeBuild,
  207. // 2.x compat build
  208. __COMPAT__: isCompatBuild,
  209. // feature flags
  210. __FEATURE_SUSPENSE__: true,
  211. __FEATURE_OPTIONS_API__: isBundlerESMBuild ? `__VUE_OPTIONS_API__` : true,
  212. __FEATURE_PROD_DEVTOOLS__: isBundlerESMBuild
  213. ? `__VUE_PROD_DEVTOOLS__`
  214. : false,
  215. ...(isProduction && isBrowserBuild
  216. ? {
  217. 'context.onError(': `/*#__PURE__*/ context.onError(`,
  218. 'emitError(': `/*#__PURE__*/ emitError(`,
  219. 'createCompilerError(': `/*#__PURE__*/ createCompilerError(`,
  220. 'createDOMCompilerError(': `/*#__PURE__*/ createDOMCompilerError(`
  221. }
  222. : {})
  223. }
  224. // allow inline overrides like
  225. //__RUNTIME_COMPILE__=true yarn build runtime-core
  226. Object.keys(replacements).forEach(key => {
  227. if (key in process.env) {
  228. replacements[key] = process.env[key]
  229. }
  230. })
  231. return replace({
  232. // @ts-ignore
  233. values: replacements,
  234. preventAssignment: true
  235. })
  236. }
  237. function createProductionConfig(format) {
  238. return createConfig(format, {
  239. file: resolve(`dist/${name}.${format}.prod.js`),
  240. format: outputConfigs[format].format
  241. })
  242. }
  243. function createMinifiedConfig(format) {
  244. const { terser } = require('rollup-plugin-terser')
  245. return createConfig(
  246. format,
  247. {
  248. file: outputConfigs[format].file.replace(/\.js$/, '.prod.js'),
  249. format: outputConfigs[format].format
  250. },
  251. [
  252. terser({
  253. module: /^esm/.test(format),
  254. compress: {
  255. ecma: 2015,
  256. pure_getters: true
  257. },
  258. safari10: true
  259. })
  260. ]
  261. )
  262. }