-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move sdk load logic into own module
- Loading branch information
1 parent
718acec
commit 9931b7d
Showing
3 changed files
with
78 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: '', | ||
}, | ||
}; |