Last active
February 11, 2024 22:50
-
-
Save bregenspan/ce384f295c76c0330d37faeefdd86193 to your computer and use it in GitHub Desktop.
Code for splitting Facebook's SDK into its constituent modules. Used for https://medium.com/@BenRegenspan/why-16-of-the-code-on-the-average-site-belongs-to-facebook-and-what-that-means-68956cd731be / https://docs.google.com/spreadsheets/d/1vdRzi-wlYNQOoAt4bGOseDMt7vgmpv9BoSfy4yM1SYY/edit#gid=873242422
This file contains 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
# 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 |
This file contains 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
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