Exemplo n.º 1
0
def _bundle_v2(tmp_out_dir, in_path, out_path, manifest_out_path, args,
               excludes):
  in_html_args = []
  for f in args.html_in_files:
    in_html_args.append(f)

  exclude_args = []
  for f in excludes:
    exclude_args.append('--exclude')
    exclude_args.append(f);

  node.RunNode(
      [node_modules.PathToBundler()] +
      _VULCANIZE_BASE_ARGS + _VULCANIZE_REDIRECT_ARGS + exclude_args +
      [
       '--manifest-out', manifest_out_path,
       '--root', in_path,
       '--redirect', 'chrome://%s/|%s' % (args.host, in_path + '/'),
       '--out-dir', os.path.relpath(tmp_out_dir, _CWD).replace('\\', '/'),
       '--shell', args.html_in_files[0],
      ] + in_html_args)

  for index, html_file in enumerate(args.html_in_files):
    with open(os.path.join(
        os.path.relpath(tmp_out_dir, _CWD), html_file), 'r') as f:
      output = f.read()

      # Grit includes are not supported, use HTML imports instead.
      output = output.replace('<include src="', '<include src-disabled="')

      if args.insert_in_head:
        assert '<head>' in output
        # NOTE(dbeam): polymer-bundler eats <base> tags after processing.
        # This undoes that by adding a <base> tag to the (post-processed)
        # generated output.
        output = output.replace('<head>', '<head>' + args.insert_in_head)

    # Open file again with 'w' such that the previous contents are
    # overwritten.
    with open(os.path.join(
        os.path.relpath(tmp_out_dir, _CWD), html_file), 'w') as f:
      f.write(output)
      f.close()

  bundled_paths = []
  for index, html_in_file in enumerate(args.html_in_files):
    bundled_paths.append(
        os.path.join(tmp_out_dir, args.html_out_files[index]))
    js_out_file = args.js_out_files[index]

    # Run crisper to separate the JS from the HTML file.
    node.RunNode([node_modules.PathToCrisper(),
                 '--source', os.path.join(tmp_out_dir, html_in_file),
                 '--script-in-head', 'false',
                 '--html', bundled_paths[index],
                 '--js', os.path.join(tmp_out_dir, js_out_file)])
  return bundled_paths
Exemplo n.º 2
0
def main():
    polymer_dir = os.path.join(_HERE_PATH, 'components-chromium', 'polymer2')
    # Final HTML bundle.
    polymer_html = os.path.join(polymer_dir, 'polymer.html')
    # Final JS bundle.
    polymer_js = os.path.join(polymer_dir, 'polymer-extracted.js')

    # Move the entire checkout to a temp location.
    tmp_dir = os.path.join(_HERE_PATH, 'components-chromium', 'polymer2temp')
    if os.path.exists(tmp_dir):
        shutil.rmtree(tmp_dir)
    shutil.move(polymer_dir, tmp_dir)

    tmp_out_dir = os.path.join(tmp_dir, 'out')
    os.makedirs(tmp_out_dir)

    try:
        # Combine everything to a single HTML bundle file.
        node.RunNode([
            node_modules.PathToBundler(),
            '--strip-comments',
            '--inline-scripts',
            '--inline-css',
            '--out-file',
            os.path.join(tmp_out_dir, 'polymer.html'),
            os.path.join(tmp_dir, 'polymer.html'),
        ])

        # Extract the JS to a separate file named polymer-extracted.js.
        extract_inline_scripts.ExtractFrom(
            os.path.join(tmp_out_dir, 'polymer.html'))

        # Minify the JS bundle.
        extracted_js = os.path.join(tmp_out_dir, 'polymer-extracted.js')
        node.RunNode([
            node_modules.PathToTerser(), extracted_js, '--comments',
            '/Copyright|license|LICENSE/', '--output', extracted_js
        ])

        # Copy generated bundled JS/HTML files back to the original location.
        os.makedirs(polymer_dir)
        shutil.move(os.path.join(tmp_out_dir, 'polymer.html'), polymer_html)
        shutil.move(extracted_js, polymer_js)

        # Copy a few more files.
        shutil.move(os.path.join(tmp_dir, 'bower.json'), polymer_dir)
        shutil.move(os.path.join(tmp_dir, 'LICENSE.txt'), polymer_dir)
    finally:
        # Delete component-chromium/shadycss since it ends up in the bundle.
        shutil.rmtree(
            os.path.join(_HERE_PATH, 'components-chromium', 'shadycss'))
        shutil.rmtree(tmp_dir)
