Skip to content

Commit

Permalink
Upgrade Handlebars.js to v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aljimenez authored and tripp committed Sep 23, 2014
1 parent 6c88ea0 commit 754c951
Show file tree
Hide file tree
Showing 16 changed files with 2,005 additions and 1,446 deletions.
218 changes: 177 additions & 41 deletions src/handlebars/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,189 @@ module.exports = function (grunt) {

var path = require('path'),

HANDLEBARS_LIB = path.join(process.cwd(), '../../../', 'handlebars.js/lib'),
PREPENDED_COMMENT = '/* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */\n',
REMOVE_EXPORTS_REGEX = /^\/\/ BEGIN\(BROWSER\)$\s*([\s\S]*)^\/\/ END\(BROWSER\)$/m,

BASE_FILES = [
'handlebars/base.js',
'handlebars/utils.js',
'handlebars/runtime.js'
],

COMPILER_FILES = [
'handlebars/compiler/parser.js',
'handlebars/compiler/base.js',
'handlebars/compiler/ast.js',
'handlebars/compiler/compiler.js'
];
HANDLEBARS_LIB = path.join(process.cwd(), '../../../', 'handlebars.js'),
HANDLEBARS_BUILD_PATH = 'dist/cjs',
PREPENDED_COMMENT = '/* THIS FILE IS GENERATED BY A BUILD SCRIPT - DO NOT EDIT! */\n',
REMOVE_EXPORTS_REGEX = /^\/\/ BEGIN\(BROWSER\)$\s*([\s\S]*)^\/\/ END\(BROWSER\)$/m,
REMOVE_REQUIRES_REGEX = /^.+=\s*require\([^\)]+\)[^;,]*[;,]\s*/gm,
MODULE_TEMPLATE = '{{comment}}' +
'{{before}}' +
'(function ({{argumentNames}}) {\n' +
'{{module}}\n' +
'}({{arguments}}));\n' +
'{{after}}',

ROOT_FILES = {
'handlebars.js': {
arguments: {
exports: 'Handlebars',
Handlebars: 'Handlebars.Runtime',
AST: 'Handlebars.AST',
Parser: 'Handlebars.Parser',
parse: 'Handlebars.parse',
Compiler: 'Handlebars.Compiler',
compile: 'Handlebars.compile',
precompile: 'Handlebars.precompile',
JavaScriptCompiler: 'Handlebars.JavaScriptCompiler'
},
'after': 'Y.Handlebars = Handlebars[\'default\'];'
},
'handlebars.runtime.js': {
before: 'Handlebars.Runtime = {};',
arguments: {
exports: 'Handlebars.Runtime',
base: 'Handlebars',
SafeString: 'Handlebars.SafeString',
Exception: 'Handlebars.Exception',
Utils: 'Handlebars.Utils',
runtime: 'Handlebars.VM'
},
after: 'Handlebars.Runtime = Handlebars.Runtime[\'default\'];'
}
},

BASE_FILES = {
'handlebars/base.js': {
arguments: {
exports: 'Handlebars',
Utils: 'Handlebars.Utils',
Exception: 'Handlebars.Exception'
}
},
'handlebars/utils.js': {
before: 'Handlebars.Utils = {};',
arguments: {
exports: 'Handlebars.Utils',
SafeString: 'Handlebars.SafeString'
}
},
'handlebars/runtime.js': {
before: 'Handlebars.VM = {};',
arguments: {
exports: 'Handlebars.VM',
Utils: 'Handlebars.Utils',
Exception: 'Handlebars.Exception',
COMPILER_REVISION: 'Handlebars.COMPILER_REVISION',
REVISION_CHANGES: 'Handlebars.REVISION_CHANGES'
},
after: 'Handlebars.template = Handlebars.VM.template;'
},
'handlebars/exception.js': {
before: 'Handlebars.Exception = {};',
arguments: {
exports: 'Handlebars.Exception'
},
after: 'Handlebars.Exception = Handlebars.Exception[\'default\'];'
},
'handlebars/safe-string.js': {
before: 'Handlebars.SafeString = {};',
arguments: {
exports: 'Handlebars.SafeString'
},
after: 'Handlebars.SafeString = Handlebars.SafeString[\'default\'];'
}
},

COMPILER_FILES = {
'handlebars/compiler/parser.js': {
before: 'Handlebars.Parser = {};',
arguments: {
exports: 'Handlebars.Parser'
},
after: 'Handlebars.Parser = Handlebars.Parser[\'default\'];'
},
'handlebars/compiler/base.js': {
arguments: {
exports: 'Handlebars',
parser: 'Handlebars.Parser',
AST: 'Handlebars.AST'
}
},
'handlebars/compiler/ast.js': {
before: 'Handlebars.AST = {};',
arguments: {
exports: 'Handlebars.AST',
Exception: 'Handlebars.Exception'
},
after: 'Handlebars.AST = Handlebars.AST[\'default\'];'
},
'handlebars/compiler/compiler.js': {
arguments: {
exports: 'Handlebars',
Exception: 'Handlebars.Exception'
}
},
'handlebars/compiler/javascript-compiler.js': {
before: 'Handlebars.JavaScriptCompiler = {};',
arguments: {
exports: 'Handlebars.JavaScriptCompiler',
COMPILER_REVISION: 'Handlebars.COMPILER_REVISION',
REVISION_CHANGES: 'Handlebars.REVISION_CHANGES',
log: 'Handlebars.log',
Exception: 'Handlebars.Exception'
},
after: 'Handlebars.JavaScriptCompiler = Handlebars.JavaScriptCompiler[\'default\'];'
}
};

