You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
7.5 KiB

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