scripts.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import config from '../config';
  2. import path from 'path';
  3. import { rollup } from 'rollup';
  4. import commonjs from '@rollup/plugin-commonjs';
  5. import resolve from '@rollup/plugin-node-resolve';
  6. import json from '@rollup/plugin-json';
  7. import babel from '@rollup/plugin-babel';
  8. import { terser } from 'rollup-plugin-terser';
  9. import browsersync from 'browser-sync';
  10. const target = config.currentTarget;
  11. const mode = config.currentMode;
  12. /**
  13. * d3 has circurlar dependencies, declared as "won't fix"
  14. * because these are, according to mbostock, compliant with es6 specification:
  15. * https://github.com/d3/d3-selection/issues/168
  16. * This function silences the corresponding rollup console error/warning output
  17. */
  18. const printWarning = (message) => {
  19. if (message.code === 'CIRCULAR_DEPENDENCY') {
  20. return;
  21. }
  22. console.error(message);
  23. };
  24. /**
  25. * Generates rollup build configurations
  26. * based on target (development / production) and mode (online / offline)
  27. *
  28. * @param cTarget: Build target
  29. * @param cMode: Build mode
  30. * @return: Rollup configuration object
  31. */
  32. const rollupConfig = (cTarget, cMode) => {
  33. const targetInfix = `${config.targets[cTarget].delimiter}${config.targets[cTarget].infix}`;
  34. const modeInfix = `${config.modes[cMode].delimiter}${config.modes[cMode].infix}`;
  35. let rconf = {
  36. source: {
  37. onwarn: printWarning,
  38. input: path.join(
  39. config.paths.src,
  40. config.paths.js,
  41. `${config.fileNames.jsx}${modeInfix}.jsx`
  42. ),
  43. plugins: [
  44. json(),
  45. resolve(),
  46. commonjs(),
  47. babel({ babelHelpers: 'bundled' })
  48. ]
  49. },
  50. destination: {
  51. output: {
  52. file: path.join(
  53. config.paths.dest,
  54. config.paths.assets,
  55. config.paths.js,
  56. `${config.fileNames.js}${modeInfix}${targetInfix}.js`
  57. ),
  58. format: 'iife'
  59. },
  60. sourceMap: cTarget === 'development' ? 'inline' : false
  61. }
  62. };
  63. if (cTarget === 'production') {
  64. rconf.source.plugins.push(terser());
  65. }
  66. return rconf;
  67. };
  68. /**
  69. * Transpile javascript with configuration specific for target and mode
  70. * based on configuration
  71. *
  72. * @param cb: callback handler passed by gulp, signals task completion when called
  73. */
  74. const scripts = (cb) => {
  75. let rConf = rollupConfig(target, mode);
  76. rollup(rConf.source)
  77. .then((bundle) => {
  78. bundle.write(rConf.destination);
  79. }).then(() => {
  80. process.stdout.write(`[${rConf.destination.output.file}]\n`);
  81. }).then(() => {
  82. cb();
  83. if (target === 'development') {
  84. browsersync.reload();
  85. }
  86. });
  87. };
  88. export {
  89. scripts
  90. };