Skip to content

Commit b5bc56d

Browse files
committed
chore: sideEffects: false and cleaner dynamic imports
1 parent 4c5a584 commit b5bc56d

File tree

4 files changed

+24
-36
lines changed

4 files changed

+24
-36
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.0.0-dev",
44
"module": "index.ts",
55
"type": "module",
6+
"sideEffects": false,
67
"exports": {
78
".": "./lib/index.js",
89
"./api": "./lib/api.js",

src/async_store.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { process } from "./util.ts";
2+
13
export type Defer = () => void | Promise<void>;
24

35
export interface ComptimeContext {
@@ -14,24 +16,17 @@ export interface AsyncContext<T> {
1416
getStore(): T | undefined;
1517
}
1618

17-
let AsyncLocalStorage: new () => AsyncContext<{ __comptime_context: ComptimeContext }>;
19+
export let asyncLocalStore: AsyncContext<{ __comptime_context: ComptimeContext }>;
1820

1921
try {
20-
// avoid an unnecessary import if we're not in a node-like environment
21-
if (typeof process === "undefined") throw new Error();
22-
AsyncLocalStorage = await import("node:async_hooks").then(m => m.AsyncLocalStorage);
22+
// avoid an unnecessary import attempt if we're not in a node-like environment
23+
if (!process) throw new Error();
24+
const AsyncLocalStorage = (await import("node:async_hooks")).AsyncLocalStorage;
25+
asyncLocalStore = new AsyncLocalStorage();
2326
} catch {
24-
// if we're not in a node-like environment, use a noop implementation, no need to error
25-
class NoopAsyncLocalStorage<T> implements AsyncContext<T> {
26-
run<R>(_: T, callback: () => R): R {
27-
return callback();
28-
}
29-
getStore(): T | undefined {
30-
return undefined;
31-
}
32-
}
33-
34-
AsyncLocalStorage = NoopAsyncLocalStorage;
27+
// noop AsyncLocalStorage implementation if we're not in a node-like environment
28+
asyncLocalStore = {
29+
run: (_, callback) => callback(),
30+
getStore: () => undefined,
31+
};
3532
}
36-
37-
export const asyncLocalStore = AsyncLocalStorage && new AsyncLocalStorage();

src/errors.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
1+
import { process } from "./util.ts";
2+
13
import type { Options } from "boxen";
24

3-
let boxen: typeof import("boxen")["default"] | undefined;
5+
export let box = (text: string, options?: Options, skip?: boolean): string => text;
46

5-
try {
6-
if (typeof process === "undefined") throw new Error();
7-
boxen = (await import("boxen")).default;
8-
} catch {
9-
boxen = undefined;
7+
if (!Boolean(process?.env.NO_BOX)) {
8+
try {
9+
if (!process) throw new Error();
10+
const boxen = (await import("boxen")).default;
11+
const pad = (text: string) => text.replace(/^.*$/gm, line => " " + line + " ");
12+
box = (text, options, skip) => (skip ? text : boxen(pad(text), options));
13+
} catch {}
1014
}
1115

12-
const NO_BOX = Boolean(process?.env.NO_BOX);
13-
14-
export const box = (text: string, options?: Options, skip?: boolean) =>
15-
boxen && !skip && !NO_BOX
16-
? boxen(
17-
text
18-
.split("\n")
19-
.map(l => " " + l + " ")
20-
.join("\n"),
21-
options,
22-
)
23-
: text;
24-
2516
export const COMPTIME_ERRORS = {
2617
CT_ERR_GET_EVALUATION: "CT_ERR_GET_EVALUATION",
2718
CT_ERR_SYNTAX_CHECK: "CT_ERR_SYNTAX_CHECK",

src/util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const process = globalThis.process as NodeJS.Process | undefined;

0 commit comments

Comments
 (0)