mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Added helper method to import a module into the compiled template module
This commit is contained in:
parent
4c419abbe9
commit
dfdbf8b377
@ -130,6 +130,10 @@ class Generator {
|
||||
this.context.addStaticCode(code);
|
||||
}
|
||||
|
||||
importModule(varName, path) {
|
||||
return this.context.importModule(varName, path);
|
||||
}
|
||||
|
||||
getStaticVars() {
|
||||
return this.context.getStaticVars();
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
13
test/fixtures/compiler/autotest/importModule/expected.js
vendored
Normal file
13
test/fixtures/compiler/autotest/importModule/expected.js
vendored
Normal file
@ -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);
|
||||
1
test/fixtures/compiler/autotest/importModule/template.marko
vendored
Normal file
1
test/fixtures/compiler/autotest/importModule/template.marko
vendored
Normal file
@ -0,0 +1 @@
|
||||
<test-import-module/>
|
||||
Loading…
x
Reference in New Issue
Block a user