diff --git a/src/snippet.js b/src/snippet.js index 2121cc4..b285dab 100644 --- a/src/snippet.js +++ b/src/snippet.js @@ -319,14 +319,17 @@ function gatherCode(curSnip) { let ids = curSnip.dependsOn ? curSnip.dependsOn.split(" ") : []; // print separators between snippets to tail output later const sep = curSnip.hasAttribute("output-tail") ? codegen.hr(curSnip.syntax) : ""; - for (const id of ids) { + // first dependency should be the last one to be prepended + for (const id of ids.reverse()) { const snip = document.getElementById(id); if (!snip) { throw new Error(`#${id} dependency not found`); } code = snip.code + `\n${sep}\n` + code; if (snip.dependsOn) { - ids.push(...snip.dependsOn.split(" ").filter((i) => !ids.includes(i))); + const moreIDs = snip.dependsOn.split(" ").filter((i) => !ids.includes(i)); + // first dependency should be the last one to be prepended + ids.push(...moreIDs.reverse()); } } return code; diff --git a/tests/snippet.js b/tests/snippet.js index 7e1575d..ccaa6eb 100644 --- a/tests/snippet.js +++ b/tests/snippet.js @@ -70,6 +70,8 @@ async function runTests() { await testFilesTargetPath(); await testDependsOn(); + await testDependsOrder1(); + await testDependsOrder2(); t.summary(); return t.errorCount; @@ -1017,6 +1019,67 @@ async function testDependsOn() { }); } +async function testDependsOrder1() { + return new Promise((resolve, reject) => { + t.log("testDependsOrder1..."); + const html = ` +
console.log("step-1")
+ console.log("step-2")
+ console.log("step-3")
+ console.log("step-1")
+ console.log("step-2")
+ console.log("step-3")
+ console.log("step-4")
+