I was trying to get amazon-cognito-identity-js to work with Webpack. It depends on the jsbn and jsbn2 libraries that were designed to be exposed globally. This doesn't work with Webpack because Webpack locally scopes everything it "requires".
Webpack useses a series of loaders whenever a file is being "required". Using the exports-loader, we can have Webpack append exports["MyVariable"] = (myVariable);
to the end of a file it's appending to our compiled output.
Read more about Webpack's options for shimming modules.
npm install imports-loader npm install exports-loader
In your webpack.config.js file, set the following loaders
settings.
/* ... Your webpack.config.js specific settings. .... */ module.exports = { /* ... */ module: { loaders: [ { test: /jsbn\.js$/, loaders: [ 'exports?BigInteger=BigInteger', 'exports?IntAt=intAt', 'exports?Nbv=nbv', 'exports?Montgomery=Montgomery', 'exports?Nbi=nbi', 'exports?Nbits=nbits' ] }, { test: /jsbn2\.js$/, loaders: [ 'imports?BigInteger=>JsbnWrapper.BigInteger', 'imports?nbi=>JsbnWrapper.Nbi', 'imports?intAt=>JsbnWrapper.IntAt', 'imports?nbv=>JsbnWrapper.Nbv', 'imports?Montgomery=>JsbnWrapper.Montgomery', 'imports?nbits=>JsbnWrapper.Nbits' ] }, { test: /amazon-cognito-identity\.min\.js$/, loaders: [ 'imports?BigInteger=>JsbnWrapper.BigInteger' ] } ] } };
In your JS file that needs to leverage the library, set the results of "require" to be global.
// In your main entry point js file. // webpack.config.js is configured to export a series of variables that we'll also import into jsbn2 and the AWS SDK. JsbnWrapper = require("./js/lib/utils/jsbn.js"); // Webpack.config.js will import variables into this file that were exported from jsbn.js. require('./js/lib/utils/jsbn2.js'); // Set "sjcl" to the global scope. sjcl = require('sjcl'); require('./node_modules/amazon-cognito-identity-js/dist/aws-cognito-sdk.min.js'); require('./node_modules/amazon-cognito-identity-js/dist/amazon-cognito-identity.min.js');
Software engineer by profession, embedded systems tinkerer, husband, father, fantasy novel devourer, wine lush, beer and cheese connoisseur