cryptpad / chainpad-crypto

Pluggable cryptography module for chainpad
GNU Affero General Public License v3.0
4 stars 3 forks source link

Fails to complie in typescript #1

Open chmac opened 5 years ago

chmac commented 5 years ago
Failed to compile.

./node_modules/chainpad-crypto/crypto.js
Module not found: Can't resolve '/bower_components/tweetnacl/nacl-fast.min.js' in '../node_modules/chainpad-crypto'

I think the issue is this part of the built output of crypto.js:

    if (typeof(module) !== 'undefined' && module.exports) {
        module.exports = factory(require('tweetnacl'));
    } else if ((typeof(define) !== 'undefined' && define !== null) && (define.amd !== null)) {
        define([
            '/bower_components/tweetnacl/nacl-fast.min.js',
        ], function () {
            return factory(window.nacl);
        });
    } else {
        window.chainpad_crypto = factory(window.nacl);
    }
chmac commented 5 years ago

Hmm. It looks like my solution does not work alone. I also need to open the node_modules/chainpad-crypto/crypto.js file in an editor and save it. I think somehow the save is triggering typescript / create-react-app to pick up the file somehow. I'm not sure on the details. But I just realised that on a clean install my branch in #2 does not work, but when I have saved the file just once (without any changes), it does work. Sorry I can't provide more useful in put at this point, I'll continue to investigate...

chmac commented 5 years ago

There was some useful input from @ansuz in #2. Unfortunately the approach I tried in that PR does not actually work. I think the problem is to do with compiling. Somehow when I save (even without editing) the node_modules/chainpad-crypto/crypto.js file, with the changes included in #2, then I'm able to use the module. I think that's caused by typescript (or maybe my editor) then compiling the module.

Eventually I figured that for my use case, the whole factory() / require('tweetnacl') approach wasn't working. So, to get the code running reliably, I exported the factory by adding export default factory to the file. Then I import the factory, import tweetnacl and combine them in my app. Not ideal, but this approach reliably gets me working in typescript (with create-react-app).

Here's the code I'm using: https://github.com/chmac/chainpad-crypto/blob/just-default-export-factory/crypto.js

Maybe as I learn more about typescript I'll be able to figure out how this could be done better and submit a better PR. Here's the typing that I created in order to be able to use some parts of the code. https://github.com/chmac/cryptparty/blob/bbf36c8d856194a13b114ef2bf7e80b292352053/app/modules.d.ts