rollup-util.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const { readFileSync } = require('fs')
  2. const path = require('path')
  3. const glob = require('glob')
  4. const cleanup = require('rollup-plugin-cleanup')
  5. const sourcemaps = require('rollup-plugin-sourcemaps')
  6. const replace = require('rollup-plugin-replace')
  7. const handleBars = require('handlebars')
  8. const rootPkgJsonData = require('../../package.json')
  9. exports.EXTERNAL_BROWSER_GLOBALS = {
  10. fullcalendar: 'FullCalendar', // if this gets updated, update codebase...
  11. 'fullcalendar-scheduler': 'FullCalendar',
  12. luxon: 'luxon',
  13. rrule: 'rrule',
  14. moment: 'moment',
  15. 'moment-timezone/builds/moment-timezone-with-data': 'moment' // see moment-timezone/src/main.ts
  16. }
  17. exports.WATCH_OPTIONS = {
  18. chokidar: { // better than default watch util. doesn't fire change events on stat changes (like last opened)
  19. awaitWriteFinish: { // because tsc/rollup sometimes takes a long time to write and triggers two recompiles
  20. stabilityThreshold: 500,
  21. pollInterval: 100
  22. }
  23. },
  24. clearScreen: false // let tsc do the screan clearing
  25. }
  26. exports.SOURCEMAP_PLUGINS = [
  27. sourcemaps(),
  28. // HACK: there's a bug with sourcemap reading and watching: the first rebuild includes the intermediate
  29. // sourceMappingURL comments, confusing consumers of the generated sourcemap. Forcefully remove these comments.
  30. cleanup({ comments: 'none' })
  31. ]
  32. exports.TEMPLATE_PLUGIN = replace({
  33. delimiters: [ '<%= ', ' %>' ],
  34. values: {
  35. version: rootPkgJsonData.version,
  36. releaseDate: new Date().toISOString().replace(/T.*/, '')
  37. // ^TODO: store this in package.json for easier old-release recreation
  38. }
  39. })
  40. exports.renderBanner = handleBars.compile(
  41. readFileSync(
  42. path.join(__dirname, '../../packages/banner.tpl'),
  43. { encoding: 'utf8' }
  44. )
  45. )
  46. const REL_PATH_RE = /^[/.]/
  47. const SCSS_PATH_RE = /\.scss$/i
  48. const TILDE_PATH_RE = /^~/
  49. exports.onwarn = function(warning, warn) {
  50. // ignore circ dep warnings. too numerous and hard to fix right now
  51. if (warning.code !== 'CIRCULAR_DEPENDENCY') {
  52. warn(warning)
  53. }
  54. }
  55. exports.stripScssTildeImporter = function(path) { // for the rollup-scss plugin
  56. return { file: path.replace(TILDE_PATH_RE, '') }
  57. }
  58. exports.isRelPath = function(path) {
  59. return REL_PATH_RE.test(path)
  60. }
  61. exports.isScssPath = function(path) {
  62. return SCSS_PATH_RE.test(path)
  63. }
  64. exports.watchSubdirSassIncludes = { // a rollup plugin
  65. transform(code, id) {
  66. if (exports.isScssPath(id)) { // yuck
  67. let allStyleFiles = glob.sync(
  68. path.join(path.dirname(id), '**/*.{scss,sass,css}')
  69. )
  70. for (let styleFile of allStyleFiles) {
  71. this.addWatchFile(styleFile)
  72. }
  73. }
  74. return null
  75. }
  76. }