Exemplo n.º 3
0
def _vulcanize(in_folder, args):
    in_path = os.path.normpath(os.path.join(_CWD, in_folder))
    out_path = os.path.join(_CWD, args.out_folder)
    manifest_out_path = _request_list_path(out_path, args.host)

    exclude_args = []
    for f in args.exclude or []:
        exclude_args.append('--exclude')
        exclude_args.append(f)

    in_html_args = []
    for f in args.html_in_files:
        in_html_args.append('--in-html')
        in_html_args.append(f)

    tmp_out_dir = os.path.join(out_path, 'bundled')
    node.RunNode(
        [node_modules.PathToBundler()] + _VULCANIZE_BASE_ARGS +
        _VULCANIZE_REDIRECT_ARGS + exclude_args +
        [  # This file is dynamically created by C++. Need to specify an exclusion
            # URL for both the relative URL and chrome:// URL syntax.
            '--exclude',
            'strings.js',
            '--exclude',
            'chrome://%s/strings.js' % args.host,
            '--manifest-out',
            manifest_out_path,
            '--root',
            in_path,
            '--redirect',
            '"chrome://%s/|%s"' % (args.host, in_path),
            '--out-dir',
            os.path.relpath(tmp_out_dir, _CWD),
            '--shell',
            args.html_in_files[0],
        ] + in_html_args)

    for index, html_file in enumerate(args.html_in_files):
        with open(os.path.join(os.path.relpath(tmp_out_dir, _CWD), html_file),
                  'r') as f:
            output = f.read()

            # Grit includes are not supported, use HTML imports instead.
            output = output.replace('<include src="',
                                    '<include src-disabled="')

            if args.insert_in_head:
                assert '<head>' in output
                # NOTE(dbeam): polymer-bundler eats <base> tags after processing. This
                # undoes that by adding a <base> tag to the (post-processed) generated
                # output.
                output = output.replace('<head>',
                                        '<head>' + args.insert_in_head)

        # Open file again with 'w' such that the previous contents are overwritten.
        with open(os.path.join(os.path.relpath(tmp_out_dir, _CWD), html_file),
                  'w') as f:
            f.write(output)
            f.close()

    try:
        for index, html_in_file in enumerate(args.html_in_files):
            html_out_file = args.html_out_files[index]
            js_out_file = args.js_out_files[index]

            # Run crisper to separate the JS from the HTML file.
            node.RunNode([
                node_modules.PathToCrisper(), '--source',
                os.path.join(tmp_out_dir, html_in_file), '--script-in-head',
                'false', '--html',
                os.path.join(tmp_out_dir, html_out_file), '--js',
                os.path.join(tmp_out_dir, js_out_file)
            ])

            # Move the HTML file to its final destination.
            shutil.copy(os.path.join(tmp_out_dir, html_out_file), out_path)

            # Pass the JS file through Uglify and write the output to its final
            # destination.
            node.RunNode([
                node_modules.PathToUglify(),
                os.path.join(tmp_out_dir, js_out_file), '--comments',
                '"/Copyright|license|LICENSE|\<\/?if/"', '--output',
                os.path.join(out_path, js_out_file)
            ])
    finally:
        shutil.rmtree(tmp_out_dir)
    return manifest_out_path
