Esempio n. 1
0
def _bundle_v3(tmp_out_dir, in_path, out_path, manifest_out_path, args,
               excludes, external_paths):
  if not os.path.exists(tmp_out_dir):
    os.makedirs(tmp_out_dir)
  path_to_plugin = os.path.join(
      os.path.abspath(_HERE_PATH), 'tools', 'rollup_plugin.js')
  rollup_config_file = _generate_rollup_config(tmp_out_dir, path_to_plugin,
                                               in_path, args.host, excludes,
                                               external_paths)
  rollup_args = [os.path.join(in_path, f) for f in args.js_module_in_files]

  # Confirm names are as expected. This is necessary to avoid having to replace
  # import statements in the generated output files.
  # TODO(rbpotter): Is it worth adding import statement replacement to support
  # arbitrary names?
  bundled_paths = []
  for index, js_file in enumerate(args.js_module_in_files):
    base_file_name = os.path.basename(js_file)
    expected_name = '%s.rollup.js' % base_file_name[:-len('.js')]
    assert args.js_out_files[index] == expected_name, \
           'Output file corresponding to %s should be named %s' % \
           (js_file, expected_name)
    bundled_paths.append(os.path.join(tmp_out_dir, expected_name))

  # This indicates that rollup is expected to generate a shared chunk file as
  # well as one file per module. Set its name using --chunkFileNames. Note:
  # Currently, this only supports 2 entry points, which generate 2 corresponding
  # outputs and 1 shared output.
  if (len(args.js_out_files) == 3):
    assert len(args.js_module_in_files) == 2, \
           'Expect 2 module entry points for generating 3 outputs'
    shared_file_name = args.js_out_files[2]
    rollup_args += [ '--chunkFileNames', shared_file_name ]
    bundled_paths.append(os.path.join(tmp_out_dir, shared_file_name))

  node.RunNode(
      [node_modules.PathToRollup()] + rollup_args + [
          '--format', 'esm',
          '--dir', tmp_out_dir,
          '--entryFileNames', '[name].rollup.js',
          '--sourcemap', '--sourcemapExcludeSources',
          '--config', rollup_config_file,
          '--silent',
      ])

  # Create the manifest file from the sourcemaps generated by rollup.
  generated_paths = _generate_manifest_file(tmp_out_dir, in_path,
                                            manifest_out_path)
  assert len(generated_paths) == len(bundled_paths), \
         'unexpected number of bundles - %s - generated by rollup' % \
         (len(generated_paths))

  for bundled_file in bundled_paths:
    with open(bundled_file, 'r') as f:
      output = f.read()
      assert "<if expr" not in output, \
          'Unexpected <if expr> found in bundled output. Check that all ' + \
          'input files using such expressions are preprocessed.'

  return bundled_paths
def _bundle_v3(tmp_out_dir, in_path, out_path, manifest_out_path, args,
               excludes):
    if not os.path.exists(tmp_out_dir):
        os.makedirs(tmp_out_dir)
    path_to_plugin = os.path.join(os.path.abspath(_HERE_PATH), 'tools',
                                  'rollup_plugin.js')
    rollup_config_file = _generate_rollup_config(tmp_out_dir, path_to_plugin,
                                                 in_path, args.host, excludes)
    bundled_paths = []
    for index, js_module_in_file in enumerate(args.js_module_in_files):
        js_out_file = args.js_out_files[index]
        rollup_js_out_file = '%s.rollup.js' % js_out_file[:-3]
        rollup_js_out_path = os.path.join(tmp_out_dir, rollup_js_out_file)
        node.RunNode([node_modules.PathToRollup()] + [
            '--format',
            'esm',
            '--input',
            os.path.join(in_path, js_module_in_file),
            '--file',
            rollup_js_out_path,
            '--sourcemap',
            '--sourcemapExcludeSources',
            '--config',
            rollup_config_file,
            '--silent',
        ])

        # Create the manifest file from the sourcemap generated by rollup.
        _generate_manifest_file(rollup_js_out_file, tmp_out_dir, in_path,
                                manifest_out_path)

        bundled_paths.append(rollup_js_out_path)
    return bundled_paths
Esempio n. 3
0
def _bundle_v3(tmp_out_dir, in_path, out_path, manifest_out_path, args,
               excludes):
  if not os.path.exists(tmp_out_dir):
    os.makedirs(tmp_out_dir)
  path_to_plugin = os.path.join(
      os.path.abspath(_HERE_PATH), 'tools', 'rollup_plugin.js')
  rollup_config_file = _generate_rollup_config(tmp_out_dir, path_to_plugin,
                                               in_path, args.host, excludes)
  bundled_paths = []
  for index, js_module_in_file in enumerate(args.js_module_in_files):
    js_out_file = args.js_out_files[index]
    rollup_js_out_file = '%s.rollup.js' % js_out_file[:-3]
    rollup_js_out_path = os.path.join(tmp_out_dir, rollup_js_out_file)
    node.RunNode(
        [node_modules.PathToRollup()] + [
            '--format', 'esm',
            '--input', os.path.join(in_path, js_module_in_file),
            '--file', rollup_js_out_path,
            '--sourcemap', '--sourcemapExcludeSources',
            '--config', rollup_config_file,
            '--silent',
        ])

    # Copy the HTML file and replace the script name.
    html_file = args.html_in_files[index]
    html_out_file = args.html_out_files[index]
    with open(os.path.join(in_path, html_file), 'r') as f:
      output = f.read()
      output = output.replace(js_module_in_file, js_out_file);
      with open(os.path.join(out_path, html_out_file), 'w') as f:
        f.write(output)
        f.close()

    # Create the manifest file from the sourcemap generated by rollup.
    _generate_manifest_file(rollup_js_out_file, tmp_out_dir, in_path,
                            manifest_out_path)

    bundled_paths.append(rollup_js_out_path)
  return bundled_paths
Esempio n. 4
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')
        node.RunNode([
            node_modules.PathToRollup(),
            # 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.PathToTerser(),
            bundled_js,
            # TODO(dpapad): Figure out a way to deduplicate LICENSE headers. In the
            # meantime exclude such comments to reduce file size.
            '--comments',
            'false',
            #'--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)