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

Allow importing library as ESM #2004

Closed
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
},
parserOptions: {
sourceType: 'module',
ecmaVersion: 2022,
},
rules: {
'no-console': 'warn',
Expand Down
6 changes: 3 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module.exports = function (grunt) {
{
cwd: 'lib/',
expand: true,
src: '**/!(index).js',
src: '**/*.js',
dest: 'dist/cjs/',
},
],
Expand All @@ -65,13 +65,13 @@ module.exports = function (grunt) {
},
},
handlebars: {
entry: './dist/cjs/handlebars.js',
entry: './dist/cjs/handlebars/index.js',
output: {
filename: 'handlebars.js',
},
},
runtime: {
entry: './dist/cjs/handlebars.runtime.js',
entry: './dist/cjs/handlebars/index.runtime.js',
output: {
filename: 'handlebars.runtime.js',
},
Expand Down
File renamed without changes.
14 changes: 8 additions & 6 deletions lib/handlebars/base.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
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 * as parser from '@handlebars/parser';
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';

const Exception = parser.Exception ?? parser.default.Exception;

export const VERSION = '4.7.7';
export const COMPILER_REVISION = 8;
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 */
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'); // eslint-disable-line no-undef
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
8 changes: 5 additions & 3 deletions lib/handlebars/compiler/compiler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-disable new-cap */

import { Exception } from '@handlebars/parser';
import { isArray, indexOf, extend } from '../utils';
import AST from './ast';
import * as parser from '@handlebars/parser';
import { isArray, indexOf, extend } from '../utils.js';
import AST from './ast.js';

const Exception = parser.Exception ?? parser.default.Exception;

const slice = [].slice;

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

const Exception = parser.Exception ?? parser.default.Exception;

function Literal(value) {
this.value = value;
Expand Down
32 changes: 32 additions & 0 deletions lib/handlebars/compiler/source-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { isArray } from '../utils.js';

/* istanbul ignore if: tested but not covered in istanbul due to dist build */
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/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
6 changes: 4 additions & 2 deletions lib/handlebars/helpers/each.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Exception } from '@handlebars/parser';
import { createFrame, isArray, isFunction, isMap, isSet } from '../utils';
import * as parser from '@handlebars/parser';
import { createFrame, isArray, isFunction, isMap, isSet } from '../utils.js';

const Exception = parser.Exception ?? parser.default.Exception;

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

const Exception = parser.Exception ?? parser.default.Exception;

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

const Exception = parser.Exception ?? parser.default.Exception;

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

const Exception = parser.Exception ?? parser.default.Exception;

export default function (instance) {
instance.registerHelper('with', function (context, options) {
Expand Down
22 changes: 11 additions & 11 deletions lib/handlebars.js → lib/handlebars/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {
parser as Parser,
parse,
parseWithoutProcessing,
Visitor,
} from '@handlebars/parser';
import * as parser from '@handlebars/parser';

import runtime from './handlebars.runtime';
const { parse, parseWithoutProcessing } = parser;

const Parser = parser.parser ?? parser.default.parser;
const Visitor = parser.Visitor ?? parser.default.Visitor;

import runtime from './index.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 './compiler/ast.js';
import { Compiler, compile, precompile } from './compiler/compiler.js';
import JavaScriptCompiler from './compiler/javascript-compiler.js';

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

let _create = runtime.create;
function create() {
Expand Down
18 changes: 11 additions & 7 deletions lib/handlebars.runtime.js → lib/handlebars/index.runtime.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Exception } from '@handlebars/parser';
import * as base from './handlebars/base';
import * as parser from '@handlebars/parser';
import * as base from './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';
import SafeString from './safe-string.js';
import * as Utils from './utils.js';
import * as runtime from './runtime.js';

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

const Exception = parser.Exception ?? parser.default.Exception;

const vm = Utils.extend({}, runtime);

// For compatibility and usage outside of module systems, make the Handlebars object a namespace
function create() {
Expand All @@ -19,7 +23,7 @@ function create() {
hb.Utils = Utils;
hb.escapeExpression = Utils.escapeExpression;

hb.VM = runtime;
hb.VM = vm;
hb.template = function (spec) {
return runtime.template(spec, hb);
};
Expand Down
2 changes: 1 addition & 1 deletion lib/handlebars/internal/create-new-lookup-object.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extend } from '../utils';
import { extend } from '../utils.js';

/**
* Create a new object with "null"-prototype to avoid truthy results on prototype properties.
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 { createNewLookupObject } from './create-new-lookup-object';
import logger from '../logger';
import { createNewLookupObject } from './create-new-lookup-object.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
14 changes: 8 additions & 6 deletions lib/handlebars/runtime.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { Exception } from '@handlebars/parser';
import * as Utils from './utils';
import * as parser from '@handlebars/parser';
import * as Utils from './utils.js';
import {
COMPILER_REVISION,
createFrame,
LAST_COMPATIBLE_COMPILER_REVISION,
REVISION_CHANGES,
} from './base';
import { moveHelperToHooks } from './helpers';
import { wrapHelper } from './internal/wrapHelper';
} from './base.js';
import { moveHelperToHooks } from './helpers.js';
import { wrapHelper } from './internal/wrapHelper.js';
import {
createProtoAccessControl,
resultIsAllowed,
} from './internal/proto-access';
} from './internal/proto-access.js';

const Exception = parser.Exception ?? parser.default.Exception;

export function checkRevision(compilerInfo) {
const compilerRevision = (compilerInfo && compilerInfo[0]) || 1,
Expand Down
2 changes: 2 additions & 0 deletions lib/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-env node */
module.exports = require('../dist/cjs/index.js').default;
29 changes: 5 additions & 24 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
// USAGE:
// var handlebars = require('handlebars');
/* eslint-env node */
/* eslint-disable no-var */
import handlebars from './handlebars/index.js';
import { PrintVisitor, print } from '@handlebars/parser';

// var local = handlebars.create();
handlebars.PrintVisitor = PrintVisitor;
handlebars.print = print;

var handlebars = require('../dist/cjs/handlebars')['default'];

var parser = require('@handlebars/parser');
handlebars.PrintVisitor = parser.PrintVisitor;
handlebars.print = parser.print;

module.exports = handlebars;

// Publish a Node.js require() handler for .handlebars and .hbs files
function extension(module, filename) {
var fs = require('fs');
var templateString = fs.readFileSync(filename, 'utf8');
module.exports = handlebars.compile(templateString);
}
/* istanbul ignore else */
if (typeof require !== 'undefined' && require.extensions) {
require.extensions['.handlebars'] = extension;
require.extensions['.hbs'] = extension;
}
export default handlebars;
2 changes: 2 additions & 0 deletions lib/index.runtime.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-env node */
module.exports = require('../dist/cjs/handlebars/index.runtime.js').default;
9 changes: 9 additions & 0 deletions lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "module",
"imports": {
"#source-map": {
"node": "source-map",
"default": "./handlebars/compiler/source-map.js"
}
}
}
Loading
Loading