Skip to content

Commit

Permalink
feat: Add working code to load the sdk bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jun 12, 2020
1 parent e70207d commit 718acec
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 4 deletions.
8 changes: 8 additions & 0 deletions gatsby-config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// const { RELEASE } = require('./src/utils/sdk');

module.exports = {
siteMetadata: {
title: 'New Relic Developers',
Expand Down Expand Up @@ -70,5 +72,11 @@ module.exports = {
},
},
'gatsby-plugin-sitemap',
{
resolve: 'gatsby-source-nr1-sdk',
options: {
release: 'release-1093',
},
},
],
};
14 changes: 11 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
78 changes: 78 additions & 0 deletions plugins/gatsby-source-nr1-sdk/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -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);
};
2 changes: 1 addition & 1 deletion src/utils/sdk.js
Original file line number Diff line number Diff line change
@@ -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`;

0 comments on commit 718acec

Please sign in to comment.