bundle.js 3.0 KB

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