diff --git a/compiler/CodeGenerator.js b/compiler/CodeGenerator.js index dd33b56e7..2abede12c 100644 --- a/compiler/CodeGenerator.js +++ b/compiler/CodeGenerator.js @@ -130,6 +130,10 @@ class Generator { this.context.addStaticCode(code); } + importModule(varName, path) { + return this.context.importModule(varName, path); + } + getStaticVars() { return this.context.getStaticVars(); } diff --git a/compiler/CompileContext.js b/compiler/CompileContext.js index 979e09252..227f83e2c 100644 --- a/compiler/CompileContext.js +++ b/compiler/CompileContext.js @@ -114,6 +114,15 @@ class CompileContext { return deresolve(targetFilename, this.dirname); } + importModule(varName, path) { + if (typeof path !== 'string') { + throw new Error('"path" should be a string'); + } + + return this.addStaticVar(varName, 'require("' + path + '")'); + } + + addStaticVar(name, init) { var actualVarName = this._uniqueVars.addVar(name, init); this._staticVars[actualVarName] = init; diff --git a/test/fixtures/compiler/autotest/importModule/expected.js b/test/fixtures/compiler/autotest/importModule/expected.js new file mode 100644 index 000000000..bf1632cb4 --- /dev/null +++ b/test/fixtures/compiler/autotest/importModule/expected.js @@ -0,0 +1,13 @@ +function create(__helpers) { + var str = __helpers.s, + empty = __helpers.e, + notEmpty = __helpers.ne, + escapeXml = __helpers.x, + foo = require("./foo"); + + return function render(data, out) { + foo(); + }; +} + +(module.exports = require("marko").c(__filename)).c(create); diff --git a/test/fixtures/compiler/autotest/importModule/template.marko b/test/fixtures/compiler/autotest/importModule/template.marko new file mode 100644 index 000000000..8e8cd64e0 --- /dev/null +++ b/test/fixtures/compiler/autotest/importModule/template.marko @@ -0,0 +1 @@ + \ No newline at end of file