0

I have minification applied in javascript file. I want to wrap that minified file with

(function(){ file content here })() . OR

Any other way I can achieve this.

Basically I want my files to be anonymously wrapped.

I am using following maven plugin.

<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>yuicompressor-maven-plugin</artifactId>
    <configuration>
        <preProcessAggregates>true</preProcessAggregates>
        <encoding>UTF-8</encoding>
        <disableOptimizations>false</disableOptimizations>
        <failOnWarning>false</failOnWarning>
        <force>true</force>
        <gzip>false</gzip>
        <nomunge>true</nomunge>
        <jswarn>false</jswarn>
        <nosuffix>true</nosuffix>
        <useProcessedResources>true</useProcessedResources>
        <aggregations>
            <aggregation>
                <insertNewLine>false</insertNewLine>
                <output>${basedir}/target/web/js/minified.js</output>
                <inputDir>${basedir}/src/main/webapp/js/app</inputDir>
                <includes>
                    <include>*.js</include>
                </includes>
            </aggregation>
        </aggregations>
        <excludes>
            <exclude>**/lib/**</exclude>
            <exclude>**/*-min.js</exclude>
            <exclude>**/*.min.js</exclude>
            <exclude>**/*-min.css</exclude>
            <exclude>**/*.min.css</exclude>
        </excludes>
        <sourceDirectory>${basedir}/target</sourceDirectory>
        <outputDirectory>${basedir}/target</outputDirectory>
    </configuration>
</plugin>

EDIT I can achieve this with putting every files in anonymous function and at build time, merging and minifying. But instead of putting this in every file, i want to put this only on merged file.

I want to wrap in anonymous function so that code cannot be accessible through console.

1
  • 1
    Why would you like to do this? I need to understand to help you better Commented Jul 8, 2015 at 15:41

1 Answer 1

1

You should use an asset pipeline tool to take a deep control over your configuration.

You can consider to use WUIC and extend it according to your need.

You can use the maven-plugin, enable YUICompressor support and then just write a custom ObjectBuilderInspector that will specify to the aggregator your custom Transformer that wraps your content.

Please find some piece of code bellow, you will find the documentation here and the samples here and here.

pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>com.github.wuic.plugins</groupId>
            <artifactId>static-helper-maven-plugin</artifactId>
            <version>${project.version}</version>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>process</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <xml>src/main/resources/wuic.xml</xml>
                <properties>src/main/resources/wuic.properties</properties>
                <output>${project.build.finalName}</output>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>com.github.wuic.extensions</groupId>
                    <artifactId>wuic-yuicompressor</artifactId>
                    <version>${project.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

wuic.xml:

<wuic>
    <heaps>
        <heap id="scripts">
            <nut-path>*/lib/*</nut-path>
            <nut-path>*/*-min.js</nut-path>
            <nut-path>*/*.min.js</nut-path>
            <nut-path>*/*-min.css</nut-path>
            <nut-path>*/*.min.css</nut-path>
        </heap>
    </heaps>
</wuic>

wuic.properties

# for resources in classpath (src/main/resources)
c.g.wuic.dao.wildcard=true
c.g.wuic.dao.basePath=/js/app
c.g.wuic.facade.additionalBuilderInspectorClasses=com.your.app.MyInspector

Your custom transformer that is plugged thanks to an inspector

public class MyInspector implements ObjectBuilderInspector, Pipe.Transformer<ConvertibleNut> {

        @Override
    public <T> T inspect(T object) {
        if (object instanceof TextAggregatorEngine) {
            TextAggregatorEngine.class.cast(object).addTransformer(this);
        }

        return object;
    }

    @Override
    public void transform(InputStream is, OutputStream os, ConvertibleNut convertible) throws IOException {
        if (convertible.getNutType().equals(NutType.JAVASCRIPT)) {
            os.write("(function(){".getBytes());
            IOUtils.copyStream(is, os);
            os.write("})();".getBytes());
        } else {
            IOUtils.copyStream(is, os);
        }
    }

    @Override
    public boolean canAggregateTransformedStream() {
        return false;
    }
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.