rollup.packages.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. const path = require('path')
  2. const nodeResolve = require('@rollup/plugin-node-resolve').default
  3. const alias = require('@rollup/plugin-alias')
  4. const dts = require('rollup-plugin-dts').default
  5. const sourceMapLoader = require('rollup-plugin-sourcemaps')
  6. const postcss = require('rollup-plugin-postcss')
  7. const { checkNoSymlinks, buildBanner } = require('./scripts/lib/new')
  8. const {
  9. externalizeStylesheets, externalizeNonRelative, injectReleaseDateAndVersion,
  10. buildAliasMap, removeStylesheetImports, removeEmptyImports
  11. } = require('./scripts/lib/new-rollup')
  12. /*
  13. needs tsc to run first
  14. but needs symlinks killed also
  15. compiles from TSC files
  16. */
  17. const { publicPackageStructs } = require('./scripts/lib/package-index')
  18. checkNoSymlinks(publicPackageStructs)
  19. let pkgsWithBrowserGlobal = []
  20. let browserGlobalByPkg = {}
  21. for (let struct of publicPackageStructs) {
  22. if (struct.meta.browserGlobal) {
  23. pkgsWithBrowserGlobal.push(struct)
  24. browserGlobalByPkg[struct.name] = struct.meta.browserGlobal
  25. }
  26. }
  27. browserGlobalByPkg['@fullcalendar/common'] = 'FullCalendar'
  28. const THIRD_PARTY_BROWSER_GLOBALS = {
  29. // preact: 'Preact', // we actually want this inlined
  30. rrule: 'rrule',
  31. moment: 'moment',
  32. 'moment-timezone': 'moment',
  33. luxon: 'luxon',
  34. 'ical.js': 'ICAL'
  35. }
  36. let allGlobals = { ...THIRD_PARTY_BROWSER_GLOBALS, ...browserGlobalByPkg }
  37. let externalList = Object.keys(allGlobals)
  38. let externalListNoCommon = externalList.filter((name) => name !== '@fullcalendar/common')
  39. let aliasMap = buildAliasMap(publicPackageStructs)
  40. module.exports = [
  41. // for JS (ESM)
  42. ...publicPackageStructs.map((struct) => {
  43. return {
  44. input: path.join(struct.dir, struct.mainTscJs),
  45. output: {
  46. format: 'es',
  47. file: path.join(struct.dir, struct.mainDistJs),
  48. sourcemap: true,
  49. banner: buildBanner(struct.isPremium)
  50. },
  51. plugins: [
  52. externalizeVDom('.js'),
  53. externalizeNonRelative(),
  54. sourceMapLoader(), // load from transpiled-via-tsc JS files
  55. postcss({ // will use postcss.config.js
  56. extract: true
  57. }),
  58. transplantCss(struct.mainName),
  59. injectReleaseDateAndVersion()
  60. ]
  61. }
  62. }),
  63. // for JS (CJS)
  64. ...publicPackageStructs.map((struct) => {
  65. return {
  66. input: path.join(struct.dir, struct.mainTscJs),
  67. output: [{
  68. format: 'cjs',
  69. exports: 'named',
  70. file: path.join(struct.dir, struct.mainDistJs.replace('.js', '.cjs.js')),
  71. banner: buildBanner(struct.isPremium)
  72. }],
  73. plugins: [
  74. externalizeVDom('.cjs'),
  75. externalizeNonRelative(),
  76. removeStylesheetImports(),
  77. injectReleaseDateAndVersion(),
  78. removeEmptyImports() // because of removeStylesheetImports and CJS
  79. ]
  80. }
  81. }),
  82. // vdom from @fullcalendar/core-preact
  83. {
  84. input: 'packages/core/tsc/vdom.js',
  85. output: [
  86. {
  87. format: 'cjs',
  88. exports: 'named',
  89. file: 'packages/core/vdom.cjs.js'
  90. },
  91. {
  92. format: 'esm',
  93. file: 'packages/core/vdom.js'
  94. }
  95. ],
  96. plugins: [
  97. externalizeNonRelative('@fullcalendar/core-preact'),
  98. nodeResolve()
  99. ]
  100. },
  101. {
  102. input: 'packages/common/tsc/vdom.js',
  103. output: [
  104. {
  105. format: 'cjs',
  106. exports: 'named',
  107. file: 'packages/common/vdom.cjs.js'
  108. },
  109. {
  110. format: 'esm',
  111. file: 'packages/common/vdom.js'
  112. }
  113. ],
  114. plugins: [
  115. externalizeNonRelative('@fullcalendar/core-preact'),
  116. nodeResolve()
  117. ]
  118. },
  119. // for global variable JS
  120. ...pkgsWithBrowserGlobal.map((struct) => {
  121. return {
  122. input: path.join(struct.dir, struct.mainGlobalTscJs),
  123. external: struct.name === '@fullcalendar/core' ? externalListNoCommon : externalList, // if core, inline common
  124. output: {
  125. format: 'iife',
  126. name: struct.meta.browserGlobal,
  127. exports: 'named',
  128. file: path.join(struct.dir, struct.mainDistJs.replace('.js', '.global.js')),
  129. banner: buildBanner(struct.isPremium),
  130. globals: allGlobals
  131. },
  132. plugins: [ // same plugins that rollup.bundle.js uses
  133. removeStylesheetImports(),
  134. alias({
  135. entries: aliasMap // TODO: for packages like @fullcalendar/common which will be inlined
  136. }),
  137. nodeResolve(), // for tslib
  138. injectReleaseDateAndVersion()
  139. ]
  140. }
  141. }),
  142. // for DTS
  143. ...publicPackageStructs.map((struct) => {
  144. return {
  145. input: path.join(struct.dir, struct.mainTscDts),
  146. output: {
  147. format: 'es',
  148. file: path.join(struct.dir, struct.mainDistDts),
  149. },
  150. plugins: [
  151. fixDtsCodeIn(),
  152. ensurePremiumCommonAmbient(),
  153. externalizeVDom(''),
  154. externalizeStylesheets(),
  155. externalizeNonRelative(),
  156. dts(),
  157. fixDtsCodeOut()
  158. ]
  159. }
  160. })
  161. ]
  162. function transplantCss(fileName) { // fileName w/o extension
  163. let hasCss = false
  164. return {
  165. resolveId(id, importer) {
  166. if (/^\./.test(id) && /\.css$/.test(id)) { // relative stylesheet
  167. hasCss = true
  168. let filePath = path.join(importer, '..', id)
  169. filePath = filePath.replace('/tsc/', '/src/') // not very robust!!!
  170. return filePath
  171. }
  172. },
  173. intro() {
  174. if (hasCss) {
  175. return `import './${fileName}.css';`
  176. } else {
  177. return ''
  178. }
  179. }
  180. }
  181. }
  182. function externalizeVDom(addExtension) {
  183. return {
  184. resolveId(id, importer) {
  185. if (/\/vdom$/.test(id) || id.match(/^(preact|react|react-dom)$/)) {
  186. if (
  187. importer.match('packages/common') ||
  188. importer.match('packages/core')
  189. ) {
  190. return { id: './vdom' + (addExtension || ''), external: true, moduleSideEffects: true }
  191. } else {
  192. return { id: '@fullcalendar/common', external: true, moduleSideEffects: true }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. function ensurePremiumCommonAmbient() {
  199. return {
  200. resolveId(id) {
  201. if (id === '@fullcalendar/premium-common') {
  202. return { id, external: true, moduleSideEffects: true }
  203. }
  204. }
  205. }
  206. }
  207. function fixDtsCodeIn() {
  208. return {
  209. /*
  210. fix problem with tsc outputting random imports like this:
  211. import("../../../packages/common/src/plugin-system-struct")
  212. import("../../../packages/common/src/main")
  213. relevant tickets:
  214. https://github.com/microsoft/TypeScript/issues/38111 - "Declaration emit reveals paths within dependency that were not referred to in the source file"
  215. */
  216. resolveId(id, importer) {
  217. if (importer && id.match('packages/common')) {
  218. return { id: '@fullcalendar/common', external: true }
  219. }
  220. }
  221. }
  222. }
  223. function fixDtsCodeOut() {
  224. return {
  225. renderChunk(code) {
  226. // remove sourcemap comments and ///<reference>
  227. code = code.replace(/\/\/.*/g, '')
  228. /*
  229. tsc, for classes that have superclasses with getter methods, sometimes reference the return type like this:
  230. import("@fullcalendar/common/tsc/whatever").Something
  231. (and this is from WITHIN the @fullcalendar/common package)
  232. possible BUG with this hack: playing weird with TS triple-slash references
  233. relevant tickets:
  234. https://github.com/microsoft/TypeScript/issues/38111 - "Declaration emit reveals paths within dependency that were not referred to in the source file"
  235. */
  236. code = code.replace(/(['"]@fullcalendar\/[^'"]+)\/[^'"]+(['"])/g, function(m0, m1, m2) {
  237. return m1 + m2
  238. })
  239. /*
  240. sometimes tsc fully resolved generic vdom declarations to preact, which makes the VNode<any> a thing (which it shouldn't be)
  241. relevant tickets:
  242. https://github.com/microsoft/TypeScript/issues/37151 - "Declaration emit should not inline type definitions"
  243. */
  244. code = code.replace(/VNode<any>/g, 'VNode')
  245. if (/\b(p?react)\b/.test(code)) {
  246. throw new Error('BAD reference to preact/react in DTS')
  247. }
  248. return code
  249. }
  250. }
  251. }