added require() method for commonJS compliancy

This commit is contained in:
azivner
2018-03-10 11:53:51 -05:00
parent 354999f37a
commit f6c7f6a0f2
3 changed files with 33 additions and 4 deletions

View File

@@ -111,13 +111,15 @@ async function getScriptBundle(note, root = true, scriptEnv = null, includedNote
}
}
const moduleNoteIds = modules.map(mod => mod.noteId);
if (note.isJavaScript()) {
bundle.script += `
apiContext.modules['${note.noteId}'] = {};
${root ? 'return ' : ''}await (async function(exports, module, api` + (modules.length > 0 ? ', ' : '') +
${root ? 'return ' : ''}await (async function(exports, module, require, api` + (modules.length > 0 ? ', ' : '') +
modules.map(child => sanitizeVariableName(child.title)).join(', ') + `) {
${note.content}
})({}, apiContext.modules['${note.noteId}'], apiContext.apis['${note.noteId}']` + (modules.length > 0 ? ', ' : '') +
})({}, apiContext.modules['${note.noteId}'], apiContext.require(${JSON.stringify(moduleNoteIds)}), apiContext.apis['${note.noteId}']` + (modules.length > 0 ? ', ' : '') +
modules.map(mod => `apiContext.modules['${mod.noteId}'].exports`).join(', ') + `);
`;
}