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.

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