rollup.config.js 8.2 KB

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