Exemplo n.º 4
0
def _optimize(in_folder, args):
  in_path = os.path.normpath(os.path.join(_CWD, in_folder)).replace('\\', '/')
  out_path = os.path.join(_CWD, args.out_folder).replace('\\', '/')
  manifest_out_path = _request_list_path(out_path, args.host)

  exclude_args = []
  for f in args.exclude or []:
    exclude_args.append('--exclude')
    exclude_args.append(f)

  in_html_args = []
  for f in args.html_in_files:
    in_html_args.append(f)

  tmp_out_dir = os.path.join(out_path, 'bundled').replace('\\', '/')
  node.RunNode(
      [node_modules.PathToBundler()] +
      _VULCANIZE_BASE_ARGS + _VULCANIZE_REDIRECT_ARGS + exclude_args +
      [# This file is dynamically created by C++. Need to specify an exclusion
       # URL for both the relative URL and chrome:// URL syntax.
       '--exclude', 'strings.js',
       '--exclude', 'chrome://%s/strings.js' % args.host,

       '--manifest-out', manifest_out_path,
       '--root', in_path,
       '--redirect', '"chrome://%s/|%s"' % (args.host, in_path + '/'),
       '--out-dir', os.path.relpath(tmp_out_dir, _CWD).replace('\\', '/'),
       '--shell', args.html_in_files[0],
      ] + in_html_args)

  for index, html_file in enumerate(args.html_in_files):
    with open(
        os.path.join(os.path.relpath(tmp_out_dir, _CWD), html_file), 'r') as f:
      output = f.read()

      # Grit includes are not supported, use HTML imports instead.
      output = output.replace('<include src="', '<include src-disabled="')

      if args.insert_in_head:
        assert '<head>' in output
        # NOTE(dbeam): polymer-bundler eats <base> tags after processing. This
        # undoes that by adding a <base> tag to the (post-processed) generated
        # output.
        output = output.replace('<head>', '<head>' + args.insert_in_head)

    # Open file again with 'w' such that the previous contents are overwritten.
    with open(
        os.path.join(os.path.relpath(tmp_out_dir, _CWD), html_file), 'w') as f:
      f.write(output)
      f.close()

  try:
    crisper_html_out_paths = []
    for index, html_in_file in enumerate(args.html_in_files):
      crisper_html_out_paths.append(
          os.path.join(tmp_out_dir, args.html_out_files[index]))
      js_out_file = args.js_out_files[index]

      # Run crisper to separate the JS from the HTML file.
      node.RunNode([node_modules.PathToCrisper(),
                   '--source', os.path.join(tmp_out_dir, html_in_file),
                   '--script-in-head', 'false',
                   '--html', crisper_html_out_paths[index],
                   '--js', os.path.join(tmp_out_dir, js_out_file)])

      if args.replace_for_html_imports_polyfill == js_out_file:
        # Replace the output file with a loader script, to wait until HTML
        # imports are ready before loading.
        with open(crisper_html_out_paths[index], 'r') as f:
          output = f.read()
          output = output.replace(js_out_file + '"',
                                  'chrome://resources/js/crisper_loader.js"' + \
                                  ' data-script-name="' + js_out_file + '"')

          # Preload the final script, even though it will not be evaluated
          # until after crisper_loader.js executes.
          output = output.replace('<head>',
                                  '<head><link rel="preload" href="' + \
                                        js_out_file + '" as="script">')
          f.close()

        # Open file again with 'w' such that the previous contents are
        # overwritten.
        with open(crisper_html_out_paths[index], 'w') as f:
          f.write(output)
          f.close()

      # Pass the JS file through Uglify and write the output to its final
      # destination.
      node.RunNode([node_modules.PathToUglify(),
                    os.path.join(tmp_out_dir, js_out_file),
                    '--comments', '"/Copyright|license|LICENSE|\<\/?if/"',
                    '--output', os.path.join(out_path, js_out_file)])

    # Run polymer-css-build and write the output HTML files to their final
    # destination.
    html_out_paths = [
        os.path.join(out_path, f) for f in args.html_out_files]
    node.RunNode([node_modules.PathToPolymerCssBuild()] +
                 ['--polymer-version', '2'] +
                 ['--no-inline-includes', '-f'] +
                 crisper_html_out_paths + ['-o'] + html_out_paths)
  finally:
    shutil.rmtree(tmp_out_dir)
  return manifest_out_path