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

Converted to ESM + added dist script for CJS support #62

Merged
merged 15 commits into from
Oct 6, 2023
Merged
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
node_modules
npm-debug.log

# Built files for commonJS portability
index.cjs
voidElements.cjs

# Built package
/*.tgz

Expand Down
2 changes: 0 additions & 2 deletions .mocharc.js → .mocharc.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use strict";

process.env.NODE_ENV = "test";

const nock = require("nock");
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Moved to esm. Backwards compatible with cjs. Breaks backwards compatibility if a cjs project requires anything
but the index or lib/voidElements as these are the only exposed as cjs.

## [2.5.0] - 2023-06-01

- Add support for `setheader` attribute on `<esi:eval />` and `<esi:include />`
Expand Down
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use strict";
import { pipeline, Readable } from "stream";

const { pipeline, Readable } = require("stream");
const ESI = require("./lib/ESI");
const HTMLStream = require("@bonniernews/atlas-html-stream");
const HTMLWriter = require("./lib/HTMLWriter");
import HTMLStream from "@bonniernews/atlas-html-stream";

module.exports = {
import ESI from "./lib/ESI.js";
import HTMLWriter from "./lib/HTMLWriter.js";

export {
ESI,
HTMLWriter,
parse,
Expand Down
12 changes: 5 additions & 7 deletions lib/ESI.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"use strict";
import ESIEvaluator from "./ESIEvaluator.js";
import ListenerContext from "./ListenerContext.js";
import ESIBase from "./ESIBase.js";

const ESIEvaluator = require("./ESIEvaluator");
const ListenerContext = require("./ListenerContext");
const ESIBase = require("./ESIBase");

module.exports = class ESI extends ESIBase {
export default class ESI extends ESIBase {
constructor(options) {
const evaluator = new ESIEvaluator(new ListenerContext(options));
super(evaluator);
this.context.emitter = this;
}
};
}
8 changes: 3 additions & 5 deletions lib/ESIBase.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use strict";
import { Transform } from "stream";

const { Transform } = require("stream");

module.exports = class ESIBase extends Transform {
export default class ESIBase extends Transform {
constructor(evaluator) {
super({ objectMode: true });
this.evaluator = evaluator;
Expand All @@ -17,4 +15,4 @@ module.exports = class ESIBase extends Transform {
return this.evaluator.onclosetag(name, next);
}
}
};
}
14 changes: 6 additions & 8 deletions lib/ESIEvaluator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable no-use-before-define */
"use strict";
import { pipeline, Readable } from "stream";

const { assign, test, replace } = require("./evaluateExpression");
const { pipeline, Readable } = require("stream");
const ESIBase = require("./ESIBase");
const HTMLStream = require("@bonniernews/atlas-html-stream");
import HTMLStream from "@bonniernews/atlas-html-stream";

import { assign, test, replace } from "./evaluateExpression.js";
import ESIBase from "./ESIBase.js";

class ESITag {
constructor(context) {
Expand Down Expand Up @@ -276,7 +276,7 @@ const EsiTags = {
"esi:foreach": ESIForEach,
};

class ESIEvaluator {
export default class ESIEvaluator {
constructor(context) {
this.context = context;
}
Expand Down Expand Up @@ -381,5 +381,3 @@ class ESIEvaluator {
}, {});
}
}

module.exports = ESIEvaluator;
9 changes: 4 additions & 5 deletions lib/HTMLWriter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"use strict";
import { Transform } from "stream";

const { chunkToMarkup } = require("./markup");
const { Transform } = require("stream");
import { chunkToMarkup } from "./markup.js";

module.exports = class HTMLWriter extends Transform {
export default class HTMLWriter extends Transform {
constructor() {
super({ writableObjectMode: true });
}
Expand All @@ -16,4 +15,4 @@ module.exports = class HTMLWriter extends Transform {
}
return next(null, markup);
}
};
}
14 changes: 7 additions & 7 deletions lib/ListenerContext.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use strict";
import { EventEmitter } from "events";

const { chunkToMarkup } = require("./markup");
const { EventEmitter } = require("events");
const { replace } = require("./evaluateExpression");
const request = require("got");
import request from "got";

module.exports = class ListenerContext {
import { chunkToMarkup } from "./markup.js";
import { replace } from "./evaluateExpression.js";

export default class ListenerContext {
constructor(options = {}, emitter) {
this.options = options;
this.emitter = emitter || new EventEmitter();
Expand Down Expand Up @@ -115,7 +115,7 @@ module.exports = class ListenerContext {
return { [key]: replace(val, self) };
}
}
};
}

