Example #1
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)
    tmp_out_dir = tempfile.mkdtemp(dir=out_path).replace('\\', '/')

    excludes = _BASE_EXCLUDES + [
        # This file is dynamically created by C++. Need to specify an exclusion
        # URL for both the relative URL and chrome:// URL syntax.
        'strings.js',
        'strings.m.js',
        'chrome://%s/strings.js' % args.host,
        'chrome://%s/strings.m.js' % args.host,
    ]
    excludes.extend(args.exclude or [])

    try:
        if args.js_module_in_files:
            pcb_out_paths = [
                os.path.join(tmp_out_dir, f) for f in args.js_out_files
            ]
            bundled_paths = _bundle_v3(tmp_out_dir, in_path, out_path,
                                       manifest_out_path, args, excludes)
        else:
            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,
                                       manifest_out_path, args, excludes)

        # Run polymer-css-build.
        node.RunNode([node_modules.PathToPolymerCssBuild()] +
                     ['--polymer-version', '2'] +
                     ['--no-inline-includes', '-f'] + bundled_paths + ['-o'] +
                     pcb_out_paths)

        # Pass the JS files through Uglify and write the output to its final
        # destination.
        for index, js_out_file in enumerate(args.js_out_files):
            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
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', '--in-html',
            os.path.join(tmp_dir, 'polymer.html'), '--out-html',
            os.path.join(tmp_out_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.PathToUglify(), 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)
  def __StripJsComments(filename):
    """Returns the minified contents of a JavaScript file with comments and
    grit directives removed.

    Args:
      filename: The name of the file to read.

    Returns:
      A string consisting of the minified file contents with comments and grit
      directives removed.
    """
    with open(filename) as f:
      text = f.read()
    text = re.sub('<if .*?>', '', text, flags=re.IGNORECASE)
    text = re.sub('</if>', '', text, flags=re.IGNORECASE)

    return node.RunNode([node_modules.PathToUglify(), filename])
Example #4
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
Example #5
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
Example #6
0
def main():
    polymer_dir = os.path.join(_HERE_PATH, 'components-chromium', 'polymer')

    # Copy the top-level Polymer file that holds all dependencies. This file is
    # not distributed via NPM, it only exists within third_party/polymer
    # repository.
    shutil.copy(os.path.join(polymer_dir, '..', '..', 'polymer.js'),
                polymer_dir)

    # Move the entire checkout to a temp location.
    tmp_dir = os.path.join(_HERE_PATH, 'components-chromium', 'polymer_temp')
    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 JS bundle file.
        bundled_js = os.path.join(tmp_out_dir, 'polymer_bundled.js')
        path_to_rollup = os.path.join('node_modules', 'rollup', 'bin',
                                      'rollup')

        node.RunNode([
            path_to_rollup,
            # See https://github.com/rollup/rollup/issues/1955
            '--silent',
            '--format',
            'esm',
            '--input',
            os.path.join(tmp_dir, 'polymer.js'),
            '--file',
            bundled_js,
        ])

        # Minify the JS bundle.
        minified_js = os.path.join(tmp_out_dir, 'polymer_bundled.min.js')
        node.RunNode([
            node_modules.PathToUglify(),
            bundled_js,
            # TODO(dpapad): Figure out a way to deduplicate LICENSE headers.
            #'--comments', '/Copyright|license|LICENSE/',
            '--output',
            minified_js
        ])

        # Copy generated JS bundle back to the original location.
        os.makedirs(polymer_dir)
        shutil.move(minified_js, polymer_dir)

        # Copy LICENSE file.
        shutil.copy(os.path.join(tmp_dir, 'LICENSE.txt'), polymer_dir)

        # Copy files needed for type checking.
        # - |bundled_js| is the JS bundle with JS type annotations.
        # - various externs files
        shutil.copy(bundled_js, polymer_dir)
        externs_to_copy = [
            os.path.join(tmp_dir, 'externs', 'closure-types.js'),
            os.path.join(tmp_dir, 'externs', 'polymer-dom-api-externs.js'),
            os.path.join(tmp_dir, 'externs', 'polymer-externs.js'),
            os.path.join(tmp_dir, 'externs', 'webcomponents-externs.js'),
            os.path.join(polymer_dir, '..', 'shadycss', 'externs',
                         'shadycss-externs.js'),
        ]
        externs_dir = os.path.join(polymer_dir, 'externs')
        os.makedirs(externs_dir)
        for extern in externs_to_copy:
            shutil.copy(extern, externs_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)
Example #7
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)

  html_out_path = os.path.join(out_path, args.html_out_file)
  js_out_path = os.path.join(out_path, args.js_out_file)

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

  output = node.RunNode(
      [node_modules.PathToVulcanize()] +
      _VULCANIZE_BASE_ARGS + _VULCANIZE_REDIRECT_ARGS + exclude_args +
      ['--out-request-list', _request_list_path(out_path, args.html_out_file),
       '--redirect', '"/|%s"' % in_path,
       '--redirect', '"chrome://%s/|%s"' % (args.host, in_path),
       # TODO(dpapad): Figure out why vulcanize treats the input path
       # differently on Windows VS Linux/Mac.
       os.path.join(
           in_path if platform.system() == 'Windows' else os.sep,
           args.html_in_file)])

  # 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): Vulcanize 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)

  crisper_input = tempfile.NamedTemporaryFile(mode='wt+', delete=False)
  crisper_input.write(output)
  crisper_input.close()

  crisper_output = tempfile.NamedTemporaryFile(mode='wt+', delete=False)
  crisper_output.close()

  try:
    node.RunNode([node_modules.PathToCrisper(),
                 '--source', crisper_input.name,
                 '--script-in-head', 'false',
                 '--only-split',
                 '--html', html_out_path,
                 '--js', crisper_output.name])

    # Crisper by default inserts a <script> tag with the name of the --js file,
    # but since we are using a temporary file, need to manually insert a
    # <script> tag with the correct final filename (in combination with
    # --only-split flag). There is no way currently to manually specify the
    # <script> tag's path, see https://github.com/PolymerLabs/crisper/issues/46.
    with open(html_out_path, 'r+') as f:
      data = f.read()
      new_data = data.replace(
          '</body></html>',
          '<script src="' + args.js_out_file + '"></script></body></html>')
      assert new_data != data, 'Expected to find </body></html> token.'
      f.seek(0)
      f.write(new_data)
      f.truncate()

    node.RunNode([node_modules.PathToUglify(), crisper_output.name,
                  '--comments', '"/Copyright|license|LICENSE|\<\/?if/"',
                  '--output', js_out_path])
  finally:
    if os.path.exists(crisper_input.name):
      os.remove(crisper_input.name)
    if os.path.exists(crisper_output.name):
      os.remove(crisper_output.name)