Skip to content

Commit

Permalink
Merge branch 'jls/bisect' into autostaging
Browse files Browse the repository at this point in the history
  • Loading branch information
orangejenny committed Mar 27, 2024
2 parents 08a3a9d + 438a397 commit d8754e1
Showing 1 changed file with 42 additions and 38 deletions.
80 changes: 42 additions & 38 deletions corehq/apps/hqwebapp/management/commands/build_requirejs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
ROOT_DIR = settings.FILEPATH
BUILD_JS_FILENAME = "staticfiles/build.js"
BUILD_TXT_FILENAME = "staticfiles/build.txt"
BOOTSTRAP_VERSIONS = ['bootstrap3', 'bootstrap5']


class Command(ResourceStaticCommand):
Expand All @@ -37,14 +38,16 @@ def add_arguments(self, parser):
'Does not allow you to mimic CDN.')
parser.add_argument('--no_optimize', action='store_true',
help='Don\'t minify files. Runs much faster. Useful when running on a local environment.')
parser.add_argument('--bootstrap_version',
default="bootstrap3",
help="Specify bootstrap3 or bootstrap5 (bootstrap3 is default)")
parser.add_argument('--bootstrap_version', default="bootstrap3",
help="Specify bootstrap3 or bootstrap5 (bootstrap3 is default)")

def handle(self, **options):
bootstrap_version = options.get('bootstrap_version')
logger.setLevel('DEBUG')

if options.get('bootstrap_version') == "bootstrap5":
logger.warning("Running with bootstrap5 is no longer necessary")
exit()

local = options['local']
verbose = options['verbosity'] > 1
optimize = not options['no_optimize']
Expand All @@ -58,41 +61,42 @@ def handle(self, **options):
if (not resource_versions):
raise ResourceVersionsNotFoundException()

config, local_js_dirs = _r_js(local=local, verbose=verbose, bootstrap_version=bootstrap_version)
if optimize:
_minify(config, verbose=verbose)

if local:
_copy_modules_back_into_corehq(config, local_js_dirs)

filename = os.path.join(ROOT_DIR, 'staticfiles', 'hqwebapp', 'js',
bootstrap_version, 'requirejs_config.js')
resource_versions[f"hqwebapp/js/{bootstrap_version}/requirejs_config.js"] = self.get_hash(filename)
if local:
dest = os.path.join(ROOT_DIR, 'corehq', 'apps', 'hqwebapp', 'static',
'hqwebapp', 'js', bootstrap_version, 'requirejs_config.js')
copyfile(filename, dest)
logger.info(f"Copied updated {bootstrap_version}/requirejs_config.js back into {_relative(dest)}")

# Overwrite each bundle in resource_versions with the sha from the optimized version in staticfiles
for module in config['modules']:
filename = os.path.join(ROOT_DIR, 'staticfiles', module['name'] + ".js")

# TODO: it'd be a performance improvement to do this after the `open` below
# and pass in the file contents, since get_hash does another read.
file_hash = self.get_hash(filename)

for bootstrap_version in BOOTSTRAP_VERSIONS:
config, local_js_dirs = _r_js(local=local, verbose=verbose, bootstrap_version=bootstrap_version)
if optimize:
# Overwrite source map reference. Source maps are accessed on the CDN,
# so they need to have the version hash appended.
with open(filename, 'r') as fin:
lines = fin.readlines()
with open(filename, 'w') as fout:
for line in lines:
if re.search(r'sourceMappingURL=bundle.js.map$', line):
line = re.sub(r'bundle.js.map', 'bundle.js.map?version=' + file_hash, line)
fout.write(line)
resource_versions[module['name'] + ".js"] = file_hash
_minify(config, verbose=verbose)

if local:
_copy_modules_back_into_corehq(config, local_js_dirs)

filename = os.path.join(ROOT_DIR, 'staticfiles', 'hqwebapp', 'js',
bootstrap_version, 'requirejs_config.js')
resource_versions[f"hqwebapp/js/{bootstrap_version}/requirejs_config.js"] = self.get_hash(filename)
if local:
dest = os.path.join(ROOT_DIR, 'corehq', 'apps', 'hqwebapp', 'static',
'hqwebapp', 'js', bootstrap_version, 'requirejs_config.js')
copyfile(filename, dest)
logger.info(f"Copied updated {bootstrap_version}/requirejs_config.js back into {_relative(dest)}")

# Overwrite each bundle in resource_versions with the sha from the optimized version in staticfiles
for module in config['modules']:
filename = os.path.join(ROOT_DIR, 'staticfiles', module['name'] + ".js")

# TODO: it'd be a performance improvement to do this after the `open` below
# and pass in the file contents, since get_hash does another read.
file_hash = self.get_hash(filename)

if optimize:
# Overwrite source map reference. Source maps are accessed on the CDN,
# so they need to have the version hash appended.
with open(filename, 'r') as fin:
lines = fin.readlines()
with open(filename, 'w') as fout:
for line in lines:
if re.search(r'sourceMappingURL=bundle.js.map$', line):
line = re.sub(r'bundle.js.map', 'bundle.js.map?version=' + file_hash, line)
fout.write(line)
resource_versions[module['name'] + ".js"] = file_hash

# Write out resource_versions.js for all js files in resource_versions
# Exclude formdesigner directory, which contains a ton of files, none of which are required by HQ
Expand Down

0 comments on commit d8754e1

Please sign in to comment.