bundle.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. 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. 'netbox-print': 'styles/_print.scss',
  77. rack_elevation: 'styles/_rack_elevation.scss',
  78. cable_trace: 'styles/_cable_trace.scss',
  79. graphiql: 'netbox-graphiql/graphiql.scss',
  80. };
  81. const pluginOptions = { outputStyle: 'compressed' };
  82. // Allow cache disabling.
  83. if (ARGS.includes('--no-cache')) {
  84. pluginOptions.cache = false;
  85. }
  86. let result = await esbuild.build({
  87. ...options,
  88. // Disable sourcemaps for CSS/SCSS files, see #7068
  89. sourcemap: false,
  90. entryPoints,
  91. plugins: [sassPlugin(pluginOptions)],
  92. loader: {
  93. '.eot': 'file',
  94. '.woff': 'file',
  95. '.woff2': 'file',
  96. '.svg': 'file',
  97. '.ttf': 'file',
  98. },
  99. });
  100. if (result.errors.length === 0) {
  101. for (const [targetName, sourceName] of Object.entries(entryPoints)) {
  102. const source = sourceName.split('/')[1];
  103. console.log(`✅ Bundled source file '${source}' to '${targetName}.css'`);
  104. }
  105. }
  106. } catch (err) {
  107. console.error(err);
  108. }
  109. }
  110. /**
  111. * Run all bundle jobs.
  112. */
  113. async function bundleAll() {
  114. if (ARGS.includes('--styles')) {
  115. // Only run style jobs.
  116. return await bundleStyles();
  117. } else if (ARGS.includes('--scripts')) {
  118. // Only run script jobs.
  119. return await bundleScripts();
  120. }
  121. await bundleStyles();
  122. await bundleScripts();
  123. }
  124. bundleAll();