Fix script needlessly being recompiled (#932)

* Fix nsjs recompiling needlesly.
This commit is contained in:
hydroflame
2021-05-03 18:53:10 -04:00
committed by GitHub
parent 5613d371c9
commit 3cbf225c98
2 changed files with 10 additions and 2 deletions
+5 -2
View File
@@ -20,6 +20,7 @@ export async function executeJSScript(scripts = [], workerScript) {
let urls = null;
let script = workerScript.getScript();
if (shouldCompile(script, scripts)) {
console.log("recompiling");
// The URL at the top is the one we want to import. It will
// recursively import all the other modules in the urlStack.
//
@@ -27,6 +28,7 @@ export async function executeJSScript(scripts = [], workerScript) {
// but not really behaves like import. Particularly, it cannot
// load fully dynamic content. So we hide the import from webpack
// by placing it inside an eval call.
script.markUpdated();
urls = _getScriptUrls(script, scripts, []);
script.url = urls[urls.length - 1].url;
script.module = new Promise(resolve => resolve(eval('import(urls[urls.length - 1].url)')));
@@ -58,14 +60,15 @@ export async function executeJSScript(scripts = [], workerScript) {
*/
function shouldCompile(script, scripts) {
if (script.module === "") return true;
console.log(script.dependencies);
return script.dependencies.some(dep => {
const depScript = scripts.find(s => s.filename == dep.url);
const depScript = scripts.find(s => s.filename == dep.filename);
// If the script is not present on the server, we should recompile, if only to get any necessary
// compilation errors.
if (!depScript) return true;
const depIsMoreRecent = depScript.moduleSequenceNumber > script.moduleSequenceNumber
const depIsMoreRecent = depScript.moduleSequenceNumber > script.moduleSequenceNumber;
return depIsMoreRecent;
});
}