Skip to content

Commit 448e43c

Browse files
committed
Merge pull request #424 from wagenet/version-check-v2
Improved precompile template version check
2 parents edb6f11 + 6fe7f17 commit 448e43c

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

dist/handlebars.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1381,10 +1381,7 @@ Handlebars.JavaScriptCompiler = function() {};
13811381
var source = this.mergeSource();
13821382

13831383
if (!this.isChild) {
1384-
source = "if (Handlebars.VERSION !== '"+Handlebars.VERSION+"') {\n"+
1385-
"throw 'Template was compiled with "+Handlebars.VERSION+", but runtime is '+Handlebars.VERSION;\n"+
1386-
"}\n"+
1387-
source;
1384+
source = "this.compiledVersion = '"+Handlebars.VERSION+"';\n"+source;
13881385
}
13891386

13901387
if (asObject) {
@@ -2126,12 +2123,17 @@ Handlebars.VM = {
21262123
}
21272124
},
21282125
programWithDepth: Handlebars.VM.programWithDepth,
2129-
noop: Handlebars.VM.noop
2126+
noop: Handlebars.VM.noop,
2127+
compiledVersion: null
21302128
};
21312129

21322130
return function(context, options) {
21332131
options = options || {};
2134-
return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
2132+
var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
2133+
if (container.compiledVersion !== Handlebars.VERSION) {
2134+
throw "Template was compiled with "+(container.compiledVersion || 'unknown version')+", but runtime is "+Handlebars.VERSION;
2135+
}
2136+
return result;
21352137
};
21362138
},
21372139

dist/handlebars.runtime.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,17 @@ Handlebars.VM = {
249249
}
250250
},
251251
programWithDepth: Handlebars.VM.programWithDepth,
252-
noop: Handlebars.VM.noop
252+
noop: Handlebars.VM.noop,
253+
compiledVersion: null
253254
};
254255

255256
return function(context, options) {
256257
options = options || {};
257-
return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
258+
var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
259+
if (container.compiledVersion !== Handlebars.VERSION) {
260+
throw "Template was compiled with "+(container.compiledVersion || 'unknown version')+", but runtime is "+Handlebars.VERSION;
261+
}
262+
return result;
258263
};
259264
},
260265

lib/handlebars/compiler/compiler.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,7 @@ Handlebars.JavaScriptCompiler = function() {};
539539
var source = this.mergeSource();
540540

541541
if (!this.isChild) {
542-
source = "if (Handlebars.VERSION !== '"+Handlebars.VERSION+"') {\n"+
543-
"throw 'Template was compiled with "+Handlebars.VERSION+", but runtime is '+Handlebars.VERSION;\n"+
544-
"}\n"+
545-
source;
542+
source = "this.compiledVersion = '"+Handlebars.VERSION+"';\n"+source;
546543
}
547544

548545
if (asObject) {

lib/handlebars/runtime.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ Handlebars.VM = {
2020
}
2121
},
2222
programWithDepth: Handlebars.VM.programWithDepth,
23-
noop: Handlebars.VM.noop
23+
noop: Handlebars.VM.noop,
24+
compiledVersion: null
2425
};
2526

2627
return function(context, options) {
2728
options = options || {};
28-
return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
29+
var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
30+
if (container.compiledVersion !== Handlebars.VERSION) {
31+
throw "Template was compiled with "+(container.compiledVersion || 'unknown version')+", but runtime is "+Handlebars.VERSION;
32+
}
33+
return result;
2934
};
3035
},
3136

0 commit comments

Comments
 (0)