Last active
August 29, 2015 14:08
-
-
Save codeboyim/cc7c967d49bf85c52859 to your computer and use it in GitHub Desktop.
a sample Webpack configuration to include Facebook Parse JavaScript SDK for a Web project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file webpack.config.js | |
* @author codeboy<[email protected]> | |
* @desc this gist shows the essential sections used in configuring webpack | |
* to make the Facebook's Parse JavaScript SDK work as other modules as expected. | |
* this gist is inspired by an answer to an issue reported in webpack repo | |
* {@link https://github.com/webpack/webpack-dev-server/issues/66} | |
* disclaimer: this configuration is correct as provided and effective on Parse 1.3.0 | |
*/ | |
module.exports = { | |
module: { | |
loaders: [{ | |
//the SDK exports the APIs to an object named Parse. | |
//without this setting, you have to use | |
//var Parse = require('parse').Parse | |
//to access its APIs. | |
//after this change, you can now use something like | |
//var Parse = require('parse'); | |
//Parse.initialize(...); | |
test: /parse-latest.js$/, | |
loader: 'exports?exports.Parse' | |
}] | |
}, | |
externals: [{ | |
//this is the main trap why this SDK can't be "require"d as usual. | |
//the original Parse requires a "xmlhttprequest" module shimming XMLHttpRequest | |
//which requires "child_process" module that is unsupported in browser environment. | |
//this setting will force the SDK to use the native XMLHttpRequest object from the browser. | |
xmlhttprequest: '{XMLHttpRequest:XMLHttpRequest}' | |
}], | |
plugins: [ | |
//save you a bit of typing of "require('parse')" on every single module it's used. | |
new webpack.ProvidePlugin({ | |
Parse: 'parse' | |
}) | |
] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I am having the exact issue you described here but when I tried to mimic your solution I am having an error of
module.js:340,
throw err;
^
Error: Cannot find module '{XMLHttpRequest:XMLHttpRequest}'
I am adding the loaders and externals inside make-webpack-config.js. I also tried to install xmlhttprequest but it didn't help. Could you let me know if I missed something?
Thanks!