Skip to content

Commit

Permalink
Allow importing library as ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
mohd-akram committed Sep 19, 2023
1 parent e497a35 commit c9ab70a
Show file tree
Hide file tree
Showing 33 changed files with 254 additions and 193 deletions.
12 changes: 9 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = function(grunt) {
{
expand: true,
cwd: 'lib/',
src: '**/!(index).js',
src: ['**/*.js', '!index.js'],
dest: 'dist/amd/'
}
]
Expand All @@ -69,7 +69,7 @@ module.exports = function(grunt) {
{
cwd: 'lib/',
expand: true,
src: '**/!(index).js',
src: ['**/*.js', '!index.js'],
dest: 'dist/cjs/'
}
]
Expand All @@ -89,6 +89,11 @@ module.exports = function(grunt) {
}
]
},
resolve: {
alias: {
'source-map$': __dirname + '/lib/handlebars/compiler/source-map.js'
}
},
output: {
path: 'dist/',
library: 'Handlebars',
Expand All @@ -112,7 +117,8 @@ module.exports = function(grunt) {
requirejs: {
options: {
optimize: 'none',
baseUrl: 'dist/amd/'
baseUrl: 'dist/amd/',
paths: { 'source-map': 'handlebars/compiler/source-map' }
},
dist: {
options: {
Expand Down
48 changes: 2 additions & 46 deletions lib/handlebars.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,2 @@
import runtime from './handlebars.runtime';

// Compiler imports
import AST from './handlebars/compiler/ast';
import {
parser as Parser,
parse,
parseWithoutProcessing
} from './handlebars/compiler/base';
import { Compiler, compile, precompile } from './handlebars/compiler/compiler';
import JavaScriptCompiler from './handlebars/compiler/javascript-compiler';
import Visitor from './handlebars/compiler/visitor';

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

let _create = runtime.create;
function create() {
let hb = _create();

hb.compile = function(input, options) {
return compile(input, options, hb);
};
hb.precompile = function(input, options) {
return precompile(input, options, hb);
};

hb.AST = AST;
hb.Compiler = Compiler;
hb.JavaScriptCompiler = JavaScriptCompiler;
hb.Parser = Parser;
hb.parse = parse;
hb.parseWithoutProcessing = parseWithoutProcessing;

return hb;
}

let inst = create();
inst.create = create;

noConflict(inst);

inst.Visitor = Visitor;

inst['default'] = inst;

export default inst;
import handlebars from './handlebars/index.js';
export default handlebars;
39 changes: 2 additions & 37 deletions lib/handlebars.runtime.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,2 @@
import * as base from './handlebars/base';

// 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 Exception from './handlebars/exception';
import * as Utils from './handlebars/utils';
import * as runtime from './handlebars/runtime';

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

// For compatibility and usage outside of module systems, make the Handlebars object a namespace
function create() {
let hb = new base.HandlebarsEnvironment();

Utils.extend(hb, base);
hb.SafeString = SafeString;
hb.Exception = Exception;
hb.Utils = Utils;
hb.escapeExpression = Utils.escapeExpression;

hb.VM = runtime;
hb.template = function(spec) {
return runtime.template(spec, hb);
};

return hb;
}

let inst = create();
inst.create = create;

noConflict(inst);

inst['default'] = inst;

export default inst;
import handlebars from './handlebars/index.runtime.js';
export default handlebars;
12 changes: 6 additions & 6 deletions lib/handlebars/base.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createFrame, extend, toString } from './utils';
import Exception from './exception';
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 Exception from './exception.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.8';
export const COMPILER_REVISION = 8;
Expand Down
8 changes: 4 additions & 4 deletions lib/handlebars/compiler/base.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import parser from './parser';
import WhitespaceControl from './whitespace-control';
import * as Helpers from './helpers';
import { extend } from '../utils';
import parser from './parser.js';
import WhitespaceControl from './whitespace-control.js';
import * as Helpers from './helpers.js';
import { extend } from '../utils.js';

export { parser };

Expand Down
48 changes: 2 additions & 46 deletions lib/handlebars/compiler/code-gen.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,6 @@
/* global define, require */
import { isArray } from '../utils';

let SourceNode;

try {
/* istanbul 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.
let SourceMap = require('source-map');
SourceNode = SourceMap.SourceNode;
}
} catch (err) {
/* NOP */
}
import { SourceNode } from 'source-map';

/* istanbul ignore if: tested but not covered in istanbul due to dist build */
if (!SourceNode) {
SourceNode = function(line, column, srcFile, chunks) {
this.src = '';
if (chunks) {
this.add(chunks);
}
};
/* istanbul 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';

function castChunk(chunk, codeGen, loc) {
if (isArray(chunk)) {
Expand Down
6 changes: 3 additions & 3 deletions lib/handlebars/compiler/compiler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable new-cap */

import Exception from '../exception';
import { isArray, indexOf, extend } from '../utils';
import AST from './ast';
import Exception from '../exception.js';
import { isArray, indexOf, extend } from '../utils.js';
import AST from './ast.js';

const slice = [].slice;

Expand Down
2 changes: 1 addition & 1 deletion lib/handlebars/compiler/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Exception from '../exception';
import Exception from '../exception.js';

function validateClose(open, close) {
close = close.path ? close.path.original : close;
Expand Down
8 changes: 4 additions & 4 deletions lib/handlebars/compiler/javascript-compiler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { COMPILER_REVISION, REVISION_CHANGES } from '../base';
import Exception from '../exception';
import { isArray } from '../utils';
import CodeGen from './code-gen';
import { COMPILER_REVISION, REVISION_CHANGES } from '../base.js';
import Exception from '../exception.js';
import { isArray } from '../utils.js';
import CodeGen from './code-gen.js';

function Literal(value) {
this.value = value;
Expand Down
2 changes: 1 addition & 1 deletion lib/handlebars/compiler/printer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable new-cap */
import Visitor from './visitor';
import Visitor from './visitor.js';

export function print(ast) {
return new PrintVisitor().accept(ast);
Expand Down
39 changes: 39 additions & 0 deletions lib/handlebars/compiler/source-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* istanbul ignore next */
export const isArray =
Array.isArray ||
function(value) {
return value && typeof value === 'object'
? toString.call(value) === '[object Array]'
: false;
};

const SourceNode = function(line, column, srcFile, chunks) {
this.src = '';
if (chunks) {
this.add(chunks);
}
};

/* istanbul 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;
}
};

export { SourceNode };
2 changes: 1 addition & 1 deletion lib/handlebars/compiler/visitor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Exception from '../exception';
import Exception from '../exception.js';

function Visitor() {
this.parents = [];
Expand Down
2 changes: 1 addition & 1 deletion lib/handlebars/compiler/whitespace-control.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Visitor from './visitor';
import Visitor from './visitor.js';

function WhitespaceControl(options = {}) {
this.options = options;
Expand Down
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('inline', function(fn, props, container, options) {
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 { appendContextPath, createFrame, isArray } from '../utils';
import { appendContextPath, createFrame, isArray } from '../utils.js';

export default function(instance) {
instance.registerHelper('blockHelperMissing', function(context, options) {
Expand Down
4 changes: 2 additions & 2 deletions lib/handlebars/helpers/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
createFrame,
isArray,
isFunction
} from '../utils';
import Exception from '../exception';
} from '../utils.js';
import Exception from '../exception.js';

export default function(instance) {
instance.registerHelper('each', function(context, options) {
Expand Down
2 changes: 1 addition & 1 deletion lib/handlebars/helpers/helper-missing.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Exception from '../exception';
import Exception from '../exception.js';

export default function(instance) {
instance.registerHelper('helperMissing', function(/* [args, ]options */) {
Expand Down
4 changes: 2 additions & 2 deletions lib/handlebars/helpers/if.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isEmpty, isFunction } from '../utils';
import Exception from '../exception';
import { isEmpty, isFunction } from '../utils.js';
import Exception from '../exception.js';

export default function(instance) {
instance.registerHelper('if', function(conditional, options) {
Expand Down
4 changes: 2 additions & 2 deletions lib/handlebars/helpers/with.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
createFrame,
isEmpty,
isFunction
} from '../utils';
import Exception from '../exception';
} from '../utils.js';
import Exception from '../exception.js';

export default function(instance) {
instance.registerHelper('with', function(context, options) {
Expand Down

0 comments on commit c9ab70a

Please sign in to comment.