Skip to content
Merged
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ npm run bench
# Run with a custom label
npm run bench -- --label my-optimization

# Filter templates by name (regex, case-insensitive)
npm run bench -- --grep "complex|recursive"

# Run only specific sections (regex, case-insensitive)
npm run bench -- --section precompil
npm run bench -- --section "compilation|precompil"

# Compare results
npm run bench:compare

Expand Down
23 changes: 18 additions & 5 deletions bin/handlebars.js → bin/handlebars.mjs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#!/usr/bin/env node

const yargs = require('yargs')
import { createRequire } from 'node:module';
import yargs from 'yargs';

const require = createRequire(import.meta.url);
const Precompiler = require('../dist/cjs/precompiler');

const parser = yargs(process.argv.slice(2))
.usage('Precompile handlebar templates.\nUsage: $0 [template|directory]...')
.help(false)
.version(false)
.option('f', {
type: 'string',
description: 'Output File',
Expand Down Expand Up @@ -105,19 +113,24 @@ const yargs = require('yargs')
})
.wrap(120);

const argv = yargs.argv;
const argv = parser.parseSync();
argv.files = argv._;
delete argv._;

const Precompiler = require('../dist/cjs/precompiler');
Precompiler.loadTemplates(argv, function (err, opts) {
if (err) {
throw err;
}

if (opts.help || (!opts.templates.length && !opts.version)) {
yargs.showHelp();
parser.showHelp('log');
} else {
Precompiler.cli(opts);
// cli() is async (returns a Promise), so errors would become unhandled
// rejections. Re-throw via nextTick to surface them as uncaught exceptions.
Promise.resolve(Precompiler.cli(opts)).catch((error) => {
process.nextTick(() => {
throw error;
});
});
}
});
7 changes: 6 additions & 1 deletion lib/handlebars/compiler/code-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ try {
if (typeof define !== 'function' || !define.amd) {
// We don't support this in AMD environments. For these environments, we assume that
// they are running on the browser and thus have no need for the source-map library.
let SourceMap = require('source-map');
// The variable indirection prevents bundlers from statically resolving and bundling
// source-map (which requires Node-built-ins). The stub SourceNode below handles
// browser/bundled usage. Bundlers may emit a "Critical dependency" warning — this
// is expected and harmless.
let mod = 'source-map';
let SourceMap = require(mod);
SourceNode = SourceMap.SourceNode;
}
// oxlint-disable-next-line no-unused-vars -- Babel 5 requires named catch param
Expand Down
9 changes: 5 additions & 4 deletions lib/precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function loadFiles(opts, callback) {
);
}

module.exports.cli = function (opts) {
module.exports.cli = async function (opts) {
if (opts.version) {
console.log(Handlebars.VERSION);
return;
Expand Down Expand Up @@ -221,7 +221,7 @@ module.exports.cli = function (opts) {
output.add('{};\n');
}

opts.templates.forEach(function (template) {
for (const template of opts.templates) {
let options = {
knownHelpers: known,
knownHelpersOnly: opts.o,
Expand All @@ -238,11 +238,12 @@ module.exports.cli = function (opts) {

// If we are generating a source map, we have to reconstruct the SourceNode object
if (opts.map) {
let consumer = new SourceMapConsumer(precompiled.map);
let consumer = await new SourceMapConsumer(precompiled.map);
precompiled = SourceNode.fromStringWithSourceMap(
precompiled.code,
consumer
);
consumer.destroy();
}

if (opts.simple) {
Expand All @@ -264,7 +265,7 @@ module.exports.cli = function (opts) {
');\n',
]);
}
});
}

// Output the content
if (!opts.simple) {
Expand Down
Loading