Skip to content

Commit

Permalink
feat: commonjs support
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Oct 26, 2024
1 parent 5afb6e2 commit 534f071
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 31 deletions.
30 changes: 17 additions & 13 deletions bench/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable no-unused-vars */
import { html } from "ghtml";
import { Bench } from "tinybench";
import { writeFileSync } from "node:fs";
import { Buffer } from "node:buffer";
"use strict";

const { html } = require("..");
const { Bench } = require("tinybench");
const { writeFileSync } = require("node:fs");

const bench = new Bench({ time: 500 });

// eslint-disable-next-line no-unused-vars
let result = "";

bench.add("simple HTML formatting", () => {
Expand Down Expand Up @@ -108,13 +110,15 @@ bench.add("sparse escape", () => {
result = html`<p>${`${"noescape".repeat(250)}<>`.repeat(5)}</p>`;
});

await bench.warmup();
await bench.run();
(async () => {
await bench.warmup();
await bench.run();

const table = bench.table();
globalThis.console.table(table);
const table = bench.table();
globalThis.console.table(table);

writeFileSync(
"bench/results.json",
Buffer.from(JSON.stringify(table), "utf8").toString("base64"),
);
writeFileSync(
"bench/results.json",
Buffer.from(JSON.stringify(table), "utf8").toString("base64"),
);
})();
6 changes: 4 additions & 2 deletions bin/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env node
import { generateHashesAndReplace } from "./utils.js";
import process from "node:process";

"use strict";

const { generateHashesAndReplace } = require("./utils.js");

const parseArguments = (args) => {
let roots = null;
Expand Down
14 changes: 9 additions & 5 deletions bin/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Glob } from "glob";
import { createHash } from "node:crypto";
import { readFile, writeFile } from "node:fs/promises";
import { win32, posix } from "node:path";
"use strict";

const { Glob } = require("glob");
const { createHash } = require("node:crypto");
const { readFile, writeFile } = require("node:fs/promises");
const { win32, posix } = require("node:path");

const generateFileHash = async (filePath) => {
try {
Expand Down Expand Up @@ -77,7 +79,7 @@ const updateFilePathsWithHashes = async (
}
};

export const generateHashesAndReplace = async ({
const generateHashesAndReplace = async ({
roots,
refs,
prefix,
Expand Down Expand Up @@ -130,3 +132,5 @@ export const generateHashesAndReplace = async ({
skipPatterns,
);
};

module.exports.generateHashesAndReplace = generateHashesAndReplace;
8 changes: 8 additions & 0 deletions eslint.config.js → eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import grules from "grules";
import globals from "globals";

export default [
...grules,
{
languageOptions: {
globals: {
...globals.node,
},
},
},
{
rules: {
"no-await-in-loop": "off",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"author": "Gürgün Dayıoğlu",
"license": "MIT",
"version": "3.0.13",
"type": "module",
"type": "commonjs",
"bin": "./bin/src/index.js",
"main": "./src/index.js",
"exports": {
Expand All @@ -27,6 +27,7 @@
"devDependencies": {
"@fastify/pre-commit": "^2.1.0",
"c8": "^10.1.2",
"globals": "^15.11.0",
"grules": "^0.25.10",
"tinybench": "^2.9.0",
"typescript": ">=5.6.2"
Expand Down
8 changes: 6 additions & 2 deletions src/includeFile.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { readFileSync } from "node:fs";
"use strict";

const { readFileSync } = require("node:fs");

const cache = new Map();

/**
* @param {string} path path
* @returns {string} string
*/
export const includeFile = (path) => {
const includeFile = (path) => {
let file = cache.get(path);

if (file === undefined) {
Expand All @@ -16,3 +18,5 @@ export const includeFile = (path) => {

return file;
};

module.exports.includeFile = includeFile;
12 changes: 9 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

const _isArray = Array.isArray;

const _iterator = Symbol.iterator;
Expand Down Expand Up @@ -56,7 +58,7 @@ const escapeFunction = (string) => {
* @param {...any} expressions expressions
* @returns {string} string
*/
export const html = (literals, ...expressions) => {
const html = (literals, ...expressions) => {
let accumulator = "";

for (let i = 0; i !== expressions.length; ++i) {
Expand All @@ -83,7 +85,7 @@ export const html = (literals, ...expressions) => {
* @yields {string} string
* @returns {Generator<string, void, void>} Generator<string, void, void>
*/
export const htmlGenerator = function* (literals, ...expressions) {
const htmlGenerator = function* (literals, ...expressions) {
for (let i = 0; i !== expressions.length; ++i) {
let expression = expressions[i];
let literal = literals.raw[i];
Expand Down Expand Up @@ -174,7 +176,7 @@ export const htmlGenerator = function* (literals, ...expressions) {
* @yields {string} string
* @returns {AsyncGenerator<string, void, void>} AsyncGenerator<string, void, void>
*/
export const htmlAsyncGenerator = async function* (literals, ...expressions) {
const htmlAsyncGenerator = async function* (literals, ...expressions) {
for (let i = 0; i !== expressions.length; ++i) {
let expression = await expressions[i];
let literal = literals.raw[i];
Expand Down Expand Up @@ -264,3 +266,7 @@ export const htmlAsyncGenerator = async function* (literals, ...expressions) {
yield literals.raw[expressions.length];
}
};

module.exports.html = html;
module.exports.htmlGenerator = htmlGenerator;
module.exports.htmlAsyncGenerator = htmlAsyncGenerator;
12 changes: 7 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { html, htmlGenerator, htmlAsyncGenerator } from "../src/index.js";
import { readFile } from "node:fs/promises";
import { readFileSync } from "node:fs";
import test from "node:test";
import assert from "node:assert";
"use strict";

const { html, htmlGenerator, htmlAsyncGenerator } = require("..");
const { readFile } = require("node:fs/promises");
const { readFileSync } = require("node:fs");
const test = require("node:test");
const assert = require("node:assert/strict");

const conditionTrue = true;
const conditionFalse = false;
Expand Down

0 comments on commit 534f071

Please sign in to comment.