Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update YUI Handlebars to use v2.0.0 #1956

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 188 additions & 41 deletions src/handlebars/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,200 @@ 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/helpers.js': {
before: 'Handlebars.Helpers = {};',
arguments: {
exports: 'Handlebars.Helpers',
Exception: 'Handlebars.Exception'
}
},
'handlebars/compiler/parser.js': {
before: 'Handlebars.Parser = {};',
arguments: {
exports: 'Handlebars.Parser',
yy: 'Handlebars.Helpers'
},
after: 'Handlebars.Parser = Handlebars.Parser[\'default\'];'
},
'handlebars/compiler/base.js': {
arguments: {
exports: 'Handlebars',
parser: 'Handlebars.Parser',
AST: 'Handlebars.AST',
extend: 'Handlebars.Utils.extend',
Helpers: 'Handlebars.Helpers'
}
},
'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',
isArray: 'Handlebars.Utils.isArray',
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 +222,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 +235,7 @@ module.exports = function (grunt) {
});

grunt.registerTask('import', [
'import-root',
'import-base',
'import-compiler'
]);
Expand Down
12 changes: 9 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 @@ -22,11 +24,15 @@
"jsfiles": [
"yui-handlebars-copyright.js",
"yui-handlebars-compiler-before.js",
"handlebars-compiler-helpers.js",
"handlebars-compiler-parser.js",
"handlebars-compiler-base.js",
"handlebars-compiler-ast.js",
"handlebars-compiler-compiler.js",
"yui-handlebars-compiler-after.js"
"handlebars-compiler-javascript-compiler.js",
"handlebars-compiler-base.js",
"yui-handlebars-compiler-after.js",
"handlebars.runtime.js",
"handlebars.js"
]
}
}
Expand Down
Loading