Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a sample Webpack configuration to include Facebook Parse JavaScript SDK for a Web project
/**
* @file webpack.config.js
* @author codeboy<me@codeboy.im>
* @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'
})
]
};
@jeffyuans
Copy link

jeffyuans commented May 6, 2015

Hi,

I am having the exact issue you described here but when I tried to mimic your solution I am having an error of

node lib/server-production

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!

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