Skip to content

Commit

Permalink
Closes #6855: Bundle and locally serve GraphiQL JS/CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
thatmattlove committed Aug 1, 2021
1 parent 80836c7 commit 08ed545
Show file tree
Hide file tree
Showing 10 changed files with 645 additions and 24 deletions.
36 changes: 33 additions & 3 deletions netbox/project-static/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,30 @@ const options = {
// Get CLI arguments for optional overrides.
const ARGS = process.argv.slice(2);

async function bundleGraphIQL() {
try {
const result = await esbuild.build({
...options,
entryPoints: {
graphiql: 'netbox-graphiql/index.ts',
},
target: 'es2016',
define: {
global: 'window',
},
});
if (result.errors.length === 0) {
console.log(`✅ Bundled source file 'netbox-graphiql/index.ts' to 'graphiql.js'`);
}
} catch (err) {
console.error(err);
}
}

/**
* Run script bundle jobs.
* Bundle Core NetBox JavaScript.
*/
async function bundleScripts() {
async function bundleNetBox() {
const entryPoints = {
netbox: 'src/index.ts',
jobs: 'src/jobs.ts',
Expand All @@ -26,7 +46,7 @@ async function bundleScripts() {
status: 'src/device/status.ts',
};
try {
let result = await esbuild.build({
const result = await esbuild.build({
...options,
entryPoints,
target: 'es2016',
Expand All @@ -42,6 +62,15 @@ async function bundleScripts() {
}
}

/**
* Run script bundle jobs.
*/
async function bundleScripts() {
for (const bundle of [bundleNetBox, bundleGraphIQL]) {
await bundle();
}
}

/**
* Run style bundle jobs.
*/
Expand All @@ -53,6 +82,7 @@ async function bundleStyles() {
'netbox-dark': 'styles/_dark.scss',
rack_elevation: 'styles/_rack_elevation.scss',
cable_trace: 'styles/_cable_trace.scss',
graphiql: 'netbox-graphiql/graphiql.scss',
};
const pluginOptions = { outputStyle: 'compressed' };
// Allow cache disabling.
Expand Down
1 change: 1 addition & 0 deletions netbox/project-static/dist/graphiql.css

Large diffs are not rendered by default.

304 changes: 304 additions & 0 deletions netbox/project-static/dist/graphiql.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions netbox/project-static/dist/graphiql.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions netbox/project-static/netbox-graphiql/graphiql.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Rather than use CDNs to include GraphiQL dependencies, import and bundle the dependencies so
// they can be locally served.
@import '../node_modules/graphiql/graphiql.css';
17 changes: 17 additions & 0 deletions netbox/project-static/netbox-graphiql/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Rather than use CDNs to include GraphiQL dependencies, import and bundle the dependencies so
* they can be locally served.
*/

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import 'graphql';
import GraphiQL from 'graphiql';
import SubscriptionsTransportWs from 'subscriptions-transport-ws';

window.React = React;
window.ReactDOM = ReactDOM;
// @ts-expect-error Assigning to window is required for graphene-django
window.SubscriptionsTransportWs = SubscriptionsTransportWs;
// @ts-expect-error Assigning to window is required for graphene-django
window.GraphiQL = GraphiQL;
16 changes: 16 additions & 0 deletions netbox/project-static/netbox-graphiql/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "netbox-graphiql",
"version": "0.1.0",
"description": "NetBox GraphiQL Custom Front End",
"main": "dist/graphiql.js",
"license": "Apache-2.0",
"private": true,
"dependencies": {
"graphiql": "1.4.1",
"graphql": ">= v14.5.0 <= 15.5.0",
"react": "17.0.2",
"react-dom": "17.0.2",
"subscriptions-transport-ws": "0.9.18",
"whatwg-fetch": "3.6.2"
}
}
4 changes: 4 additions & 0 deletions netbox/project-static/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"version": "0.1.0",
"main": "dist/netbox.js",
"license": "Apache-2.0",
"private": true,
"workspaces": [
"netbox-graphiql"
],
"scripts": {
"bundle": "node bundle.js",
"bundle:styles": "node bundle.js --styles",
Expand Down
Loading

0 comments on commit 08ed545

Please sign in to comment.