From 718acec2abbdff8ce525b629de9eda3a910c6f05 Mon Sep 17 00:00:00 2001
From: Jerel Miller <jmiller@newrelic.com>
Date: Fri, 12 Jun 2020 00:33:35 -0700
Subject: [PATCH] feat: Add working code to load the sdk bundle

---
 gatsby-config.js                             |  8 ++
 package-lock.json                            | 14 +++-
 package.json                                 |  1 +
 plugins/gatsby-source-nr1-sdk/gatsby-node.js | 78 ++++++++++++++++++++
 src/utils/sdk.js                             |  2 +-
 5 files changed, 99 insertions(+), 4 deletions(-)
 create mode 100644 plugins/gatsby-source-nr1-sdk/gatsby-node.js

diff --git a/gatsby-config.js b/gatsby-config.js
index 55f49cea0..8f87f3954 100644
--- a/gatsby-config.js
+++ b/gatsby-config.js
@@ -1,3 +1,5 @@
+// const { RELEASE } = require('./src/utils/sdk');
+
 module.exports = {
   siteMetadata: {
     title: 'New Relic Developers',
@@ -70,5 +72,11 @@ module.exports = {
       },
     },
     'gatsby-plugin-sitemap',
+    {
+      resolve: 'gatsby-source-nr1-sdk',
+      options: {
+        release: 'release-1093',
+      },
+    },
   ],
 };
diff --git a/package-lock.json b/package-lock.json
index e88315cb4..9a5b91c98 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -6690,6 +6690,13 @@
       "requires": {
         "node-fetch": "2.1.2",
         "whatwg-fetch": "2.0.4"
+      },
+      "dependencies": {
+        "node-fetch": {
+          "version": "2.1.2",
+          "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.1.2.tgz",
+          "integrity": "sha1-q4hOjn5X44qUR1POxwb3iNF2i7U="
+        }
       }
     },
     "cross-spawn": {
@@ -19174,9 +19181,10 @@
       "integrity": "sha1-n7CwmbzSoCGUDmA8ZCVNwAPZp6g="
     },
     "node-fetch": {
-      "version": "2.1.2",
-      "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.1.2.tgz",
-      "integrity": "sha1-q4hOjn5X44qUR1POxwb3iNF2i7U="
+      "version": "2.6.0",
+      "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz",
+      "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==",
+      "dev": true
     },
     "node-forge": {
       "version": "0.9.0",
diff --git a/package.json b/package.json
index 968c3c9f8..820328971 100644
--- a/package.json
+++ b/package.json
@@ -42,6 +42,7 @@
     "husky": "^4.2.5",
     "identity-obj-proxy": "^3.0.0",
     "jest": "^26.0.1",
+    "node-fetch": "^2.6.0",
     "prettier": "2.0.4",
     "react-test-renderer": "^16.13.1"
   },
diff --git a/plugins/gatsby-source-nr1-sdk/gatsby-node.js b/plugins/gatsby-source-nr1-sdk/gatsby-node.js
new file mode 100644
index 000000000..78d10f562
--- /dev/null
+++ b/plugins/gatsby-source-nr1-sdk/gatsby-node.js
@@ -0,0 +1,78 @@
+const fetch = require('node-fetch');
+const vm = require('vm');
+
+const noop = () => {};
+
+const BASE_URL =
+  'https://hypertext-sandbox.nr-assets.net/wanda--wanda-ec-ui--nr1-docs';
+
+const getBundle = async (src) => {
+  const res = await fetch(src);
+  return res.text();
+};
+
+exports.sourceNodes = async ({ actions }, { release }) => {
+  // const { createNode } = actions;
+  global.Element = {
+    prototype: {
+      setAttribute: noop,
+    },
+  };
+  global.CSSStyleDeclaration = {
+    prototype: {
+      setAttribute: noop,
+    },
+  };
+  global.navigator = {
+    userAgent: '',
+  };
+  global.removeEventListener = noop;
+  global.addEventListener = noop;
+  global.document = {
+    cookie: '',
+    getElementsByTagName() {
+      return [];
+    },
+    body: {
+      appendChild: noop,
+      removeChild: noop,
+    },
+    createElement() {
+      return {
+        setAttribute: noop,
+        style: {},
+      };
+    },
+  };
+
+  global.btoa = noop;
+
+  global.window = {
+    document: global.document,
+    history: {},
+    navigator: global.navigator,
+    location: {
+      hostname: '',
+      search: '',
+    },
+  };
+
+  const bundles = await Promise.all([
+    getBundle('https://nr1.nr-assets.net/lib/d3/3.5.17/d3.js'),
+    getBundle(
+      'https://nr1.nr-assets.net/lib/react/16.6.3/react.development.js'
+    ),
+    getBundle(
+      'https://nr1.nr-assets.net/lib/react/16.6.3/react-dom.development.js'
+    ),
+    getBundle(
+      'https://nr1.nr-assets.net/lib/react-router-dom/4.2.2/react-router-dom.js'
+    ),
+    getBundle(`${BASE_URL}-${release}.js`),
+    '__NR1_SDK__',
+  ]);
+
+  const sdk = vm.runInThisContext(bundles.join('\n'));
+
+  console.log(sdk);
+};
diff --git a/src/utils/sdk.js b/src/utils/sdk.js
index 786954413..0d65ab61d 100644
--- a/src/utils/sdk.js
+++ b/src/utils/sdk.js
@@ -1,6 +1,6 @@
 const BASE_URL =
   '//hypertext-sandbox.nr-assets.net/wanda--wanda-ec-ui--nr1-docs';
-const RELEASE = 'release-1093';
 
+export const RELEASE = 'release-1093';
 export const JS_BUNDLE = `${BASE_URL}-${RELEASE}.js`;
 export const CSS_BUNDLE = `${BASE_URL}-${RELEASE}.css`;