Skip to content

Commit

Permalink
refactor: Move sdk load logic into own module
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jun 12, 2020
1 parent 718acec commit 9931b7d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 71 deletions.
73 changes: 2 additions & 71 deletions plugins/gatsby-source-nr1-sdk/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,9 @@
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();
};
const loadSdk = require('./loadSdk');

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'));
const sdk = await loadSdk(release);

console.log(sdk);
};
31 changes: 31 additions & 0 deletions plugins/gatsby-source-nr1-sdk/loadSdk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const vm = require('vm');
const fetch = require('node-fetch');

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();
};

module.exports = async (release) => {
require('./mockGlobals');

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`),
]);

return vm.runInThisContext([...bundles, '__NR1_SDK__'].join('\n'));
};
45 changes: 45 additions & 0 deletions plugins/gatsby-source-nr1-sdk/mockGlobals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const noop = () => {};

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: '',
},
};

0 comments on commit 9931b7d

Please sign in to comment.