bundle.js 3.9 KB

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