Run the precompiler module through es6 toolchain
This commit is contained in:
+1
-1
@@ -52,7 +52,7 @@ module.exports = function(grunt) {
|
||||
files: [{
|
||||
expand: true,
|
||||
cwd: 'lib/',
|
||||
src: '**/!(index|precompiler).js',
|
||||
src: '**/!(index).js',
|
||||
dest: 'dist/amd/'
|
||||
}]
|
||||
},
|
||||
|
||||
+1
-1
@@ -108,4 +108,4 @@ if (argv.help || (!argv.templates.length && !argv.version)) {
|
||||
return;
|
||||
}
|
||||
|
||||
return require('../lib/precompiler').cli(argv);
|
||||
return require('../dist/cjs/precompiler').cli(argv);
|
||||
|
||||
+16
-18
@@ -1,11 +1,9 @@
|
||||
/*eslint-disable no-console */
|
||||
var fs = require('fs'),
|
||||
Handlebars = require('./index'),
|
||||
basename = require('path').basename,
|
||||
SourceMap = require('source-map'),
|
||||
SourceMapConsumer = SourceMap.SourceMapConsumer,
|
||||
SourceNode = SourceMap.SourceNode,
|
||||
uglify = require('uglify-js');
|
||||
import fs from 'fs';
|
||||
import * as Handlebars from './handlebars';
|
||||
import {basename} from 'path';
|
||||
import {SourceMapConsumer, SourceNode} from 'source-map';
|
||||
import uglify from 'uglify-js';
|
||||
|
||||
module.exports.cli = function(opts) {
|
||||
if (opts.version) {
|
||||
@@ -21,7 +19,7 @@ module.exports.cli = function(opts) {
|
||||
try {
|
||||
fs.statSync(template);
|
||||
} catch (err) {
|
||||
throw new Handlebars.Exception('Unable to open template file "' + template + '"');
|
||||
throw new Handlebars.Exception(`Unable to open template file "${template}"`);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -33,21 +31,21 @@ module.exports.cli = function(opts) {
|
||||
}
|
||||
|
||||
// Convert the known list into a hash
|
||||
var known = {};
|
||||
let known = {};
|
||||
if (opts.known && !Array.isArray(opts.known)) {
|
||||
opts.known = [opts.known];
|
||||
}
|
||||
if (opts.known) {
|
||||
for (var i = 0, len = opts.known.length; i < len; i++) {
|
||||
for (let i = 0, len = opts.known.length; i < len; i++) {
|
||||
known[opts.known[i]] = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Build file extension pattern
|
||||
var extension = opts.extension.replace(/[\\^$*+?.():=!|{}\-\[\]]/g, function(arg) { return '\\' + arg; });
|
||||
let extension = opts.extension.replace(/[\\^$*+?.():=!|{}\-\[\]]/g, function(arg) { return '\\' + arg; });
|
||||
extension = new RegExp('\\.' + extension + '$');
|
||||
|
||||
var output = new SourceNode();
|
||||
let output = new SourceNode();
|
||||
if (!opts.simple) {
|
||||
if (opts.amd) {
|
||||
output.add('define([\'' + opts.handlebarPath + 'handlebars.runtime\'], function(Handlebars) {\n Handlebars = Handlebars["default"];');
|
||||
@@ -66,24 +64,24 @@ module.exports.cli = function(opts) {
|
||||
output.add('{};\n');
|
||||
}
|
||||
function processTemplate(template, root) {
|
||||
var path = template,
|
||||
let path = template,
|
||||
stat = fs.statSync(path);
|
||||
if (stat.isDirectory()) {
|
||||
fs.readdirSync(template).map(function(file) {
|
||||
var childPath = template + '/' + file;
|
||||
let childPath = template + '/' + file;
|
||||
|
||||
if (extension.test(childPath) || fs.statSync(childPath).isDirectory()) {
|
||||
processTemplate(childPath, root || template);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
var data = fs.readFileSync(path, 'utf8');
|
||||
let data = fs.readFileSync(path, 'utf8');
|
||||
|
||||
if (opts.bom && data.indexOf('\uFEFF') === 0) {
|
||||
data = data.substring(1);
|
||||
}
|
||||
|
||||
var options = {
|
||||
let options = {
|
||||
knownHelpers: known,
|
||||
knownHelpersOnly: opts.o
|
||||
};
|
||||
@@ -103,11 +101,11 @@ module.exports.cli = function(opts) {
|
||||
}
|
||||
template = template.replace(extension, '');
|
||||
|
||||
var precompiled = Handlebars.precompile(data, options);
|
||||
let precompiled = Handlebars.precompile(data, options);
|
||||
|
||||
// If we are generating a source map, we have to reconstruct the SourceNode object
|
||||
if (opts.map) {
|
||||
var consumer = new SourceMapConsumer(precompiled.map);
|
||||
let consumer = new SourceMapConsumer(precompiled.map);
|
||||
precompiled = SourceNode.fromStringWithSourceMap(precompiled.code, consumer);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ describe('precompiler', function() {
|
||||
}
|
||||
|
||||
var Handlebars = require('../lib'),
|
||||
Precompiler = require('../lib/precompiler'),
|
||||
Precompiler = require('../dist/cjs/precompiler'),
|
||||
fs = require('fs'),
|
||||
uglify = require('uglify-js');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user