rollup-tests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. module.exports = function() {
  10. let nonMainEntryPoints = glob.sync('tmp/tsc-output/packages?(-premium)/__tests__/src/*.js').filter((entryPoint) => (
  11. !path.basename(entryPoint).match(/^main\./)
  12. ))
  13. let configs = [
  14. buildConfig({
  15. input: 'tmp/tests-index.js',
  16. outputFile: 'tmp/tests-compiled/main.js'
  17. })
  18. ]
  19. for (let nonMainEntryPoint of nonMainEntryPoints) {
  20. configs.push(
  21. buildConfig({
  22. input: nonMainEntryPoint,
  23. outputFile: path.join('tmp/tests-compiled', path.basename(nonMainEntryPoint))
  24. })
  25. )
  26. }
  27. return configs
  28. }
  29. function buildConfig(options) {
  30. let nodeModulesDirs = [
  31. 'packages/__tests__/node_modules',
  32. 'packages-premium/__tests__/node_modules'
  33. ]
  34. return {
  35. input: options.input,
  36. output: {
  37. file: options.outputFile,
  38. format: 'iife',
  39. sourcemap: true
  40. },
  41. plugins: [
  42. {
  43. resolveId(id, importer) { // TODO: not really DRY
  44. if (isStylePath(id) && isRelPath(id) && importer.match('/tmp/tsc-output/')) {
  45. let resourcePath = importer.replace('/tmp/tsc-output/', '/')
  46. resourcePath = path.dirname(resourcePath)
  47. resourcePath = path.join(resourcePath, id)
  48. return { id: resourcePath, external: false }
  49. }
  50. return null
  51. }
  52. },
  53. alias({ // needs to go before node-resolve/commonjs so that alias resolution takes precedence
  54. // the alias to the non-premium tests. must be absolute
  55. 'standard-tests': path.join(process.cwd(), 'tmp/tsc-output/packages/__tests__'),
  56. 'premium-tests': path.join(process.cwd(), 'tmp/tsc-output/packages-premium/__tests__'),
  57. // despite using rollup/node for compilation, we want to bundle builds that runs in a real browser
  58. // also for HACK below
  59. 'xhr-mock': path.join(process.cwd(), './node_modules/xhr-mock/dist/xhr-mock.js'),
  60. 'luxon': path.join(process.cwd(), 'node_modules/luxon/build/cjs-browser/luxon.js'),
  61. // HACK
  62. // because the monorepo-tool doesn't support hoisting, it's likely we'll get multiple version of 3rd party packages.
  63. // explicitly map some references to top-level packages.
  64. 'moment/locale/es': path.join(process.cwd(), 'node_modules/moment/locale/es.js'), // needs to go before moment
  65. 'moment': path.join(process.cwd(), 'node_modules/moment/moment.js'),
  66. 'moment-timezone/builds/moment-timezone-with-data': path.join(process.cwd(), 'node_modules/moment-timezone/builds/moment-timezone-with-data.js'),
  67. }),
  68. nodeResolve({
  69. customResolveOptions: {
  70. paths: nodeModulesDirs
  71. }
  72. }),
  73. commonjs(), // for fast-deep-equal import
  74. postCss({
  75. extract: true // to separate .css file
  76. }),
  77. sourcemaps()
  78. ],
  79. watch: WATCH_OPTIONS,
  80. onwarn
  81. }
  82. }