bundle.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. const esbuild = require('esbuild');
  2. const { sassPlugin } = require('esbuild-sass-plugin');
  3. const util = require('util');
  4. const fs = require('fs');
  5. const copyFilePromise = util.promisify(fs.copyFile);
  6. // Bundler options common to all bundle jobs.
  7. const options = {
  8. outdir: './dist',
  9. bundle: true,
  10. minify: true,
  11. sourcemap: 'external',
  12. sourcesContent: false,
  13. logLevel: 'error',
  14. };
  15. // Get CLI arguments for optional overrides.
  16. const ARGS = process.argv.slice(2);
  17. function copyFiles(files) {
  18. return Promise.all(files.map(f => {
  19. return copyFilePromise(f.source, f.dest);
  20. }));
  21. }
  22. async function bundleGraphIQL() {
  23. let fileMap = [
  24. {
  25. source: './node_modules/react/umd/react.production.min.js',
  26. dest: './dist/graphiql/react.production.min.js'
  27. },
  28. {
  29. source: './node_modules/react-dom/umd/react-dom.production.min.js',
  30. dest: './dist/graphiql/react-dom.production.min.js'
  31. },
  32. {
  33. source: './node_modules/js-cookie/dist/js.cookie.min.js',
  34. dest: './dist/graphiql/js.cookie.min.js'
  35. },
  36. {
  37. source: './node_modules/graphiql/graphiql.min.js',
  38. dest: './dist/graphiql/graphiql.min.js'
  39. },
  40. {
  41. source: './node_modules/@graphiql/plugin-explorer/dist/index.umd.js',
  42. dest: './dist/graphiql/index.umd.js'
  43. },
  44. {
  45. source: './node_modules/graphiql/graphiql.min.css',
  46. dest: './dist/graphiql/graphiql.min.css'
  47. },
  48. {
  49. source: './node_modules/@graphiql/plugin-explorer/dist/style.css',
  50. dest: './dist/graphiql/plugin-explorer-style.css'
  51. }
  52. ];
  53. try {
  54. if (!fs.existsSync('./dist/graphiql/')) {
  55. fs.mkdirSync('./dist/graphiql/');
  56. }
  57. } catch (err) {
  58. console.error(err);
  59. }
  60. copyFiles(fileMap).then(() => {
  61. console.log('✅ Copied graphiql files');
  62. }).catch(err => {
  63. console.error(err);
  64. });
  65. }
  66. /**
  67. * Bundle Core NetBox JavaScript.
  68. */
  69. async function bundleNetBox() {
  70. const entryPoints = {
  71. netbox: 'src/index.ts',
  72. };
  73. try {
  74. const result = await esbuild.build({
  75. ...options,
  76. entryPoints,
  77. target: 'es2016',
  78. });
  79. if (result.errors.length === 0) {
  80. for (const [targetName, sourceName] of Object.entries(entryPoints)) {
  81. const source = sourceName.split('/')[1];
  82. console.log(`✅ Bundled source file '${source}' to '${targetName}.js'`);
  83. }
  84. }
  85. } catch (err) {
  86. console.error(err);
  87. }
  88. }
  89. /**
  90. * Run script bundle jobs.
  91. */
  92. async function bundleScripts() {
  93. for (const bundle of [bundleNetBox, bundleGraphIQL]) {
  94. await bundle();
  95. }
  96. }
  97. /**
  98. * Run style bundle jobs.
  99. */
  100. async function bundleStyles() {
  101. try {
  102. const entryPoints = {
  103. 'netbox-external': 'styles/external.scss',
  104. 'netbox': 'styles/netbox.scss',
  105. rack_elevation: 'styles/svg/rack_elevation.scss',
  106. cable_trace: 'styles/svg/cable_trace.scss',
  107. };
  108. const pluginOptions = { outputStyle: 'compressed' };
  109. // Allow cache disabling.
  110. if (ARGS.includes('--no-cache')) {
  111. pluginOptions.cache = false;
  112. }
  113. let result = await esbuild.build({
  114. ...options,
  115. // Disable sourcemaps for CSS/SCSS files, see #7068
  116. sourcemap: false,
  117. entryPoints,
  118. plugins: [sassPlugin(pluginOptions)],
  119. loader: {
  120. '.eot': 'file',
  121. '.woff': 'file',
  122. '.woff2': 'file',
  123. '.svg': 'file',
  124. '.ttf': 'file',
  125. },
  126. });
  127. if (result.errors.length === 0) {
  128. for (const [targetName, sourceName] of Object.entries(entryPoints)) {
  129. console.log(`✅ Bundled source file '${sourceName}' to '${targetName}.css'`);
  130. }
  131. }
  132. } catch (err) {
  133. console.error(err);
  134. }
  135. }
  136. /**
  137. * Run all bundle jobs.
  138. */
  139. async function bundleAll() {
  140. if (ARGS.includes('--styles')) {
  141. // Only run style jobs.
  142. return await bundleStyles();
  143. } else if (ARGS.includes('--scripts')) {
  144. // Only run script jobs.
  145. return await bundleScripts();
  146. }
  147. await bundleStyles();
  148. await bundleScripts();
  149. }
  150. bundleAll();