Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# Install NPM dependencies:
npm install babel-generator@6.26.0 babel-traverse@6.26.0 babylon@6.18.0
# Download Facebook's unminified SDK
wget https://connect.facebook.net/en_US/sdk/debug.js
# Run
node split-facebook-debug-js.js
const babylon = require('babylon');
const traverse = require('babel-traverse').default;
const generate = require('babel-generator').default;
const path = require('path');
const amdPath = path.join(__dirname, 'amd');
const fs = require('fs');
const fb = babylon.parse(fs.readFileSync('debug.js').toString());
const modules = [];
// Traverse the AST and extract modularized code
traverse(fb, {
enter (path) {
if (path.isIdentifier({ name: '__d' })) {
if (path.parent.type !== 'CallExpression') {
return;
}
// Handle calls to __d() module definition function
const { code, map } = generate(path.parent);
modules.push({
name: path.parent.arguments[0].value,
code
});
// Remove the module from the AST
path.parentPath.remove();
}
}
});
// Write out module files
modules.forEach((module) => {
fs.writeFileSync(path.join(amdPath, `${module.name}.js`), module.code);
});
// Write out miscellaneous non-module code/module loader code
fs.writeFileSync(path.join(amdPath, `entry.js`), generate(fb).code);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment