bundle.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. const esbuild = require('esbuild');
  2. const { sassPlugin } = require('esbuild-sass-plugin');
  3. // Bundler options common to all bundle jobs.
  4. const options = {
  5. outdir: './dist',
  6. bundle: true,
  7. minify: true,
  8. sourcemap: 'external',
  9. sourcesContent: false,
  10. logLevel: 'error',
  11. };
  12. // Get CLI arguments for optional overrides.
  13. const ARGS = process.argv.slice(2);
  14. async function bundleGraphIQL() {
  15. try {
  16. const result = await esbuild.build({
  17. ...options,
  18. entryPoints: {
  19. graphiql: 'netbox-graphiql/index.ts',
  20. },
  21. target: 'es2016',
  22. define: {
  23. global: 'window',
  24. },
  25. });
  26. if (result.errors.length === 0) {
  27. console.log(`✅ Bundled source file 'netbox-graphiql/index.ts' to 'graphiql.js'`);
  28. }
  29. } catch (err) {
  30. console.error(err);
  31. }
  32. }
  33. /**
  34. * Bundle Core NetBox JavaScript.
  35. */
  36. async function bundleNetBox() {
  37. const entryPoints = {
  38. netbox: 'src/index.ts',
  39. jobs: 'src/jobs.ts',
  40. lldp: 'src/device/lldp.ts',
  41. config: 'src/device/config.ts',
  42. status: 'src/device/status.ts',
  43. };
  44. try {
  45. const result = await esbuild.build({
  46. ...options,
  47. entryPoints,
  48. target: 'es2016',
  49. });
  50. if (result.errors.length === 0) {
  51. for (const [targetName, sourceName] of Object.entries(entryPoints)) {
  52. const source = sourceName.split('/')[1];
  53. console.log(`✅ Bundled source file '${source}' to '${targetName}.js'`);
  54. }
  55. }
  56. } catch (err) {
  57. console.error(err);
  58. }
  59. }
  60. /**
  61. * Run script bundle jobs.
  62. */
  63. async function bundleScripts() {
  64. for (const bundle of [bundleNetBox, bundleGraphIQL]) {
  65. await bundle();
  66. }
  67. }
  68. /**
  69. * Run style bundle jobs.
  70. */
  71. async function bundleStyles() {
  72. try {
  73. const entryPoints = {
  74. 'netbox-external': 'styles/_external.scss',
  75. 'netbox-light': 'styles/_light.scss',
  76. 'netbox-dark': 'styles/_dark.scss',
  77. 'netbox-print': 'styles/_print.scss',
  78. rack_elevation: 'styles/_rack_elevation.scss',
  79. cable_trace: 'styles/_cable_trace.scss',
  80. graphiql: 'netbox-graphiql/graphiql.scss',
  81. };
  82. const pluginOptions = { outputStyle: 'compressed' };
  83. // Allow cache disabling.
  84. if (ARGS.includes('--no-cache')) {
  85. pluginOptions.cache = false;
  86. }
  87. let result = await esbuild.build({
  88. ...options,
  89. // Disable sourcemaps for CSS/SCSS files, see #7068
  90. sourcemap: false,
  91. entryPoints,
  92. plugins: [sassPlugin(pluginOptions)],
  93. loader: {
  94. '.eot': 'file',
  95. '.woff': 'file',
  96. '.woff2': 'file',
  97. '.svg': 'file',
  98. '.ttf': 'file',
  99. },
  100. });
  101. if (result.errors.length === 0) {
  102. for (const [targetName, sourceName] of Object.entries(entryPoints)) {
  103. const source = sourceName.split('/')[1];
  104. console.log(`✅ Bundled source file '${source}' to '${targetName}.css'`);
  105. }
  106. }
  107. } catch (err) {
  108. console.error(err);
  109. }
  110. }
  111. /**
  112. * Run all bundle jobs.
  113. */
  114. async function bundleAll() {
  115. if (ARGS.includes('--styles')) {
  116. // Only run style jobs.
  117. return await bundleStyles();
  118. } else if (ARGS.includes('--scripts')) {
  119. // Only run script jobs.
  120. return await bundleScripts();
  121. }
  122. await bundleStyles();
  123. await bundleScripts();
  124. }
  125. bundleAll();