def _webpack_optimization(property_, version):
    # this must be a webpack_config["optimization"]

    def apply_legacy_uglifyjs_plugin(config):
        inject_array_items_to_object_property_value(
            config, asttypes.String('"plugins"'),
            es5_single('[new webpack.optimize.UglifyJsPlugin({})]'))

    if version < (4, 0, 0):
        try:
            # this indiscriminately extract an assignment that has
            # "minimize": true
            walker.extract(
                property_.right, lambda node:
                (isinstance(node, Assign) and isinstance(node.left, String) and
                 isinstance(node.right, Boolean) and node.left.value ==
                 '"minimize"' and node.right.value == 'true'))
        except TypeError:
            logger.info('dropping unsupported property for webpack %s: {%s}',
                        '.'.join(str(v) for v in version), property_)
        else:
            logger.info(
                'converting unsupported property to a plugin for '
                'webpack %s: {%s}', '.'.join(str(v) for v in version),
                property_)
            return apply_legacy_uglifyjs_plugin
        return
    return property_
def extract_webpack_config_object(fd):
    tree = read_es5(fd)
    obj = walker.extract(tree, lambda node: isinstance(node, asttypes.Object))
    # this manipulates nodes in place to remove anything that cannot be
    # JSON deserialized

    # remove plugins
    for node in walker.filter(
            obj, lambda node:
        (isinstance(node, asttypes.Assign) and
         isinstance(node.right, asttypes.Array) and isinstance(
             node.left, asttypes.String) and node.left.value == '"plugins"')):
        node.right.items = []

    return json.loads(str(obj))
    def __str__(self):
        ast, config_object_node = generate_ast_and_config_node(
            _WEBPACK_KARMA_CONFIG_TEMPLATE)
        karma_node = self.es5()

        try:
            webpack_object_node = walker.extract(
                karma_node,
                lambda node: (isinstance(node, Assign) and isinstance(
                    node.right, Object) and isinstance(node.left, String) and
                              node.left.value == '"webpack"')).right
        except TypeError:
            raise KeyError(
                "'webpack' attribute missing in karma configuration object")

        inject_array_items_to_object_property_value(
            webpack_object_node,
            asttypes.String('"plugins"'),
            es5_single(_WEBPACK_KARMA_CONFIG_PLUGINS),
        )

        config_object_node.properties = karma_node.properties
        return str(ast)
def es5_single(text):
    return walker.extract(es5(text),
                          lambda node: isinstance(node, ExprStatement)).expr
def generate_ast_and_config_node(template, skip=0):
    ast = es5(template)
    config_object_node = walker.extract(ast,
                                        lambda node: isinstance(node, Object),
                                        skip=skip)
    return ast, config_object_node