Skip to content

Commit f10dfed

Browse files
committed
wip: conversion to ESM
[ci skip]
1 parent 3269059 commit f10dfed

17 files changed

+10309
-4953
lines changed

.npmrc

Lines changed: 0 additions & 2 deletions
This file was deleted.
File renamed without changes.

package-lock.json

Lines changed: 10223 additions & 4915 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/test.mjs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env node
2+
3+
import os from 'node:os';
4+
import path from 'node:path';
5+
import url from 'node:url';
6+
import process from 'node:process';
7+
import childProcess from 'node:child_process';
8+
9+
const projectPath = path.dirname(
10+
path.dirname(url.fileURLToPath(import.meta.url)),
11+
);
12+
13+
const platform = os.platform();
14+
15+
/* eslint-disable no-console */
16+
async function main(argv = process.argv) {
17+
argv = argv.slice(2);
18+
const tscArgs = [`-p`, path.join(projectPath, 'tsconfig.build.json')];
19+
console.error('Running tsc:');
20+
console.error(['tsc', ...tscArgs].join(' '));
21+
childProcess.execFileSync('tsc', tscArgs, {
22+
stdio: ['inherit', 'inherit', 'inherit'],
23+
windowsHide: true,
24+
encoding: 'utf-8',
25+
shell: platform === 'win32' ? true : false,
26+
});
27+
const jestArgs = [...argv];
28+
console.error('Running jest:');
29+
console.error(['jest', ...jestArgs].join(' '));
30+
childProcess.execFileSync('jest', jestArgs, {
31+
env: {
32+
...process.env,
33+
NODE_OPTIONS: '--experimental-vm-modules',
34+
},
35+
stdio: ['inherit', 'inherit', 'inherit'],
36+
windowsHide: true,
37+
encoding: 'utf-8',
38+
shell: platform === 'win32' ? true : false,
39+
});
40+
}
41+
/* eslint-enable no-console */
42+
43+
if (import.meta.url.startsWith('file:')) {
44+
const modulePath = url.fileURLToPath(import.meta.url);
45+
if (process.argv[1] === modulePath) {
46+
void main();
47+
}
48+
}

src/Id.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { MultibaseFormats } from './utils';
2-
import * as utils from './utils';
1+
import type { MultibaseFormats } from './utils.js';
2+
import * as utils from './utils.js';
33

44
/**
55
* IdInternal can be used as a string primitive

src/IdDeterministic.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Id } from './Id';
1+
import type { Id } from './Id.js';
22
import { v5 as uuidv5, NIL } from 'uuid';
3-
import IdInternal from './Id';
3+
import IdInternal from './Id.js';
44

55
/**
66
* This produces deterministic ids based on:

src/IdRandom.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { Id } from './Id';
1+
import type { Id } from './Id.js';
22
import { v4 as uuidv4 } from 'uuid';
3-
import * as utils from './utils';
4-
import IdInternal from './Id';
3+
import * as utils from './utils.js';
4+
import IdInternal from './Id.js';
55

66
class IdRandom<T extends Id = Id> implements IterableIterator<T> {
77
protected randomSource: (size: number) => Uint8Array;

src/IdSortable.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Id } from './Id';
2-
import IdInternal from './Id';
3-
import * as utils from './utils';
1+
import type { Id } from './Id.js';
2+
import IdInternal from './Id.js';
3+
import * as utils from './utils.js';
44

55
/**
66
* Constants for UUIDv7 with millisecond precision

src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
export type { Id } from './Id';
2-
export { default as IdInternal } from './Id';
3-
export { default as IdRandom } from './IdRandom';
4-
export { default as IdDeterministic } from './IdDeterministic';
5-
export { default as IdSortable } from './IdSortable';
6-
export * as idSortable from './IdSortable';
7-
export * as utils from './utils';
1+
export type { Id } from './Id.js';
2+
export { default as IdInternal } from './Id.js';
3+
export { default as IdRandom } from './IdRandom.js';
4+
export { default as IdDeterministic } from './IdDeterministic.js';
5+
export { default as IdSortable } from './IdSortable.js';
6+
export * as idSortable from './IdSortable.js';
7+
export * as utils from './utils.js';

src/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import type { Codec } from 'multiformats/bases/base';
2-
import type { Id } from './Id';
1+
import type { Id } from './Id.js';
32
import crypto from 'crypto';
43
import { performance } from 'perf_hooks';
54
import { bases } from 'multiformats/basics';
6-
import IdInternal from './Id';
5+
import IdInternal from './Id.js';
76

87
/**
98
* Gets random bytes as Uint8Array
@@ -140,8 +139,9 @@ function fromUUID(uuid: string): Id | undefined {
140139
}
141140

142141
type MultibaseFormats = keyof typeof bases;
142+
type Codec = (typeof bases)[MultibaseFormats];
143143

144-
const basesByPrefix: Record<string, Codec<string, string>> = {};
144+
const basesByPrefix: Record<string, Codec> = {};
145145
for (const k in bases) {
146146
const codec = bases[k];
147147
basesByPrefix[codec.prefix] = codec;

tests/Id.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Id } from '@/Id';
2-
import IdInternal from '@/Id';
3-
import * as utils from '@/utils';
1+
import type { Id } from '#Id.js';
2+
import IdInternal from '#Id.js';
3+
import * as utils from '#utils.js';
44

55
describe('Id', () => {
66
test('create id from buffer', () => {

tests/IdDeterministic.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Id } from '@';
2-
import IdDeterministic from '@/IdDeterministic';
3-
import * as utils from '@/utils';
1+
import type { Id } from '#index.js';
2+
import IdDeterministic from '#IdDeterministic.js';
3+
import * as utils from '#utils.js';
44

55
describe('IdDeterministic', () => {
66
test('ids are Uint8Array', () => {

tests/IdRandom.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Id } from '@';
2-
import IdRandom from '@/IdRandom';
3-
import * as utils from '@/utils';
1+
import type { Id } from '#index.js';
2+
import IdRandom from '#IdRandom.js';
3+
import * as utils from '#utils.js';
44

55
describe('IdRandom', () => {
66
test('ids are Uint8Array', () => {

tests/IdSortable.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { Id } from '@';
2-
import IdSortable, { extractTs, extractSeq, extractRand } from '@/IdSortable';
3-
import * as utils from '@/utils';
4-
import { sleep, shuffle } from './utils';
1+
import type { Id } from '#index.js';
2+
import { sleep, shuffle } from './utils.js';
3+
import IdSortable, { extractTs, extractSeq, extractRand } from '#IdSortable.js';
4+
import * as utils from '#utils.js';
55

66
describe('IdSortable', () => {
77
test('ids are Uint8Array', () => {

tests/setupAfterEnv.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { jest } from '@jest/globals';
2+
13
// Default timeout per test
24
// some tests may take longer in which case you should specify the timeout
35
// explicitly for each test by using the third parameter of test function

tests/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as uuid from 'uuid';
2-
import * as utils from '@/utils';
2+
import * as utils from '#utils.js';
33

44
describe('utils', () => {
55
test('take from an iterator', () => {

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"./src/**/*",
2828
"./src/**/*.json",
2929
"./tests/**/*",
30-
"./scripts/**/*",
30+
"./scripts/**/*"
3131
],
3232
"ts-node": {
3333
"esm": true,

0 commit comments

Comments
 (0)