Commit 5c33c83d authored by rbpotter's avatar rbpotter Committed by Chromium LUCI CQ

optimize_webui: Support passing full URL for host

This allows bundling of extension URL hosts by passing:
chrome-extension://extensionid/ as the host
to fully work (e.g., excluding local files from the bundle).

Bug: 1163956
Change-Id: Ib467c61c1bb507872292376b3508a10f72d8f076
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2618561Reviewed-by: default avatardpapad <dpapad@chromium.org>
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#842286}
parent 51607ae9
...@@ -119,7 +119,8 @@ def _undo_mapping(mappings, url): ...@@ -119,7 +119,8 @@ def _undo_mapping(mappings, url):
# TODO(dbeam): can we make this stricter? # TODO(dbeam): can we make this stricter?
return url return url
def _request_list_path(out_path, host): def _request_list_path(out_path, host_url):
host = host_url[host_url.find('://') + 3:-1]
return os.path.join(out_path, host + '_requestlist.txt') return os.path.join(out_path, host + '_requestlist.txt')
# Get a list of all files that were bundled with polymer-bundler and update the # Get a list of all files that were bundled with polymer-bundler and update the
...@@ -142,7 +143,7 @@ def _update_dep_file(in_folder, args, manifest): ...@@ -142,7 +143,7 @@ def _update_dep_file(in_folder, args, manifest):
# current working directory. # current working directory.
url_mappings = _URL_MAPPINGS + [ url_mappings = _URL_MAPPINGS + [
('/', os.path.relpath(in_path, _CWD)), ('/', os.path.relpath(in_path, _CWD)),
('chrome://%s/' % args.host, os.path.relpath(in_path, _CWD)), (args.host_url, os.path.relpath(in_path, _CWD)),
] ]
deps = [_undo_mapping(url_mappings, u) for u in request_list] deps = [_undo_mapping(url_mappings, u) for u in request_list]
...@@ -158,10 +159,8 @@ def _update_dep_file(in_folder, args, manifest): ...@@ -158,10 +159,8 @@ def _update_dep_file(in_folder, args, manifest):
# Autogenerate a rollup config file so that we can import the plugin and # Autogenerate a rollup config file so that we can import the plugin and
# pass it information about the location of the directories and files to exclude # pass it information about the location of the directories and files to exclude
# from the bundle. # from the bundle.
def _generate_rollup_config(tmp_out_dir, path_to_plugin, in_path, host, def _generate_rollup_config(tmp_out_dir, path_to_plugin, in_path, host_url,
excludes, external_paths): excludes, external_paths):
scheme_end_index = host.find('://')
host_url = 'chrome://%s/' % host if scheme_end_index == -1 else host
rollup_config_file = os.path.join(tmp_out_dir, 'rollup.config.js') rollup_config_file = os.path.join(tmp_out_dir, 'rollup.config.js')
config_content = r''' config_content = r'''
import plugin from '{plugin_path}'; import plugin from '{plugin_path}';
...@@ -211,7 +210,7 @@ def _bundle_v3(tmp_out_dir, in_path, out_path, manifest_out_path, args, ...@@ -211,7 +210,7 @@ def _bundle_v3(tmp_out_dir, in_path, out_path, manifest_out_path, args,
path_to_plugin = os.path.join( path_to_plugin = os.path.join(
os.path.abspath(_HERE_PATH), 'rollup_plugin.js') os.path.abspath(_HERE_PATH), 'rollup_plugin.js')
rollup_config_file = _generate_rollup_config(tmp_out_dir, path_to_plugin, rollup_config_file = _generate_rollup_config(tmp_out_dir, path_to_plugin,
in_path, args.host, excludes, in_path, args.host_url, excludes,
external_paths) external_paths)
rollup_args = [os.path.join(in_path, f) for f in args.js_module_in_files] rollup_args = [os.path.join(in_path, f) for f in args.js_module_in_files]
...@@ -282,7 +281,7 @@ def _bundle_v2(tmp_out_dir, in_path, out_path, manifest_out_path, args, ...@@ -282,7 +281,7 @@ def _bundle_v2(tmp_out_dir, in_path, out_path, manifest_out_path, args,
[ [
'--manifest-out', manifest_out_path, '--manifest-out', manifest_out_path,
'--root', in_path, '--root', in_path,
'--redirect', 'chrome://%s/|%s' % (args.host, in_path + '/'), '--redirect', '%s|%s' % (args.host_url, in_path + '/'),
'--out-dir', os.path.relpath(tmp_out_dir, _CWD).replace('\\', '/'), '--out-dir', os.path.relpath(tmp_out_dir, _CWD).replace('\\', '/'),
'--shell', args.html_in_files[0], '--shell', args.html_in_files[0],
] + in_html_args) ] + in_html_args)
...@@ -325,7 +324,7 @@ def _bundle_v2(tmp_out_dir, in_path, out_path, manifest_out_path, args, ...@@ -325,7 +324,7 @@ def _bundle_v2(tmp_out_dir, in_path, out_path, manifest_out_path, args,
def _optimize(in_folder, args): def _optimize(in_folder, args):
in_path = os.path.normpath(os.path.join(_CWD, in_folder)).replace('\\', '/') in_path = os.path.normpath(os.path.join(_CWD, in_folder)).replace('\\', '/')
out_path = os.path.join(_CWD, args.out_folder).replace('\\', '/') out_path = os.path.join(_CWD, args.out_folder).replace('\\', '/')
manifest_out_path = _request_list_path(out_path, args.host) manifest_out_path = _request_list_path(out_path, args.host_url)
tmp_out_dir = tempfile.mkdtemp(dir=out_path).replace('\\', '/') tmp_out_dir = tempfile.mkdtemp(dir=out_path).replace('\\', '/')
excludes = _BASE_EXCLUDES + [ excludes = _BASE_EXCLUDES + [
...@@ -333,8 +332,8 @@ def _optimize(in_folder, args): ...@@ -333,8 +332,8 @@ def _optimize(in_folder, args):
# URL for both the relative URL and chrome:// URL syntax. # URL for both the relative URL and chrome:// URL syntax.
'strings.js', 'strings.js',
'strings.m.js', 'strings.m.js',
'chrome://%s/strings.js' % args.host, '%s/strings.js' % args.host_url,
'chrome://%s/strings.m.js' % args.host, '%s/strings.m.js' % args.host_url,
] ]
excludes.extend(args.exclude or []) excludes.extend(args.exclude or [])
external_paths = args.external_paths or [] external_paths = args.external_paths or []
...@@ -347,7 +346,8 @@ def _optimize(in_folder, args): ...@@ -347,7 +346,8 @@ def _optimize(in_folder, args):
external_paths) external_paths)
else: else:
# Ensure Polymer 2 and Polymer 3 request lists don't collide. # Ensure Polymer 2 and Polymer 3 request lists don't collide.
manifest_out_path = _request_list_path(out_path, args.host + '-v2') manifest_out_path = _request_list_path(out_path,
args.host_url[:-1] + '-v2/')
pcb_out_paths = [os.path.join(out_path, f) for f in args.html_out_files] pcb_out_paths = [os.path.join(out_path, f) for f in args.html_out_files]
bundled_paths = _bundle_v2(tmp_out_dir, in_path, out_path, bundled_paths = _bundle_v2(tmp_out_dir, in_path, out_path,
manifest_out_path, args, excludes) manifest_out_path, args, excludes)
...@@ -397,6 +397,11 @@ def main(argv): ...@@ -397,6 +397,11 @@ def main(argv):
args.depfile = os.path.normpath(args.depfile) args.depfile = os.path.normpath(args.depfile)
args.input = os.path.normpath(args.input) args.input = os.path.normpath(args.input)
args.out_folder = os.path.normpath(args.out_folder) args.out_folder = os.path.normpath(args.out_folder)
scheme_end_index = args.host.find('://')
if (scheme_end_index == -1):
args.host_url = 'chrome://%s/' % args.host
else:
args.host_url = args.host
manifest_out_path = _optimize(args.input, args) manifest_out_path = _optimize(args.input, args)
......
...@@ -53,7 +53,6 @@ class OptimizeWebUiTest(unittest.TestCase): ...@@ -53,7 +53,6 @@ class OptimizeWebUiTest(unittest.TestCase):
# TODO(dbeam): make it possible to _run_optimize twice? Is that useful? # TODO(dbeam): make it possible to _run_optimize twice? Is that useful?
args = input_args + [ args = input_args + [
'--depfile', os.path.join(self._out_folder, 'depfile.d'), '--depfile', os.path.join(self._out_folder, 'depfile.d'),
'--host', 'fake-host',
'--input', self._tmp_src_dir, '--input', self._tmp_src_dir,
'--out_folder', self._out_folder, '--out_folder', self._out_folder,
] ]
...@@ -157,6 +156,7 @@ import './element_in_dir/element_in_dir.js'; ...@@ -157,6 +156,7 @@ import './element_in_dir/element_in_dir.js';
def testSimpleOptimize(self): def testSimpleOptimize(self):
self._write_files_to_src_dir() self._write_files_to_src_dir()
args = [ args = [
'--host', 'fake-host',
'--html_in_files', 'ui.html', '--html_in_files', 'ui.html',
'--html_out_files', 'fast.html', '--html_out_files', 'fast.html',
'--js_out_files', 'fast.js', '--js_out_files', 'fast.js',
...@@ -172,6 +172,7 @@ import './element_in_dir/element_in_dir.js'; ...@@ -172,6 +172,7 @@ import './element_in_dir/element_in_dir.js';
def testV3SimpleOptimize(self): def testV3SimpleOptimize(self):
self._write_v3_files_to_src_dir() self._write_v3_files_to_src_dir()
args = [ args = [
'--host', 'fake-host',
'--js_module_in_files', 'ui.js', '--js_module_in_files', 'ui.js',
'--js_out_files', 'ui.rollup.js', '--js_out_files', 'ui.rollup.js',
] ]
...@@ -185,6 +186,7 @@ import './element_in_dir/element_in_dir.js'; ...@@ -185,6 +186,7 @@ import './element_in_dir/element_in_dir.js';
resources_path = os.path.join( resources_path = os.path.join(
'gen', 'ui', 'webui', 'resources', 'preprocessed') 'gen', 'ui', 'webui', 'resources', 'preprocessed')
args = [ args = [
'--host', 'fake-host',
'--js_module_in_files', 'ui.js', '--js_module_in_files', 'ui.js',
'--js_out_files', 'ui.rollup.js', '--js_out_files', 'ui.rollup.js',
'--external_paths', 'chrome://resources|%s' % resources_path, '--external_paths', 'chrome://resources|%s' % resources_path,
...@@ -215,6 +217,7 @@ import './element_in_dir/element_in_dir.js'; ...@@ -215,6 +217,7 @@ import './element_in_dir/element_in_dir.js';
''') ''')
args = [ args = [
'--host', 'fake-host',
'--js_module_in_files', 'ui.js', 'lazy.js', '--js_module_in_files', 'ui.js', 'lazy.js',
'--js_out_files', 'ui.rollup.js', 'lazy.rollup.js', 'shared.rollup.js', '--js_out_files', 'ui.rollup.js', 'lazy.rollup.js', 'shared.rollup.js',
'--out-manifest', os.path.join(self._out_folder, 'out_manifest.json'), '--out-manifest', os.path.join(self._out_folder, 'out_manifest.json'),
...@@ -259,6 +262,7 @@ import './element_in_dir/element_in_dir.js'; ...@@ -259,6 +262,7 @@ import './element_in_dir/element_in_dir.js';
resources_path = os.path.join( resources_path = os.path.join(
'gen', 'ui', 'webui', 'resources', 'preprocessed') 'gen', 'ui', 'webui', 'resources', 'preprocessed')
args = [ args = [
'--host', 'fake-host',
'--js_module_in_files', 'ui.js', '--js_module_in_files', 'ui.js',
'--js_out_files', 'ui.rollup.js', '--js_out_files', 'ui.rollup.js',
'--external_paths', '--external_paths',
...@@ -284,5 +288,23 @@ import './element_in_dir/element_in_dir.js'; ...@@ -284,5 +288,23 @@ import './element_in_dir/element_in_dir.js';
'external_element_dep.js')), 'external_element_dep.js')),
depfile_d) depfile_d)
def testV3SimpleOptimizeExcludes(self):
self._write_v3_files_to_src_dir()
args = [
'--host', 'chrome-extension://myextensionid/',
'--js_module_in_files', 'ui.js',
'--js_out_files', 'ui.rollup.js',
'--exclude', 'element_in_dir/element_in_dir.js',
]
self._run_optimize(args)
output_js = self._read_out_file('ui.rollup.js')
self.assertIn('yay', output_js)
self.assertNotIn('hello from element_in_dir', output_js)
depfile_d = self._read_out_file('depfile.d')
self.assertIn('element.js', depfile_d)
self.assertNotIn('element_in_dir', depfile_d)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment