Skip to content

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
# 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') {
// Handle calls to __d() module definition function
const { code, map } = generate(path.parent);
name: path.parent.arguments[0].value,
// Remove the module from the AST
// Write out module files
modules.forEach((module) => {
fs.writeFileSync(path.join(amdPath, `${}.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
You can’t perform that action at this time.