rollup-tests.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const path = require('path')
  2. const glob = require('glob')
  3. const nodeResolve = require('@rollup/plugin-node-resolve')
  4. const alias = require('@rollup/plugin-alias')
  5. const commonjs = require('@rollup/plugin-commonjs')
  6. const sourcemaps = require('rollup-plugin-sourcemaps')
  7. const postCss = require('rollup-plugin-postcss')
  8. const { WATCH_OPTIONS, onwarn, isStylePath, isRelPath } = require('./rollup-util')
  9. const replace = require('@rollup/plugin-replace')
  10. const react = require('react')
  11. const reactDom = require('react-dom')
  12. module.exports = function() {
  13. let nonMainEntryPoints = glob.sync('tmp/tsc-output/packages?(-premium)/__tests__/src/*.js').filter((entryPoint) => (
  14. !path.basename(entryPoint).match(/^main\./)
  15. ))
  16. let configs = [
  17. buildConfig({
  18. input: 'tmp/tests-index.js',
  19. outputFile: 'tmp/tests-compiled/main.js'
  20. })
  21. ]
  22. for (let nonMainEntryPoint of nonMainEntryPoints) {
  23. configs.push(
  24. buildConfig({
  25. input: nonMainEntryPoint,
  26. outputFile: path.join('tmp/tests-compiled', path.basename(nonMainEntryPoint))
  27. })
  28. )
  29. }
  30. return configs
  31. }
  32. function buildConfig(options) {
  33. let nodeModulesDirs = [
  34. 'packages/__tests__/node_modules',
  35. 'packages-premium/__tests__/node_modules'
  36. ]
  37. return {
  38. input: options.input,
  39. output: {
  40. file: options.outputFile,
  41. format: 'iife',
  42. sourcemap: true
  43. },
  44. plugins: [
  45. {
  46. resolveId(id, importer) {
  47. // contrib files are not processed by tsc and not in tmp/
  48. // for vdom-react i think. yuck
  49. if (isRelPath(id)) {
  50. let m = id.match(/(packages-contrib\/.*)$/)
  51. if (m) {
  52. return { id: m[1].replace('/src/', '/dist/') + '.js' } // @fullcalendar/react has a dist dir
  53. }
  54. }
  55. if (isStylePath(id) && isRelPath(id) && importer.match('/tmp/tsc-output/')) {
  56. let resourcePath = importer.replace('/tmp/tsc-output/', '/')
  57. resourcePath = path.dirname(resourcePath)
  58. resourcePath = path.join(resourcePath, id)
  59. return { id: resourcePath, external: false }
  60. }
  61. return null
  62. }
  63. },
  64. alias({ // needs to go before node-resolve/commonjs so that alias resolution takes precedence
  65. entries: {
  66. // the alias to the non-premium tests. must be absolute
  67. 'standard-tests': path.join(process.cwd(), 'tmp/tsc-output/packages/__tests__'),
  68. 'premium-tests': path.join(process.cwd(), 'tmp/tsc-output/packages-premium/__tests__'),
  69. // despite using rollup/node for compilation, we want to bundle builds that runs in a real browser
  70. // also for HACK below
  71. 'xhr-mock': path.join(process.cwd(), './node_modules/xhr-mock/dist/xhr-mock.js'),
  72. 'luxon': path.join(process.cwd(), 'node_modules/luxon/build/cjs-browser/luxon.js'),
  73. // HACK
  74. // because the monorepo-tool doesn't support hoisting, it's likely we'll get multiple version of 3rd party packages.
  75. // explicitly map some references to top-level packages.
  76. 'moment/locale/es': path.join(process.cwd(), 'node_modules/moment/locale/es.js'), // needs to go before moment
  77. 'moment': path.join(process.cwd(), 'node_modules/moment/moment.js'),
  78. 'moment-timezone/builds/moment-timezone-with-data': path.join(process.cwd(), 'node_modules/moment-timezone/builds/moment-timezone-with-data.js'),
  79. // the default esm version of rrule has a weird dynamic import that rollup chokes on. use the umd version
  80. 'rrule': path.join(process.cwd(), 'node_modules/rrule/dist/es5/rrule.js')
  81. }
  82. }),
  83. nodeResolve({
  84. customResolveOptions: {
  85. paths: nodeModulesDirs
  86. }
  87. }),
  88. commonjs({
  89. // for react(-dom) hack, ALSO IN rollup-bundle.js
  90. namedExports: {
  91. 'react': Object.keys(react),
  92. 'react-dom': Object.keys(reactDom),
  93. 'rrule': [ 'RRule', 'rrulestr' ]
  94. }
  95. }),
  96. replace({ // for react. also in rollup-tests.js
  97. values: {
  98. 'process.env.NODE_ENV': '"production"'
  99. }
  100. }),
  101. postCss({
  102. extract: true // to separate .css file
  103. }),
  104. sourcemaps()
  105. ],
  106. watch: WATCH_OPTIONS,
  107. onwarn
  108. }
  109. }