Skip to content

Commit 0e1d355

Browse files
authored
Switch to ESM (#2138)
1 parent b10cec2 commit 0e1d355

85 files changed

Lines changed: 2733 additions & 9647 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.oxlintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
}
117117
},
118118
{
119-
"files": ["tests/bench/**/*.mjs"],
119+
"files": ["tests/bench/**/*.js"],
120120
"rules": {
121121
"no-console": "off"
122122
}

.swcrc

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
#!/usr/bin/env node
22

3-
import { createRequire } from 'node:module';
3+
import { loadTemplates, cli } from '../lib/precompiler.js';
44
import yargs from 'yargs';
55

6-
const require = createRequire(import.meta.url);
7-
const Precompiler = require('../dist/cjs/precompiler');
8-
96
const parser = yargs(process.argv.slice(2))
107
.usage('Precompile handlebar templates.\nUsage: $0 [template|directory]...')
118
.help(false)
@@ -19,23 +16,6 @@ const parser = yargs(process.argv.slice(2))
1916
type: 'string',
2017
description: 'Source Map File',
2118
})
22-
.option('a', {
23-
type: 'boolean',
24-
description: 'Exports amd style (require.js)',
25-
alias: 'amd',
26-
})
27-
.option('c', {
28-
type: 'string',
29-
description: 'Exports CommonJS style, path to Handlebars module',
30-
alias: 'commonjs',
31-
default: null,
32-
})
33-
.option('h', {
34-
type: 'string',
35-
description: 'Path to handlebar.js (only valid for amd-style)',
36-
alias: 'handlebarPath',
37-
default: '',
38-
})
3919
.option('k', {
4020
type: 'string',
4121
description: 'Known helpers',
@@ -117,7 +97,7 @@ const argv = parser.parseSync();
11797
argv.files = argv._;
11898
delete argv._;
11999

120-
Precompiler.loadTemplates(argv, function (err, opts) {
100+
loadTemplates(argv, function (err, opts) {
121101
if (err) {
122102
throw err;
123103
}
@@ -127,7 +107,7 @@ Precompiler.loadTemplates(argv, function (err, opts) {
127107
} else {
128108
// cli() is async (returns a Promise), so errors would become unhandled
129109
// rejections. Re-throw via nextTick to surface them as uncaught exceptions.
130-
Promise.resolve(Precompiler.cli(opts)).catch((error) => {
110+
Promise.resolve(cli(opts)).catch((error) => {
131111
process.nextTick(() => {
132112
throw error;
133113
});
File renamed without changes.

lib/handlebars.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ import {
55
Visitor,
66
} from '@handlebars/parser';
77

8-
import runtime from './handlebars.runtime';
8+
import runtime from './handlebars.runtime.js';
99

1010
// Compiler imports
11-
import AST from './handlebars/compiler/ast';
12-
import { Compiler, compile, precompile } from './handlebars/compiler/compiler';
13-
import JavaScriptCompiler from './handlebars/compiler/javascript-compiler';
11+
import AST from './handlebars/compiler/ast.js';
12+
import {
13+
Compiler,
14+
compile,
15+
precompile,
16+
} from './handlebars/compiler/compiler.js';
17+
import JavaScriptCompiler from './handlebars/compiler/javascript-compiler.js';
1418

15-
import noConflict from './handlebars/no-conflict';
19+
import noConflict from './handlebars/no-conflict.js';
1620

1721
let _create = runtime.create;
1822
function create() {

lib/handlebars.runtime.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Exception } from '@handlebars/parser';
2-
import * as base from './handlebars/base';
2+
import * as base from './handlebars/base.js';
33

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

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

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

3838
inst['default'] = inst;
3939

40+
// Named re-exports for CJS interop.
41+
// See the comment in lib/index.js for the full explanation. In short:
42+
// require('handlebars/runtime').COMPILER_REVISION must be directly accessible
43+
// for tools like handlebars-loader that compare compiler and runtime revisions.
44+
export {
45+
VERSION,
46+
COMPILER_REVISION,
47+
LAST_COMPATIBLE_COMPILER_REVISION,
48+
REVISION_CHANGES,
49+
} from './handlebars/base.js';
50+
4051
export default inst;

lib/handlebars/base.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Exception } from '@handlebars/parser';
2-
import { createFrame, extend, toString } from './utils';
3-
import { registerDefaultHelpers } from './helpers';
4-
import { registerDefaultDecorators } from './decorators';
5-
import logger from './logger';
6-
import { resetLoggedProperties } from './internal/proto-access';
2+
import { createFrame, extend, toString } from './utils.js';
3+
import { registerDefaultHelpers } from './helpers.js';
4+
import { registerDefaultDecorators } from './decorators.js';
5+
import logger from './logger.js';
6+
import { resetLoggedProperties } from './internal/proto-access.js';
77

88
export const VERSION = '4.7.7';
99
export const COMPILER_REVISION = 8;

lib/handlebars/compiler/code-gen.js

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,5 @@
1-
/* global define */
2-
import { isArray } from '../utils';
3-
4-
let SourceNode;
5-
6-
try {
7-
/* v8 ignore next */
8-
if (typeof define !== 'function' || !define.amd) {
9-
// We don't support this in AMD environments. For these environments, we assume that
10-
// they are running on the browser and thus have no need for the source-map library.
11-
// The variable indirection prevents bundlers from statically resolving and bundling
12-
// source-map (which requires Node-built-ins). The stub SourceNode below handles
13-
// browser/bundled usage. Bundlers may emit a "Critical dependency" warning — this
14-
// is expected and harmless.
15-
let mod = 'source-map';
16-
let SourceMap = require(mod);
17-
SourceNode = SourceMap.SourceNode;
18-
}
19-
// oxlint-disable-next-line no-unused-vars -- Babel 5 requires named catch param
20-
} catch (err) {
21-
/* NOP */
22-
}
23-
24-
/* v8 ignore next -- tested but not covered due to dist build */
25-
if (!SourceNode) {
26-
SourceNode = function (line, column, srcFile, chunks) {
27-
this.src = '';
28-
if (chunks) {
29-
this.add(chunks);
30-
}
31-
};
32-
/* v8 ignore next */
33-
SourceNode.prototype = {
34-
add: function (chunks) {
35-
if (isArray(chunks)) {
36-
chunks = chunks.join('');
37-
}
38-
this.src += chunks;
39-
},
40-
prepend: function (chunks) {
41-
if (isArray(chunks)) {
42-
chunks = chunks.join('');
43-
}
44-
this.src = chunks + this.src;
45-
},
46-
toStringWithSourceMap: function () {
47-
return { code: this.toString() };
48-
},
49-
toString: function () {
50-
return this.src;
51-
},
52-
};
53-
}
1+
import { isArray } from '../utils.js';
2+
import { SourceNode } from '#source-node';
543

554
function castChunk(chunk, codeGen, loc) {
565
if (isArray(chunk)) {

lib/handlebars/compiler/compiler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Exception } from '@handlebars/parser';
2-
import { isArray, indexOf, extend } from '../utils';
3-
import AST from './ast';
2+
import { isArray, indexOf, extend } from '../utils.js';
3+
import AST from './ast.js';
44

55
const slice = [].slice;
66

lib/handlebars/compiler/javascript-compiler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Exception } from '@handlebars/parser';
2-
import { COMPILER_REVISION, REVISION_CHANGES } from '../base';
3-
import { isArray } from '../utils';
4-
import CodeGen from './code-gen';
2+
import { COMPILER_REVISION, REVISION_CHANGES } from '../base.js';
3+
import { isArray } from '../utils.js';
4+
import CodeGen from './code-gen.js';
55

66
function Literal(value) {
77
this.value = value;

0 commit comments

Comments
 (0)