-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add rollup config for UMD an ESM modules #7
Add rollup config for UMD an ESM modules #7
Conversation
Babel does add some boilerplate functions to the top of the distribution files, a much cleaner alternative is bublé but it doesn't translate as much features as babel. I tried it with your code, had to modify the 'for..of' since it is not supported and it worked but crashed on an infinite loop in stringifyArr, I think buble has some bug transpiling |
Thanks for this. I'm really excited to check it out, but I probably won't get time before this weekend. |
This is awesomw, thanks again for putting this together. Some quick thoughts:
Let me know what you think. |
|
|
Here is my PR for issue #5. I have modified the main JS file to use ES6 exports and removed the wrapping function (note that the code should be indented to the left 1 tab but I kept the indentation to keep the PR cleaner). With a modern browser you could directly use this file as a JS module. However it is good practice to generate an distribution ES module file with exports but with all other features transpiled to a specific target. This is the
dist/webauth-simple-app.esm.js
file. It is ES5 compatible apart from the exports. This is done by the babel plugin (babel-preset-env as preset).For CommonJS/AMD or <script> tag use a UMD module is built to
dist/webauthn-simple-app.js
.You can use
npm run build
ornpm run build:prod
to build these distribution files (currently the only difference is the generation of source maps).One problem I came across were the tests. On the node side, commonJS modules are expected so it no longer works with the new export statements. I have solved this by referring to the dist file instead. Downside is that you need to run build before test. If you want to keep using the actual source file in tests then alternative are using ES6 import statements in the tests as well or using
mocha --require babel-core/register
which will magically make it work (it will transpile the modules on the fly). However for the shared test code and the actual browser tests the former will only work with ES6 browsers and the latter can not work in a browser environment.On the browser side I also modified the code to use the dist file instead. Another change that had to be made in the shared test code on the browser side is that all exports are now grouped in one single global object,
window.WebAuthnSimpleApp
(btw, this name is pretty long but you can change it to something else in the rollup config if you want). This is a breaking change but it is more similar to how commonJS works and prevents global namespace pollution.