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
2 changes: 1 addition & 1 deletion .oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
}
},
{
"files": ["tests/bench/**/*.mjs"],
"files": ["tests/bench/**/*.js"],
"rules": {
"no-console": "off"
}
Expand Down
8 changes: 0 additions & 8 deletions .swcrc

This file was deleted.

26 changes: 3 additions & 23 deletions bin/handlebars.mjs → bin/handlebars.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#!/usr/bin/env node

import { createRequire } from 'node:module';
import { loadTemplates, cli } from '../lib/precompiler.js';
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)
Expand All @@ -19,23 +16,6 @@ const parser = yargs(process.argv.slice(2))
type: 'string',
description: 'Source Map File',
})
.option('a', {
type: 'boolean',
description: 'Exports amd style (require.js)',
alias: 'amd',
})
.option('c', {
type: 'string',
description: 'Exports CommonJS style, path to Handlebars module',
alias: 'commonjs',
default: null,
})
.option('h', {
type: 'string',
description: 'Path to handlebar.js (only valid for amd-style)',
alias: 'handlebarPath',
default: '',
})
.option('k', {
type: 'string',
description: 'Known helpers',
Expand Down Expand Up @@ -117,7 +97,7 @@ const argv = parser.parseSync();
argv.files = argv._;
delete argv._;

Precompiler.loadTemplates(argv, function (err, opts) {
loadTemplates(argv, function (err, opts) {
if (err) {
throw err;
}
Expand All @@ -127,7 +107,7 @@ Precompiler.loadTemplates(argv, function (err, opts) {
} else {
// 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) => {
Promise.resolve(cli(opts)).catch((error) => {
process.nextTick(() => {
throw error;
});
Expand Down
File renamed without changes.
14 changes: 9 additions & 5 deletions lib/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import {
Visitor,
} from '@handlebars/parser';

import runtime from './handlebars.runtime';
import runtime from './handlebars.runtime.js';

// Compiler imports
import AST from './handlebars/compiler/ast';
import { Compiler, compile, precompile } from './handlebars/compiler/compiler';
import JavaScriptCompiler from './handlebars/compiler/javascript-compiler';
import AST from './handlebars/compiler/ast.js';
import {
Compiler,
compile,
precompile,
} from './handlebars/compiler/compiler.js';
import JavaScriptCompiler from './handlebars/compiler/javascript-compiler.js';

import noConflict from './handlebars/no-conflict';
import noConflict from './handlebars/no-conflict.js';

let _create = runtime.create;
function create() {
Expand Down
23 changes: 17 additions & 6 deletions lib/handlebars.runtime.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Exception } from '@handlebars/parser';
import * as base from './handlebars/base';
import * as base from './handlebars/base.js';

// Each of these augment the Handlebars object. No need to setup here.
// (This is done to easily share code between commonjs and browse envs)
import SafeString from './handlebars/safe-string';
import * as Utils from './handlebars/utils';
import * as runtime from './handlebars/runtime';
// (This is done to easily share code between module systems and browser envs)
import SafeString from './handlebars/safe-string.js';
import * as Utils from './handlebars/utils.js';
import * as runtime from './handlebars/runtime.js';

import noConflict from './handlebars/no-conflict';
import noConflict from './handlebars/no-conflict.js';

// For compatibility and usage outside of module systems, make the Handlebars object a namespace
function create() {
Expand Down Expand Up @@ -37,4 +37,15 @@ noConflict(inst);

inst['default'] = inst;

// Named re-exports for CJS interop.
// See the comment in lib/index.js for the full explanation. In short:
// require('handlebars/runtime').COMPILER_REVISION must be directly accessible
// for tools like handlebars-loader that compare compiler and runtime revisions.
export {
VERSION,
COMPILER_REVISION,
LAST_COMPATIBLE_COMPILER_REVISION,
REVISION_CHANGES,
} from './handlebars/base.js';

export default inst;
10 changes: 5 additions & 5 deletions lib/handlebars/base.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Exception } from '@handlebars/parser';
import { createFrame, extend, toString } from './utils';
import { registerDefaultHelpers } from './helpers';
import { registerDefaultDecorators } from './decorators';
import logger from './logger';
import { resetLoggedProperties } from './internal/proto-access';
import { createFrame, extend, toString } from './utils.js';
import { registerDefaultHelpers } from './helpers.js';
import { registerDefaultDecorators } from './decorators.js';
import logger from './logger.js';
import { resetLoggedProperties } from './internal/proto-access.js';

export const VERSION = '4.7.7';
export const COMPILER_REVISION = 8;
Expand Down
55 changes: 2 additions & 53 deletions lib/handlebars/compiler/code-gen.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,5 @@
/* global define */
import { isArray } from '../utils';

let SourceNode;

try {
/* v8 ignore next */
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.
// 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
} catch (err) {
/* NOP */
}

/* v8 ignore next -- tested but not covered due to dist build */
if (!SourceNode) {
SourceNode = function (line, column, srcFile, chunks) {
this.src = '';
if (chunks) {
this.add(chunks);
}
};
/* v8 ignore next */
SourceNode.prototype = {
add: function (chunks) {
if (isArray(chunks)) {
chunks = chunks.join('');
}
this.src += chunks;
},
prepend: function (chunks) {
if (isArray(chunks)) {
chunks = chunks.join('');
}
this.src = chunks + this.src;
},
toStringWithSourceMap: function () {
return { code: this.toString() };
},
toString: function () {
return this.src;
},
};
}
import { isArray } from '../utils.js';
import { SourceNode } from '#source-node';

function castChunk(chunk, codeGen, loc) {
if (isArray(chunk)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/handlebars/compiler/compiler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Exception } from '@handlebars/parser';
import { isArray, indexOf, extend } from '../utils';
import AST from './ast';
import { isArray, indexOf, extend } from '../utils.js';
import AST from './ast.js';

const slice = [].slice;

Expand Down
34 changes: 24 additions & 10 deletions lib/handlebars/compiler/javascript-compiler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Exception } from '@handlebars/parser';
import { COMPILER_REVISION, REVISION_CHANGES } from '../base';
import { isArray } from '../utils';
import CodeGen from './code-gen';
import { COMPILER_REVISION, REVISION_CHANGES } from '../base.js';
import { isArray } from '../utils.js';
import CodeGen from './code-gen.js';

function Literal(value) {
this.value = value;
Expand Down Expand Up @@ -1175,20 +1175,34 @@ function strictLookup(requireTerminal, compiler, parts, startPartIndex, type) {
stack = compiler.nameLookup(stack, parts[i], type);
}

if (requireTerminal) {
if (!requireTerminal) {
return stack;
}

if (startPartIndex > len) {
// Compat mode already consumed all parts via depthedLookup; the stack
// holds the resolved value, not an object to look the property up on.
// Use container.strictLookup to traverse depths with a strict error on miss.
return [
compiler.aliasable('container.strict'),
'(',
stack,
', ',
compiler.aliasable('container.strictLookup'),
'(depths, ',
compiler.quotedString(parts[len]),
', ',
JSON.stringify(compiler.source.currentLocation),
' )',
];
} else {
return stack;
}

return [
compiler.aliasable('container.strict'),
'(',
stack,
', ',
compiler.quotedString(parts[len]),
', ',
JSON.stringify(compiler.source.currentLocation),
' )',
];
}

export default JavaScriptCompiler;
31 changes: 31 additions & 0 deletions lib/handlebars/compiler/source-node.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { isArray } from '../utils.js';

// Lightweight stub for browser environments where the source-map package
// (which depends on Node.js built-ins) is not available.
export function SourceNode(line, column, srcFile, chunks) {
this.src = '';
if (chunks) {
this.add(chunks);
}
}

SourceNode.prototype = {
add(chunks) {
if (isArray(chunks)) {
chunks = chunks.join('');
}
this.src += chunks;
},
prepend(chunks) {
if (isArray(chunks)) {
chunks = chunks.join('');
}
this.src = chunks + this.src;
},
toStringWithSourceMap() {
return { code: this.toString() };
},
toString() {
return this.src;
},
};
1 change: 1 addition & 0 deletions lib/handlebars/compiler/source-node.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { SourceNode } from 'source-map';
2 changes: 1 addition & 1 deletion lib/handlebars/decorators.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import registerInline from './decorators/inline';
import registerInline from './decorators/inline.js';

export function registerDefaultDecorators(instance) {
registerInline(instance);
Expand Down
2 changes: 1 addition & 1 deletion lib/handlebars/decorators/inline.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extend } from '../utils';
import { extend } from '../utils.js';

export default function (instance) {
instance.registerDecorator(
Expand Down
14 changes: 7 additions & 7 deletions lib/handlebars/helpers.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import registerBlockHelperMissing from './helpers/block-helper-missing';
import registerEach from './helpers/each';
import registerHelperMissing from './helpers/helper-missing';
import registerIf from './helpers/if';
import registerLog from './helpers/log';
import registerLookup from './helpers/lookup';
import registerWith from './helpers/with';
import registerBlockHelperMissing from './helpers/block-helper-missing.js';
import registerEach from './helpers/each.js';
import registerHelperMissing from './helpers/helper-missing.js';
import registerIf from './helpers/if.js';
import registerLog from './helpers/log.js';
import registerLookup from './helpers/lookup.js';
import registerWith from './helpers/with.js';

export function registerDefaultHelpers(instance) {
registerBlockHelperMissing(instance);
Expand Down
2 changes: 1 addition & 1 deletion lib/handlebars/helpers/block-helper-missing.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray } from '../utils';
import { isArray } from '../utils.js';

export default function (instance) {
instance.registerHelper('blockHelperMissing', function (context, options) {
Expand Down
2 changes: 1 addition & 1 deletion lib/handlebars/helpers/each.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Exception } from '@handlebars/parser';
import { createFrame, isArray, isFunction, isMap, isSet } from '../utils';
import { createFrame, isArray, isFunction, isMap, isSet } from '../utils.js';

export default function (instance) {
instance.registerHelper('each', function (context, options) {
Expand Down
2 changes: 1 addition & 1 deletion lib/handlebars/helpers/if.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Exception } from '@handlebars/parser';
import { isEmpty, isFunction } from '../utils';
import { isEmpty, isFunction } from '../utils.js';

export default function (instance) {
instance.registerHelper('if', function (conditional, options) {
Expand Down
2 changes: 1 addition & 1 deletion lib/handlebars/helpers/with.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Exception } from '@handlebars/parser';
import { isEmpty, isFunction } from '../utils';
import { isEmpty, isFunction } from '../utils.js';

export default function (instance) {
instance.registerHelper('with', function (context, options) {
Expand Down
4 changes: 2 additions & 2 deletions lib/handlebars/internal/proto-access.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { extend } from '../utils';
import logger from '../logger';
import { extend } from '../utils.js';
import logger from '../logger.js';

const loggedProperties = Object.create(null);

Expand Down
2 changes: 1 addition & 1 deletion lib/handlebars/logger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { indexOf } from './utils';
import { indexOf } from './utils.js';

let logger = {
methodMap: ['debug', 'info', 'warn', 'error'],
Expand Down
Loading