rollup.config.js 8.5 KB

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