Skip to content

Commit

Permalink
Unapologetically ditch all CommonJS support
Browse files Browse the repository at this point in the history
Now that Node.js finally has unflagged ESM support, there's no reason to
use CommonJS in my own projects anymore. Since a breaking release is due
anyway, now's as good a time as any.
  • Loading branch information
Alhadis committed Dec 10, 2019
1 parent 20442bd commit c084ba9
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 106 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"globalThis": true
},
"overrides": [{
"files": ["lib/*.mjs", "test/*.js"],
"files": ["lib/*.mjs", "test/*.mjs"],
"rules": {"require-atomic-updates": 0}
},{
"files": ["index.mjs", "lib/env.mjs"],
Expand All @@ -16,7 +16,7 @@
"files": ["lib/functions.mjs"],
"rules": {"keyword-spacing": 0}
},{
"files": ["test/*.js"],
"files": ["test/*.mjs"],
"rules": {"keyword-spacing": 0},
"globals": {"expect": true}
}]
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
.DS_Store
node_modules
*.log
*.map
*.tgz
/index.js
/lib/*.d.ts
/test.*
tmp.*
File renamed without changes.
41 changes: 12 additions & 29 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
all: index.js lint test

# Generate a CommonJS version of ESM libraries
index.js: index.mjs lib/*.mjs
npx rollup \
--silent \
--format cjs \
--preferConst \
--no-interop \
--sourcemap \
--sourcemapExcludeSources \
--input index.mjs \
--file $@
all: lint types test


# Generate TypeScript declarations from JSDoc
types: index.d.ts
index.d.ts: index.mjs lib/*.mjs
npx jg typewrite \
--exclude BlendModes \
--declare const BlendModes '{[key: string]: (...args: number[]) => number};' \
--header '// Generated file; run `make $@` to update.' \
--sort index.js
npx terser \
--keep-classnames \
--mangle \
--compress \
--source-map "content=$@.map,url=$(@F).map" \
--output $@ $@


# Nuke generated CJS bundle
clean:
rm -f index.js*

.PHONY: clean
--header '// Generated file; run `make types` to update.' \
--sort $^
cat lib/*.d.ts | sort | uniq > $@
rm lib/*.d.ts
npx jg lint -t


# Check source for errors and style violations
Expand All @@ -39,7 +22,7 @@ lint:


# Run unit-tests
test: index.js
test:
npx mocha

.PHONY: test
Expand Down
6 changes: 3 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated file; run `make index.js` to update.
// Generated file; run `make types` to update.
declare type CMYColour = [number, number, number];
declare type CMYKColour = [number, number, number, number];
declare type CommandList = Array<(string|Array<string>)>;
Expand All @@ -16,6 +16,8 @@ export declare const asap: (fn: Function) => void;
export declare const haveHighResTiming: boolean;
export declare const isElectron: boolean;
export declare const isNode: boolean;
export declare const now: Function;
export declare const openExternal: (uri: string) => void;
export declare const self: object;
export declare function New(type: string, attr?: object): Element;
export declare function addTo(parent: Node): Function;
Expand Down Expand Up @@ -79,8 +81,6 @@ export declare function kebabToCamelCase(input: string): string;
export declare function keyGrep(subject: object, pattern: RegExp | string): object;
export declare function nearest(subject: Node, selector: string, ignoreSelf?: boolean): Element;
export declare function nerf(fn: Function, context?: object): Function;
export declare function now(): number;
export declare function openExternal(uri: string): void;
export declare function ordinalSuffix(n: number): string;
export declare function parseCSSDuration(value: string): number;
export declare function parseHTMLFragment(input: string): Node[];
Expand Down
23 changes: 6 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,18 @@
"repository": "https://github.com/Alhadis/Utils",
"author": "John Gardner <gardnerjohng@gmail.com>",
"license": "ISC",
"type": "commonjs",
"main": "index.mjs",
"type": "module",
"types": "./index.d.ts",
"exports": {
"./binary": "./lib/binary.mjs",
"./canvas": "./lib/canvas.mjs",
"./colours": "./lib/colours.mjs",
"./dom": "./lib/dom.mjs",
"./env": "./lib/env.mjs",
"./functions": "./lib/functions.mjs",
"./math": "./lib/math.mjs",
"./misc": "./lib/misc.mjs",
"./shell": "./lib/shell.mjs",
"./text": "./lib/text.mjs",
".": "./index.mjs"
"engines": {
"node": ">=13.2.0"
},
"devDependencies": {
"@alhadis/eslint-config": "^2.1.0",
"mocha-when": "^1.0.1",
"rollup": "^1.27.8",
"chai": "^4.2.0",
"eslint": "^6.7.2",
"mocha": "^6.2.2",
"chai": "^4.2.0",
"terser": "^4.4.2"
"mocha-when": "^1.0.1"
},
"scripts": {
"lint": "make lint",
Expand Down
12 changes: 6 additions & 6 deletions test/binary.js → test/binary.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use strict";
import {readFileSync} from "fs";
import {dirname, join} from "path";
import {fileURLToPath} from "url";
import * as utils from "../index.mjs";

describe("Byte-level functions", () => {
const utils = require("../index.js");
const {join} = require("path");
const fs = require("fs");
const file = path =>
fs.readFileSync(join(__dirname, "fixtures", ...path.split("/")), {encoding: "binary"});
const dir = dirname(fileURLToPath(import.meta.url));
const file = path => readFileSync(join(dir, "fixtures", ...path.split("/")));

describe("adler32()", () => {
const {adler32} = utils;
Expand Down
14 changes: 8 additions & 6 deletions test/colours.js → test/colours.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"use strict";
import * as utils from "../index.mjs";

describe("Colour-related functions", function(){
this.slow(1000);
const utils = require("../index.js");

describe("Blending modes", () => {
// NB: Subtract is omitted because it's inconsistent with Photoshop's behaviour
Expand Down Expand Up @@ -418,10 +417,13 @@ describe("Colour-related functions", function(){
});

describe("Colour conversion", () => {
const cmykTests = require("./fixtures/colours/cmyk-tests.js");
const hslTests = require("./fixtures/colours/hsl-tests.js");
const hsvTests = require("./fixtures/colours/hsv-tests.js");
const mixedTests = require("./fixtures/colours/mixed-tests.js");
let cmykTests, hslTests, hsvTests, mixedTests;
before("Loading fixtures", async () => {
cmykTests = await import("./fixtures/colours/cmyk-tests.mjs");
hslTests = await import("./fixtures/colours/hsl-tests.mjs");
hsvTests = await import("./fixtures/colours/hsv-tests.mjs");
mixedTests = await import("./fixtures/colours/mixed-tests.mjs");
});

it("converts CMYK to CMY", () => {
const {cmykToCMY} = utils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict";
module.exports = [
export default [
{rgb: [255, 255, 255], cmyk: [0, 0, 0, 0], cmy: [0, 0, 0]},
{rgb: [255, 228, 228], cmyk: [0, 0.11, 0.11, 0], cmy: [0, 0.11, 0.11]},
{rgb: [255, 196, 196], cmyk: [0, 0.23, 0.23, 0], cmy: [0, 0.23, 0.23]},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict";
module.exports = [
export default [
{rgb: [0, 0, 0], hsl: [0, 0, 0]},
{rgb: [0, 0, 32], hsl: [240, 1, 0.063]},
{rgb: [0, 0, 64], hsl: [240, 1, 0.125]},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict";
module.exports = [
export default [
{rgb: [0, 0, 0], hsv: [0, 0, 0]},
{rgb: [0, 0, 32], hsv: [240, 1, 0.125]},
{rgb: [0, 0, 64], hsv: [240, 1, 0.251]},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict";
module.exports = [
export default [
{
hexInt: 0xFFFFFF,
hexStr: "#FFFFFF",
Expand Down
4 changes: 1 addition & 3 deletions test/functions.js → test/functions.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use strict";
import * as utils from "../index.mjs";

describe("Higher-order functions", () => {
const utils = require("../index.js");

describe("punch()", () => {
const {punch} = utils;

Expand Down
15 changes: 7 additions & 8 deletions test/misc.js → test/misc.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";
import * as utils from "../index.mjs";

describe("Miscellaneous functions", () => {
const utils = require("../index.js");
const htmlAllFn = "function HTMLAllCollection() { [native code] }";
const isBrowser = (
"object" === typeof window &&
Expand Down Expand Up @@ -341,9 +340,9 @@ describe("Miscellaneous functions", () => {
}
HTMLAllCollection.toString = function toString(){ return htmlAllFn; };
const document = {all: new HTMLAllCollection()};
global.HTMLAllCollection = HTMLAllCollection;
global.window = {HTMLAllCollection, document};
global.document = document;
globalThis.HTMLAllCollection = HTMLAllCollection;
globalThis.window = {HTMLAllCollection, document};
globalThis.document = document;
}

/**
Expand All @@ -352,8 +351,8 @@ describe("Miscellaneous functions", () => {
*/
function unspoofBrowser(){
if(isBrowser) return;
delete global.HTMLAllCollection;
delete global.window;
delete global.document;
delete globalThis.HTMLAllCollection;
delete globalThis.window;
delete globalThis.document;
}
});
33 changes: 16 additions & 17 deletions test/shell.js → test/shell.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use strict";
import fs from "fs";
import path from "path";
import url from "url";
import * as utils from "../index.mjs";

