rollup-tests.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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) { // TODO: not really DRY
  47. if (isStylePath(id) && isRelPath(id) && importer.match('/tmp/tsc-output/')) {
  48. let resourcePath = importer.replace('/tmp/tsc-output/', '/')
  49. resourcePath = path.dirname(resourcePath)
  50. resourcePath = path.join(resourcePath, id)
  51. return { id: resourcePath, external: false }
  52. }
  53. return null
  54. }
  55. },
  56. alias({ // needs to go before node-resolve/commonjs so that alias resolution takes precedence
  57. // the alias to the non-premium tests. must be absolute
  58. 'standard-tests': path.join(process.cwd(), 'tmp/tsc-output/packages/__tests__'),
  59. 'premium-tests': path.join(process.cwd(), 'tmp/tsc-output/packages-premium/__tests__'),
  60. // despite using rollup/node for compilation, we want to bundle builds that runs in a real browser
  61. // also for HACK below
  62. 'xhr-mock': path.join(process.cwd(), './node_modules/xhr-mock/dist/xhr-mock.js'),
  63. 'luxon': path.join(process.cwd(), 'node_modules/luxon/build/cjs-browser/luxon.js'),
  64. // HACK
  65. // because the monorepo-tool doesn't support hoisting, it's likely we'll get multiple version of 3rd party packages.
  66. // explicitly map some references to top-level packages.
  67. 'moment/locale/es': path.join(process.cwd(), 'node_modules/moment/locale/es.js'), // needs to go before moment
  68. 'moment': path.join(process.cwd(), 'node_modules/moment/moment.js'),
  69. 'moment-timezone/builds/moment-timezone-with-data': path.join(process.cwd(), 'node_modules/moment-timezone/builds/moment-timezone-with-data.js'),
  70. }),
  71. nodeResolve({
  72. customResolveOptions: {
  73. paths: nodeModulesDirs
  74. }
  75. }),
  76. commonjs({
  77. // for fast-deep-equal import
  78. // also for react(-dom) hack, ALSO IN rollup-bundle.js
  79. include: 'node_modules/**',
  80. namedExports: {
  81. 'react': Object.keys(react),
  82. 'react-dom': Object.keys(reactDom)
  83. }
  84. }),
  85. replace({ // for react. also in rollup-tests.js
  86. values: {
  87. 'process.env.NODE_ENV': '"development"'
  88. }
  89. }),
  90. postCss({
  91. extract: true // to separate .css file
  92. }),
  93. sourcemaps()
  94. ],
  95. watch: WATCH_OPTIONS,
  96. onwarn
  97. }
  98. }