Skip to content

Instantly share code, notes, and snippets.

@bregenspan
Last active February 11, 2024 22:50
Show Gist options
  • Save bregenspan/ce384f295c76c0330d37faeefdd86193 to your computer and use it in GitHub Desktop.
Save bregenspan/ce384f295c76c0330d37faeefdd86193 to your computer and use it in GitHub Desktop.
# 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