Skip to content

Commit ac98e7b

Browse files
committed
Merge pull request #694 from blakeembrey/compile-env
Make the environment reusable
2 parents 14d1d42 + 2f0c96b commit ac98e7b

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

lib/handlebars.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ var create = function() {
1414
hb.compile = function(input, options) {
1515
return compile(input, options, hb);
1616
};
17-
hb.precompile = precompile;
17+
hb.precompile = function (input, options) {
18+
return precompile(input, options, hb);
19+
};
1820

1921
hb.AST = AST;
2022
hb.Compiler = Compiler;

lib/handlebars/compiler/compiler.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import Exception from "../exception";
2-
import { parse } from "./base";
3-
import JavaScriptCompiler from "./javascript-compiler";
4-
import AST from "./ast";
52

63
export function Compiler() {}
74

@@ -423,8 +420,8 @@ Compiler.prototype = {
423420
}
424421
};
425422

426-
export function precompile(input, options) {
427-
if (input == null || (typeof input !== 'string' && input.constructor !== AST.ProgramNode)) {
423+
export function precompile(input, options, env) {
424+
if (input == null || (typeof input !== 'string' && input.constructor !== env.AST.ProgramNode)) {
428425
throw new Exception("You must pass a string or Handlebars AST to Handlebars.precompile. You passed " + input);
429426
}
430427

@@ -433,13 +430,13 @@ export function precompile(input, options) {
433430
options.data = true;
434431
}
435432

436-
var ast = parse(input);
437-
var environment = new Compiler().compile(ast, options);
438-
return new JavaScriptCompiler().compile(environment, options);
433+
var ast = env.parse(input);
434+
var environment = new env.Compiler().compile(ast, options);
435+
return new env.JavaScriptCompiler().compile(environment, options);
439436
}
440437

441438
export function compile(input, options, env) {
442-
if (input == null || (typeof input !== 'string' && input.constructor !== AST.ProgramNode)) {
439+
if (input == null || (typeof input !== 'string' && input.constructor !== env.AST.ProgramNode)) {
443440
throw new Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input);
444441
}
445442

@@ -452,9 +449,9 @@ export function compile(input, options, env) {
452449
var compiled;
453450

454451
function compileInput() {
455-
var ast = parse(input);
456-
var environment = new Compiler().compile(ast, options);
457-
var templateSpec = new JavaScriptCompiler().compile(environment, options, undefined, true);
452+
var ast = env.parse(input);
453+
var environment = new env.Compiler().compile(ast, options);
454+
var templateSpec = new env.JavaScriptCompiler().compile(environment, options, undefined, true);
458455
return env.template(templateSpec);
459456
}
460457

spec/env/runtime.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,32 @@ var _ = require('underscore'),
88
global.Handlebars = undefined;
99
vm.runInThisContext(fs.readFileSync(__dirname + '/../../dist/handlebars.runtime.js'), 'dist/handlebars.runtime.js');
1010

11+
var parse = require('../../dist/cjs/handlebars/compiler/base').parse;
1112
var compiler = require('../../dist/cjs/handlebars/compiler/compiler');
13+
var JavaScriptCompiler = require('../../dist/cjs/handlebars/compiler/javascript-compiler')['default'];
1214

1315
global.CompilerContext = {
1416
compile: function(template, options) {
15-
var templateSpec = compiler.precompile(template, options);
17+
// Hack the compiler on to the environment for these specific tests
18+
handlebarsEnv.precompile = function(template, options) {
19+
return compiler.precompile(template, options, handlebarsEnv);
20+
};
21+
handlebarsEnv.parse = parse;
22+
handlebarsEnv.Compiler = compiler.Compiler;
23+
handlebarsEnv.JavaScriptCompiler = JavaScriptCompiler;
24+
25+
var templateSpec = handlebarsEnv.precompile(template, options);
1626
return handlebarsEnv.template(safeEval(templateSpec));
1727
},
1828
compileWithPartial: function(template, options) {
1929
// Hack the compiler on to the environment for these specific tests
2030
handlebarsEnv.compile = function(template, options) {
2131
return compiler.compile(template, options, handlebarsEnv);
2232
};
33+
handlebarsEnv.parse = parse;
34+
handlebarsEnv.Compiler = compiler.Compiler;
35+
handlebarsEnv.JavaScriptCompiler = JavaScriptCompiler;
36+
2337
return handlebarsEnv.compile(template, options);
2438
}
2539
};

0 commit comments

Comments
 (0)