Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to complie in typescript #1

Open
chmac opened this issue May 17, 2019 · 2 comments
Open

Fails to complie in typescript #1

chmac opened this issue May 17, 2019 · 2 comments

Comments

@chmac
Copy link

chmac commented May 17, 2019

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 added a commit to chmac/chainpad-crypto that referenced this issue May 17, 2019
@chmac
Copy link
Author

chmac commented May 22, 2019

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
Copy link
Author

chmac commented May 29, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant