StableLib / stablelib

A stable library of useful TypeScript/JavaScript code
https://www.stablelib.com
Other
173 stars 35 forks source link

Support ES Modules #41

Closed dyoder closed 3 months ago

dyoder commented 3 years ago

I'm adding this per @dchest's comment in #28:

One "feature" that I'd like to add in the future is to support the modern module system in addition to CommonJS, so I'll work on it some time in the future. It will probably require reworking of the random generator detection.

FWIW, we need this for our work (we also need this for Tweet-NaCl). If there are no immediate plans to do this, we can submit a PR.

dyoder commented 3 years ago

@dchest I have a working model for doing this, but I don't want to apply it across all the packages unless it's acceptable to you.

I've tested this with base64 and utf8 and the tests still pass, although that's unsurprising since we still build into the lib directory as before. I didn't attempt to get Jasmine to run the tests in lib-es6 as well, but I can do that if this approach makes sense to you. However, I have successfully loaded the module into the browser via an import map, so it's able to find the esm version okay.

If you're agreeable to this approach, I can apply across all the packages and submit a PR. If not, I can try to incorporate your feedback. Or if you have other plans for implementing this, that's fine, too, obviously. Just thought I'd share my work. 🙂

josh-i386g commented 7 months ago

Hi, is this related to the errors I'm getting?

On eslint I use "plugin:@typescript-eslint/recommended-type-checked" instead of "plugin:@typescript-eslint/recommended".

import * as base64 from "@stablelib/base64";
import * as utf8 from "@stablelib/utf8";
import * as hex from "@stablelib/hex";
import * as nacl from "@stablelib/nacl";
Unsafe call of an `any` typed value.eslint[@typescript-eslint/no-unsafe-call](https://typescript-eslint.io/rules/no-unsafe-call)
Unsafe argument of type `any` assigned to a parameter of type `Uint8Array`.eslint[@typescript-eslint/no-unsafe-argument](https://typescript-eslint.io/rules/no-unsafe-argument)
Unsafe member access .decode on an `any` value.eslint[@typescript-eslint/no-unsafe-member-access](https://typescript-eslint.io/rules/no-unsafe-member-access)
function decode(s: string): Uint8Array

I have to use the following:

/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */

Just curious if this is something on stablelib's types, thank you.


Edit: this is a weird eslint bug that disappeared when i closed and reopened my vscode. Marking as resolved.

PhearZero commented 7 months ago

Hello @dchest and @dyoder. Hope you are well, didn't know about stable lib until today. Just submitted a draft PR to Tweet-NaCl for ESM. Would love to sync up and help in any way.

dchest commented 3 months ago

Fixed in 2.0.