Skip to content

Commit

Permalink
✨ add sourceURL mapping to inline script optimize debug experience (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuitos authored Apr 28, 2020
1 parent d238847 commit 05f0056
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ function getEmbedHTML(template, styles, opts = {}) {
});
}

function getExecutableScript(scriptText, proxy, strictGlobal) {
function getExecutableScript(scriptSrc, scriptText, proxy, strictGlobal) {
window.proxy = proxy;
// TODO 通过 strictGlobal 方式切换切换 with 闭包,待 with 方式坑趟平后再合并
return strictGlobal
? `;(function(window, self){with(window){;${scriptText}\n}}).bind(window.proxy)(window.proxy, window.proxy);`
: `;(function(window, self){;${scriptText}\n}).bind(window.proxy)(window.proxy, window.proxy);`;
? `;(function(window, self){with(window){;${scriptText}\n//# sourceURL=${scriptSrc}\n}}).bind(window.proxy)(window.proxy, window.proxy);`
: `;(function(window, self){;${scriptText}\n//# sourceURL=${scriptSrc}\n}).bind(window.proxy)(window.proxy, window.proxy);`;
}

// for prefetch
Expand Down Expand Up @@ -85,6 +85,7 @@ export function getExternalScripts(scripts, fetch = defaultFetch) {
const { src, async } = script;
if (async) {
return {
src,
async: true,
content: new Promise((resolve, reject) => requestIdleCallback(() => fetchScript(src).then(resolve, reject))),
};
Expand Down Expand Up @@ -123,33 +124,23 @@ export function execScripts(entry, scripts, proxy = window, opts = {}) {
if (scriptSrc === entry) {
noteGlobalProps(strictGlobal ? proxy : window);

try {
// bind window.proxy to change `this` reference in script
geval(getExecutableScript(inlineScript, proxy, strictGlobal));
} catch (e) {
console.error(`error occurs while executing the entry ${scriptSrc}`);
throw e;
}
// bind window.proxy to change `this` reference in script
geval(getExecutableScript(scriptSrc, inlineScript, proxy, strictGlobal));

const exports = proxy[getGlobalProp(strictGlobal ? proxy : window)] || {};
resolve(exports);

} else {

if (typeof inlineScript === 'string') {
try {
// bind window.proxy to change `this` reference in script
geval(getExecutableScript(inlineScript, proxy, strictGlobal));
} catch (e) {
console.error(`error occurs while executing ${scriptSrc}`);
throw e;
}
// bind window.proxy to change `this` reference in script
geval(getExecutableScript(scriptSrc, inlineScript, proxy, strictGlobal));
} else {
// external script marked with async
inlineScript.async && inlineScript?.content
.then(downloadedScriptText => geval(getExecutableScript(downloadedScriptText, proxy)))
.then(downloadedScriptText => geval(getExecutableScript(inlineScript.src, downloadedScriptText, proxy, strictGlobal)))
.catch(e => {
console.error(`error occurs while executing async script ${scriptSrc?.src}`);
console.error(`error occurs while executing async script ${inlineScript.src}`);
throw e;
});
}
Expand Down

0 comments on commit 05f0056

Please sign in to comment.