inclusion of scripts based on script environment

This commit is contained in:
azivner 2018-03-05 23:09:36 -05:00
parent b3209a9bbf
commit d26170762b
2 changed files with 24 additions and 3 deletions

View File

@ -32,6 +32,18 @@ class Note extends Entity {
return (this.type === "code" || this.type === "file") && this.mime === "text/html"; return (this.type === "code" || this.type === "file") && this.mime === "text/html";
} }
getScriptEnv() {
if (this.isHtml() || (this.isJavaScript() && this.mime.endsWith('env=frontend'))) {
return "frontend";
}
if (this.isJavaScript() && this.mime.endsWith('env=backend')) {
return "backend";
}
return null;
}
async getAttributes() { async getAttributes() {
return this.repository.getEntities("SELECT * FROM attributes WHERE noteId = ? AND isDeleted = 0", [this.noteId]); return this.repository.getEntities("SELECT * FROM attributes WHERE noteId = ? AND isDeleted = 0", [this.noteId]);
} }

View File

@ -60,7 +60,7 @@ async function getRenderScript(note) {
+ bundle.html; + bundle.html;
} }
async function getScriptBundle(note, includedNoteIds = []) { async function getScriptBundle(note, scriptEnv, includedNoteIds = []) {
if (!note.isJavaScript() && !note.isHtml() && note.type !== 'render') { if (!note.isJavaScript() && !note.isHtml() && note.type !== 'render') {
return; return;
} }
@ -69,6 +69,10 @@ async function getScriptBundle(note, includedNoteIds = []) {
return; return;
} }
if (!scriptEnv) {
scriptEnv = note.getScriptEnv();
}
const bundle = { const bundle = {
note: note, note: note,
script: '', script: '',
@ -82,10 +86,16 @@ async function getScriptBundle(note, includedNoteIds = []) {
includedNoteIds.push(note.noteId); includedNoteIds.push(note.noteId);
bundle.script = `api.__modules['${note.noteId}'] = { __noteId: '${note.noteId}', __env: '${note.getScriptEnv()}'};`;
if (note.type !== 'file' && scriptEnv !== note.getScriptEnv()) {
return bundle;
}
const modules = []; const modules = [];
for (const child of await note.getChildren()) { for (const child of await note.getChildren()) {
const childBundle = await getScriptBundle(child, includedNoteIds); const childBundle = await getScriptBundle(child, scriptEnv, includedNoteIds);
if (childBundle) { if (childBundle) {
modules.push(childBundle.note); modules.push(childBundle.note);
@ -97,7 +107,6 @@ async function getScriptBundle(note, includedNoteIds = []) {
if (note.isJavaScript()) { if (note.isJavaScript()) {
bundle.script += ` bundle.script += `
api.__modules['${note.noteId}'] = {};
await (async function(exports, module, api, startNote, currentNote` + (modules.length > 0 ? ', ' : '') + await (async function(exports, module, api, startNote, currentNote` + (modules.length > 0 ? ', ' : '') +
modules.map(child => child.title).join(', ') + `) { modules.map(child => child.title).join(', ') + `) {
${note.content} ${note.content}