function importFiles(files, prefix) {
prefix || (prefix = '');
function removeExports(contents) {
var match = contents.match(REMOVE_EXPORTS_REGEX);
return match ? match[1] : contents;
}

files.forEach(function (file) {
var src = path.join(HANDLEBARS_LIB, file),
dest = path.join('js', prefix + path.basename(file));
function removeRequires(contents) {
return contents.replace(REMOVE_REQUIRES_REGEX, '');
}

if (!grunt.file.exists(src)) {
grunt.fail.fatal('Did you build Handlebars.js yet?');
function render(template, templateValues) {
var stachs = template.match(/\{\{([^{}]+)\}\}/g) || [];

stachs.forEach(function (stach) {
var key = stach.substring(2, stach.length - 2).trim(),
value = templateValues[key] !== undefined ? templateValues[key] : '';
stach = stach.replace(/\{/g, '\\{').replace(/\}/g, '\\}');
if (value instanceof Array) {
value = value.join(', ');
}
template = template.replace(new RegExp(stach, 'g'), value);
});

grunt.log.writeln('Importing: '.green + file.cyan + ' to ' + dest.cyan);
grunt.file.write(dest, processFile(src));
return template;
}

function processFile(file, options) {
var module = grunt.file.read(file),
templateValues = {};

templateValues.comment = PREPENDED_COMMENT;
templateValues.before = options.before && (options.before + '\n');
templateValues.module = removeRequires(removeExports(module)).trim();
templateValues.argumentNames = Object.keys(options.arguments || {});
templateValues.arguments = [];
templateValues.argumentNames.forEach(function (argumentName) {
var argument = options.arguments[argumentName];
templateValues.arguments.push(argument);
});
templateValues.after = options.after && (options.after + '\n');

return render(MODULE_TEMPLATE, templateValues);
}

function processFile(file) {
var contents = grunt.file.read(file),
processed;
function importFiles(files, prefix) {
var fileName;
prefix || (prefix = '');

// Processes the raw file by adding a the "this is a build file"
// comment, and remove the module exports from the contents of the file.
// This also trims the file and makes sure it ends with a newline char.
processed = [
PREPENDED_COMMENT,
removeExports(contents).trim(),
''
].join('\n');
for (fileName in files) {
var src = path.join(HANDLEBARS_LIB, HANDLEBARS_BUILD_PATH, fileName),
dest = path.join('js', prefix + path.basename(fileName));

return processed;
}
if (!grunt.file.exists(src)) {
grunt.fail.fatal('Did you build Handlebars.js yet? Make sure to run grunt inside handlebars.js.');
}

function removeExports(contents) {
var match = contents.match(REMOVE_EXPORTS_REGEX);
return match ? match[1] : contents;
grunt.log.writeln('Importing: '.green + fileName.cyan + ' to ' + dest.cyan);
grunt.file.write(dest, processFile(src, files[fileName]));
};
}

// -- Tasks ----------------------------------------------------------------
Expand All @@ -80,6 +211,10 @@ module.exports = function (grunt) {
});
});

grunt.registerTask('import-root', 'Import Handlebars root source files.', function () {
importFiles(ROOT_FILES);
});

grunt.registerTask('import-base', 'Import Handlebars base source files.', function () {
importFiles(BASE_FILES, 'handlebars-');
});
Expand All @@ -89,6 +224,7 @@ module.exports = function (grunt) {
});

grunt.registerTask('import', [
'import-root',
'import-base',
'import-compiler'
]);
Expand Down
4 changes: 3 additions & 1 deletion src/handlebars/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Handlebars Change History
@VERSION@
------

* No changes.
* Upgraded Handlebars.js to v1.3.0. See [Handlebars' release notes][v1.3.0].

[v1.3.0]: https://github.com/wycats/handlebars.js/blob/master/release-notes.md#v130---january-1st-2014

3.17.2
------
Expand Down
11 changes: 8 additions & 3 deletions src/handlebars/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
"jsfiles": [
"yui-handlebars-copyright.js",
"yui-handlebars-base-before.js",
"handlebars-base.js",
"handlebars-safe-string.js",
"handlebars-utils.js",
"handlebars-exception.js",
"handlebars-base.js",
"handlebars-runtime.js",
"yui-handlebars-base-after.js"
]
Expand All @@ -23,10 +25,13 @@
"yui-handlebars-copyright.js",
"yui-handlebars-compiler-before.js",
"handlebars-compiler-parser.js",
"handlebars-compiler-base.js",
"handlebars-compiler-ast.js",
"handlebars-compiler-base.js",
"handlebars-compiler-compiler.js",
"yui-handlebars-compiler-after.js"
"handlebars-compiler-javascript-compiler.js",
"yui-handlebars-compiler-after.js",
"handlebars.runtime.js",
"handlebars.js"
]
}
}
Expand Down
Loading

0 comments on commit 754c951

Please sign in to comment.