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

How to properly export autoNumeric as JS module #727

Open
stsrki opened this issue Dec 23, 2021 · 4 comments
Open

How to properly export autoNumeric as JS module #727

stsrki opened this issue Dec 23, 2021 · 4 comments

Comments

@stsrki
Copy link

stsrki commented Dec 23, 2021

I'm trying to use autoNumeric as part of my library. And I want to import it as a JS module. But whatever I try I cannot make it work.

The errors I got so far:

Unhandled exception rendering component: The requested module './vendors/autoNumeric.js' does not provide an export named 'default'
      SyntaxError: The requested module './vendors/autoNumeric.js' does not provide an export named 'default'

If I try to import directly from src I get

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

I have tried building autocomplete with libraryTarget as umd, amd, commonjs2, commonjs, etc. Nothing seems to work.

I'm missing something. Any chance you could point me in the right direction?

@stsrki
Copy link
Author

stsrki commented Dec 24, 2021

One of my team members was able to track down the cause by using an older version of the library and slowly moving up, starting from v4.5.4. After comparing between versions, he changes this to window and the error fade away.

image

With this knowledge, we have tried to build again autoNumeric source and it worked.

output : {
    libraryTarget: 'umd',
    library      : 'AutoNumeric',
    filename     : 'autoNumeric.min.js',
    path         : resolve('dist'),
+    globalObject : 'window',
},

We are now using this custom-built version, but I think it might be worth knowing if anyone else happened to stumble on the same error.

@AlexandreBonneau
Copy link
Member

I'd have to check that out thoroughly, since I was always able to export to module on my end. Perhaps indeed I missed something in the few last versions.

@stsrki
Copy link
Author

stsrki commented Jan 19, 2022

Just to let you know. We're using this with Blazor JSInterop and importing it into the browser. No nodejs, babel, webpack. or anything else. You can see it in action at https://github.com/Megabit/Blazorise/blob/d42a30b34a46870b1cc28bbc36955b6b654732d9/Source/Blazorise/wwwroot/numericPicker.js#L3

Hope it helps.

@colinj
Copy link

colinj commented Feb 14, 2022

Thank you @stsrki. I am getting the same errors as you. Unfortunately AutoNumeric is practically unusable when importing as an ES6 module. Changing this to window did work but as it stands for me unless I go down the same path as you and create a custom-build the only other option is to add the library as a script tag on the index page.

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

No branches or pull requests

3 participants