Skip to content

Instantly share code, notes, and snippets.

@petehunt
Created July 3, 2013 16:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petehunt/5920306 to your computer and use it in GitHub Desktop.
Save petehunt/5920306 to your computer and use it in GitHub Desktop.
import os
import json
import subprocess
import sys
def relativize(path):
return os.path.relpath(os.path.abspath(path), os.path.abspath('.'))[:-3]
DEPS_MEMO = {}
def get_deps(module):
if module in DEPS_MEMO:
return DEPS_MEMO[module]
deps = subprocess.check_output(['browserify', '--deps', module])
dep_map = json.loads(deps)
rv = (relativize(dep['id']) for dep in dep_map)
DEPS_MEMO[module] = rv
return rv
def package(modules, bundle, excludes):
modules_in_bundle = set()
cmd = ['browserify']
for exclude in excludes:
cmd.append('-x')
cmd.append(exclude)
for module in modules:
modules_in_bundle.update(get_deps(module))
cmd.append(module)
packaged = subprocess.check_output(cmd)
with open(bundle, 'w') as f:
f.write(packaged)
return modules_in_bundle - excludes
def package_all(descriptions):
bundled_deps = set()
load_map = {} # map module id -> required bundles
for bundle, modules in descriptions:
modules_in_bundle = package(modules, bundle, bundled_deps)
for module in modules_in_bundle:
required_bundles = set([bundle])
deps = list(get_deps(module))
for dep in deps[1:]:
if dep in load_map:
required_bundles.update(load_map[dep])
load_map[module] = list(required_bundles)
bundled_deps.update(modules_in_bundle)
return load_map
def parse_args(argv):
for arg in argv:
bundle, module_string = arg.split(':', 1)
modules = module_string.split(',')
yield bundle, modules
if __name__ == '__main__':
print json.dumps(package_all(list(parse_args(sys.argv[1:]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment