Ejemplo 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
Ejemplo n.º 2
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)

    with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp:
        tmp.write(output)

    try:
        node.RunNode([
            node_modules.PathToCrisper(), '--source', tmp.name,
            '--script-in-head', 'false', '--html', html_out_path, '--js',
            js_out_path
        ])

        # Create an empty JS file if crisper did not create one.
        if not os.path.isfile(js_out_path):
            open(js_out_path, 'w').close()

        node.RunNode([
            node_modules.PathToUglifyJs(), js_out_path, '--comments',
            '"/Copyright|license|LICENSE|\<\/?if/"', '--output', js_out_path
        ])
    finally:
        os.remove(tmp.name)
Ejemplo n.º 3
0
def main(original_html):
    name = os_path.splitext(os_path.basename(original_html))[0]
    dst_dir = os_path.dirname(original_html)
    extracted_html = os_path.join(dst_dir, name + '-extracted.html')
    extracted_js = os_path.join(dst_dir, name + '-extracted.js')

    node.RunNode([
        node_modules.PathToCrisper(), '--script-in-head', 'false', '--source',
        original_html, '--html', extracted_html, '--js', extracted_js
    ])

    shutil.move(extracted_html, original_html)
Ejemplo n.º 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)

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

    output = _run_node([node_modules.PathToVulcanize(
    )] + _VULCANIZE_BASE_ARGS + _VULCANIZE_REDIRECT_ARGS + [
        '--out-request-list',
        os.path.join(out_path, _REQUEST_LIST_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
        output = output.replace('<head>', '<head>' + args.insert_in_head)

    with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp:
        tmp.write(output)

    try:
        _run_node([
            node_modules.PathToCrisper(), '--source', tmp.name,
            '--script-in-head', 'false', '--html', html_out_path, '--js',
            js_out_path
        ])

        # TODO(tsergeant): Remove when JS resources are minified by default:
        # crbug.com/619091.
        _run_node([
            node_modules.PathToUglifyJs(), js_out_path, '--comments',
            '"/Copyright|license|LICENSE|\<\/?if/"', '--output', js_out_path
        ])
    finally:
        os.remove(tmp.name)
Ejemplo n.º 5
0
def _vulcanize(directory,
               host,
               html_in_file,
               html_out_file='vulcanized.html',
               js_out_file='crisper.js',
               extra_args=None):
    print 'Vulcanizing %s/%s' % (directory, html_in_file)

    target_path = os.path.join(_HERE_PATH, directory)
    html_in_path = os.path.join(target_path, html_in_file)
    html_out_path = os.path.join(target_path, html_out_file)
    js_out_path = os.path.join(target_path, js_out_file)
    extra_args = extra_args or []

    output = _run_node([node_modules.PathToVulcanize()] +
                       _VULCANIZE_BASE_ARGS + extra_args + [
                           '--redirect',
                           '"chrome://%s/|%s"' %
                           (host, target_path), html_in_path
                       ])

    with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp:
        # Grit includes are not supported, use HTML imports instead.
        tmp.write(output.replace('<include src="', '<include src-disabled="'))

    try:
        _run_node([
            node_modules.PathToCrisper(), '--source', tmp.name,
            '--script-in-head', 'false', '--html', html_out_path, '--js',
            js_out_path
        ])

        # TODO(tsergeant): Remove when JS resources are minified by default:
        # crbug.com/619091.
        _run_node([
            node_modules.PathToUglifyJs(), js_out_path, '--comments',
            '"/Copyright|license|LICENSE|\<\/?if/"', '--output', js_out_path
        ])
    finally:
        os.remove(tmp.name)
Ejemplo n.º 6
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
Ejemplo n.º 7
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
Ejemplo n.º 8
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)