示例#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_url)
    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',
        '%s/strings.js' % args.host_url,
        '%s/strings.m.js' % args.host_url,
    ]
    excludes.extend(args.exclude or [])
    external_paths = args.external_paths 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,
                                       external_paths)
        else:
            # Ensure Polymer 2 and Polymer 3 request lists don't collide.
            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
            ]
            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.PathToTerser(),
                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 Minify(source):
    # Open two temporary files, so that uglify can read the input from one and
    # write its output to the other.
    with tempfile.NamedTemporaryFile(suffix='.js') as infile, \
         tempfile.NamedTemporaryFile(suffix='.js') as outfile:
        infile.write(source)
        infile.flush()
        node.RunNode([
            node_modules.PathToTerser(), infile.name, '--output', outfile.name
        ])
        result = outfile.read()
        return result
示例#3
0
def main(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument('--out_folder', required=True)
    args = parser.parse_args(argv)

    node.RunNode([
        node_modules.PathToTerser(),
        os.path.join(_HERE_PATH, 'lottie_worker.js'), '--ascii_only=true',
        '-b', 'beautify=false', '--compress', '--mangle',
        'reserved=[\'$\',\'onmessage\',\'postMessage\']', '--output',
        os.path.join(args.out_folder, 'lottie_worker.min.js')
    ])
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)
  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.PathToTerser(), filename])
示例#6
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.target_name)
    tmp_out_dir = tempfile.mkdtemp(dir=out_path).replace('\\', '/')

    excludes = _BASE_EXCLUDES + [
        # This file is dynamically created by C++. Should always be imported with a
        # relative path.
        'strings.m.js',
    ]
    excludes.extend(args.exclude or [])
    external_paths = args.external_paths or []

    try:
        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,
                                   external_paths)

        # 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 Terser and write the output to its final
        # destination.
        for index, js_out_file in enumerate(args.js_out_files):
            node.RunNode([
                node_modules.PathToTerser(),
                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
示例#7
0
def _build(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('\\', '/')
    request_list_path = _request_list_path(out_path, args.host_url)
    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',
        '%s/strings.js' % args.host_url,
        '%s/strings.m.js' % args.host_url,
    ]
    excludes.extend(args.exclude or [])
    external_paths = args.external_paths or []

    try:
        if args.js_module_in_files:
            bundled_paths = build(tmp_out_dir, in_path, out_path,
                                  request_list_path, args, excludes,
                                  external_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.PathToTerser(),
                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 request_list_path
示例#8
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.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)