function buildHeaderVariables(headers) {
if (!headers) return {};
Expand Down
18 changes: 5 additions & 13 deletions lib/evaluateExpression.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
"use strict";
import evaluate from "./expression/evaluate.js";
import { parse, split } from "./expression/parser.js";

const evaluate = require("./expression/evaluate");
const { parse, split } = require("./expression/parser");

module.exports = {
assign,
test,
replace,
};

function assign(value, context) {
export function assign(value, context) {
if (value === "true" || value === "false") return value;
return evaluate(parse(value), context);
}

function test(expression, context) {
export function test(expression, context) {
return evaluate(parse(expression), context);
}

function replace(text, context) {
export function replace(text, context) {
if (!text) return;

const expressions = split(text);
Expand Down
10 changes: 5 additions & 5 deletions lib/expression/evaluate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable camelcase */
"use strict";

const crypto = require("crypto");
const ent = require("ent");
import crypto from "crypto";

import ent from "ent";

class Evaluator {
constructor(context) {
Expand Down Expand Up @@ -205,9 +205,9 @@ class Evaluator {
}
}

module.exports = function evaluate(ast, context) {
export default function evaluate(ast, context) {
return new Evaluator(context).execute(ast.type, ast);
};
}

function castRight(left, right) {
switch (typeof left) {
Expand Down
40 changes: 18 additions & 22 deletions lib/expression/lexer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
/* eslint-disable prefer-template */

"use strict";
import {
ARRAY,
BINARY,
BLOCK,
BOOLEAN,
ENDMARK,
FUNCTION,
IDENTIFIER,
IDENTIFIER_CHARS,
LITERAL,
LOGICAL,
MEMBER,
NUMBER,
OBJECT,
UNARY,
WHITESPACE,
} from "./types.js";

const oneCharacterSymbols = "=()<>|*+-&{}/%,]:";
const twoCharacterSymbols = [
Expand All @@ -18,24 +34,6 @@ const NUMBERS = "0123456789";
const NUMBER_STARTCHARS = `-${NUMBERS}`;
const NUMBER_CHARS = NUMBER_STARTCHARS + ".";

const {
ARRAY,
BINARY,
BLOCK,
BOOLEAN,
ENDMARK,
FUNCTION,
IDENTIFIER,
IDENTIFIER_CHARS,
LITERAL,
LOGICAL,
MEMBER,
NUMBER,
OBJECT,
UNARY,
WHITESPACE,
} = require("./types");

class EsiSyntaxError extends SyntaxError {
constructor(message, source, column) {
super(message);
Expand Down Expand Up @@ -208,7 +206,7 @@ class Token {
}
}

class Lexer {
export class Lexer {
constructor(str, columnOffset, line) {
this.str = str;
this.scanner = new Scanner(str);
Expand Down Expand Up @@ -457,5 +455,3 @@ class Lexer {
throw err;
}
}

module.exports = { Lexer };
17 changes: 5 additions & 12 deletions lib/expression/parser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* eslint-disable prefer-template */
"use strict";

const { Lexer } = require("./lexer");

const {
import { Lexer } from "./lexer.js";
import {
ARRAY,
BINARY,
BLOCK,
Expand All @@ -17,12 +15,7 @@ const {
OBJECT,
UNARY,
WHITESPACE,
} = require("./types");

module.exports = {
parse,
split,
};
} from "./types.js";

class AST {
constructor() {
Expand Down Expand Up @@ -376,7 +369,7 @@ class Parser {
}
}

function parse(input, columnOffset) {
export function parse(input, columnOffset) {
if (!input) return;
input = input.trim();

Expand All @@ -389,7 +382,7 @@ function parse(input, columnOffset) {
return ast.tree;
}

function split(input) {
export function split(input) {
const lines = input.split("\n");

return lines.reduce((result, str, idx) => {
Expand Down
36 changes: 16 additions & 20 deletions lib/expression/types.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
"use strict";

module.exports = {
ARRAY: "ArrayExpression",
BINARY: "BinaryExpression",
BLOCK: "BlockStatement",
BOOLEAN: "Boolean",
ENDMARK: "EOL",
EXPRESSION: "Expression",
FUNCTION: "CallExpression",
IDENTIFIER: "Identifier",
LITERAL: "Literal",
LOGICAL: "LogicalExpression",
MEMBER: "MemberExpression",
NUMBER: "Number",
OBJECT: "ObjectExpression",
UNARY: "UnaryExpression",
WHITESPACE: "Space",
IDENTIFIER_CHARS: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
};
export const ARRAY = "ArrayExpression";
export const BINARY = "BinaryExpression";
export const BLOCK = "BlockStatement";
export const BOOLEAN = "Boolean";
export const ENDMARK = "EOL";
export const EXPRESSION = "Expression";
export const FUNCTION = "CallExpression";
export const IDENTIFIER = "Identifier";
export const LITERAL = "Literal";
export const LOGICAL = "LogicalExpression";
export const MEMBER = "MemberExpression";
export const NUMBER = "Number";
export const OBJECT = "ObjectExpression";
export const UNARY = "UnaryExpression";
export const WHITESPACE = "Space";
export const IDENTIFIER_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
15 changes: 5 additions & 10 deletions lib/markup.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
"use strict";
import { selfClosingElements, voidElements } from "./voidElements.js";

const { selfClosingElements, voidElements } = require("./voidElements");

module.exports = {
chunkToMarkup,
opentag,
closetag,
export {
voidElements,
selfClosingElements,
};

function chunkToMarkup({ name, data, text }) {
export function chunkToMarkup({ name, data, text }) {
let markup = "";
if (text) markup += text;
else if (name && data) markup += opentag(name, data);
Expand All @@ -19,7 +14,7 @@ function chunkToMarkup({ name, data, text }) {
return markup;
}

function opentag(tagname, attribs) {
export function opentag(tagname, attribs) {
if (selfClosingElements.includes(tagname)) {
return `<${tagname}${attributesToString(attribs)}/>`;
}
Expand All @@ -29,7 +24,7 @@ function opentag(tagname, attribs) {
return `<${tagname}${attributesToString(attribs)}>`;
}

function closetag(tagname) {
export function closetag(tagname) {
if (selfClosingElements.includes(tagname) || voidElements.includes(tagname)) {
return "";
}
Expand Down
Loading
Loading