describe("Shell-specific functions", () => {
const utils = require("../index.js");
const fs = require("fs");
const path = require("path");

const dir = path.dirname(url.fileURLToPath(import.meta.url));

describe("exec()", function(){
const {exec, wait} = utils;
this.slow(1000);
Expand Down Expand Up @@ -108,7 +109,7 @@ describe("Shell-specific functions", () => {

describe("Redirection", function(){
this.slow(5000);
const tempFile = require("path").join(__dirname, "fixtures", "temp.log");
const tempFile = path.join(dir, "fixtures", "temp.log");
after("Removing temporary file", () => fs.unlinkSync(tempFile));

it("can write standard output to a file", async () => {
Expand Down Expand Up @@ -163,18 +164,17 @@ describe("Shell-specific functions", () => {

let cwd = "";
afterEach(() => cwd && process.chdir(cwd));
beforeEach(() => { cwd = process.cwd(); process.chdir(__dirname); });
beforeEach(() => { cwd = process.cwd(); process.chdir(dir); });

it("defaults to the parent process's working directory", async () => {
const {stdout} = await exec("node", echoCwd);
expect(stdout).to.equal(__dirname);
expect(stdout).to.equal(dir);
});

it("can change the subprocess's working directory", async () => {
const {join} = require("path");
cwd = join(__dirname, "fixtures");
cwd = path.join(dir, "fixtures");
const {stdout} = await exec("node", echoCwd, null, {cwd});
expect(stdout).to.equal(join(__dirname, "fixtures"));
expect(stdout).to.equal(path.join(dir, "fixtures"));
});
});
});
Expand Down Expand Up @@ -223,8 +223,7 @@ describe("Shell-specific functions", () => {
}));

it("can write the last command's output to a file", async () => {
const fs = require("fs");
const tmp = require("path").join(__dirname, "fixtures", "temp.log");
const tmp = path.join(dir, "fixtures", "temp.log");
fs.existsSync(tmp) && fs.unlinkSync(tmp);
expect(await execChain([
["node", "-e", "console.warn(123); console.log(456)"],
Expand Down Expand Up @@ -275,7 +274,7 @@ describe("Shell-specific functions", () => {

describe("ls()", () => {
const {ls} = utils;
const fixtures = path.join(__dirname, "fixtures", "ls");
const fixtures = path.join(dir, "fixtures", "ls");
const stripTimestamps = stats => Object.keys(stats)
.filter(key => /^(?:[amc]|birth)time(?:ms)?$/i.test(key) || stats[key] instanceof Date)
.forEach(timestamp => delete stats[timestamp]);
Expand Down Expand Up @@ -312,7 +311,7 @@ describe("Shell-specific functions", () => {
describe("which()", () => {
const {which} = utils;
const pathKey = "win32" === process.platform ? "Path" : "PATH";
const fixtures = path.join(__dirname, "fixtures", "which");
const fixtures = path.join(dir, "fixtures", "which");
const tmpClean = () => fs.readdirSync(fixtures).forEach(file =>
/^tmp\./i.test(file) && fs.unlinkSync(path.join(fixtures, file)));

Expand Down Expand Up @@ -370,15 +369,15 @@ describe("Shell-specific functions", () => {
"win32" === process.platform && describe("Windows-specific", () => {
beforeEach(() => process.env = {...env, Path: fixtures});
before(async () => {
process.env.Path = __dirname;
process.env.Path = dir;
process.env.PATHEXT = ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.WSF;.WSH;.MSC";
expect(await which("bar")) .to.equal("");
expect(await which("tmp.1.foo")) .to.equal("");
expect(await which("tmp.2.foo")) .to.equal("");
});

it("tests %PATHEXT% case-insensitively", async () => {
process.env.Path = fixtures + path.delimiter + __dirname;
process.env.Path = fixtures + path.delimiter + dir;
process.env.PATHEXT += ";.FOO";
fs.writeFileSync(path.join(fixtures, "tmp.1.foo"), "");
fs.writeFileSync(path.join(fixtures, "tmp.2.FOO"), "");
Expand Down
4 changes: 1 addition & 3 deletions test/text.js → test/text.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use strict";
import * as utils from "../index.mjs";

describe("Text-related functions", () => {
const utils = require("../index.js");

describe("escapeHTML()", () => {
const {escapeHTML} = utils;
it("escapes angle brackets", () => void expect(escapeHTML("< < > >")).to.equal("&#60; &#60; &#62; &#62;"));
Expand Down

0 comments on commit c084ba9

Please sign in to comment.