Skip to content

Instantly share code, notes, and snippets.

@g-i-o-
Last active January 20, 2019 03:33
Show Gist options
  • Save g-i-o-/9311f428c4ab7fc1c90f6df8ff1e508a to your computer and use it in GitHub Desktop.
Save g-i-o-/9311f428c4ab7fc1c90f6df8ff1e508a to your computer and use it in GitHub Desktop.
Preparing a bunch of AngularJS modules for webpack
import os
import os.path
import re
import ast
def parse_angular_file(file):
with open(file, 'r') as finp:
contents = finp.read()
matches = re.findall('angular.module\w*\(([^)]+)\)', contents)
if matches:
modules, dependencies = zip(*map(lambda x: ast.literal_eval(x.replace('//', '# ')), matches))
dependencies = [x for y in dependencies for x in y]
else:
modules, dependencies = [], []
return {
'file': file,
'modules': modules,
'dependencies': dependencies,
'contents': contents
}
# def replace_angular_modules(contents):
# def repl(match):
# module, dependencies = ast.literal_eval(match.group(1).replace('//', '# '))
# return "angular.module('%s', [\n %s\n])" % (module, ',\n '.join(map(sanitize_module, dependencies)))
#
# return re.subn('angular.module\w*\(([^)]+)\)', repl, contents)[0]
def parse_all_angular_files(path):
return [
parse_angular_file(os.path.join(subpath, file))
for subpath, folders, files in os.walk(path)
for file in files
if file[-3:] == '.js'
]
def build_module_map(angular_files):
modulect = {}
for item in angular_files:
for module in item['modules']:
modulect[module] = modulect.get(module, 0) + 1
repeatedmodules = [m for m, ct in modulect.items() if ct > 1]
if len(repeatedmodules):
raise ValueError("Modules %s are repeated" % ', '.join(repeatedmodules))
return {
module: item['file']
for item in angular_files
for module in item['modules']
}
def sanitize_module(x):
return 'module_' + x.replace('.', '__').replace('-', '_')
def resolve_angular_module_dependencies(path):
jsfiles = parse_all_angular_files(path)
modulefilemap = build_module_map(jsfiles)
for item in jsfiles:
item['resolutions'] = [(
"var %s = require('%s'); // module '%s'" % (sanitize_module(module), os.path.join('.', os.path.relpath(modulefilemap[module], os.path.dirname(item['file']))), module)
if module in modulefilemap
else "// unresolved module '%s'" % module
)
for idx, module in enumerate(item['dependencies'])
]
return jsfiles
SENTRY = '// module resolutions'
def remove_resolutions(contents):
split = contents.split(SENTRY)
return split[0] + ''.join('\n\n'.join(x.split('\n\n')[1:]) for x in split[1:])
def concat_resolutions(jsfile):
return jsfile['file'], ('\n'.join([
SENTRY,
'\n'.join(jsfile['resolutions']),
'',
(
jsfile['contents']
if SENTRY not in jsfile['contents']
else remove_resolutions(jsfile['contents'])
)
])
)
## to run, toggle comments
for file, contents in map(concat_resolutions, resolve_angular_module_dependencies('./assets'))[:1]:
# for file, contents in map(concat_resolutions, resolve_angular_module_dependencies('./assets')):
# print file
# print contents
#with open(file, 'w') as fout:
# fout.write(contents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment