stablelib icon indicating copy to clipboard operation
stablelib copied to clipboard

Support ES Modules

Open dyoder opened this issue 4 years ago • 3 comments

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 avatar Aug 01 '21 20:08 dyoder

@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.

  • Basically, I add a second tsconfig file, like this.
  • Then I add exports and alter the build command like this.
  • Running npm run build now will build both versions, placing the esm-compatible version into lib-es6.

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. 🙂

dyoder avatar Aug 03 '21 04:08 dyoder

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.

josh-i386g avatar Feb 11 '24 20:02 josh-i386g

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.

  • https://github.com/dchest/tweetnacl-js/pull/261

PhearZero avatar Feb 15 '24 21:02 PhearZero

Fixed in 2.0.

dchest avatar Jun 29 '24 17:06 dchest