2

我正在为 maven 使用 yui-compressor 插件,但似乎无法对其进行压缩。所有 js 文件的附加工作正常。它不会删除注释、换行符,也不会缩小 js(即将 var myVar 转换为 var a)。我的配置有问题吗?

    <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>yuicompressor-maven-plugin</artifactId>
                    <version>1.1</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compress</goal>
                            </goals>
                            <configuration>
                                <jswarn>false</jswarn>
                                <disableOptimizations>false</disableOptimizations>
                                <insertNewLine>false</insertNewLine>
                                <preserveAllSemiColons>false</preserveAllSemiColons>
                                <aggregations>
                                    <aggregation>
                                        <removeIncluded>true</removeIncluded>
                                        <!-- insert new line after each concatenation (default: false) -->
                                        <output>${project.build.directory}/${project.build.finalName}/WEB-INF/scripts/all.js</output>
                                        <!-- files to include, path relative to output's directory or absolute 
                                            path -->
                                        <!--inputDir>base directory for non absolute includes, default to 
                                            parent dir of output</inputDir -->
                                        <includes>
                                            <include>${basedir}/src/main/webapp/WEB-INF/scripts/underscore.js</include>
<include>${basedir}/src/main/webapp/WEB-INF/scripts/backbone.dev.js</include>
<include>${basedir}/src/main/webapp/WEB-INF/scripts/modernizr.custom.83543.js</include>
<include>${basedir}/src/main/webapp/WEB-INF/scripts/jquery.slider.min.js</include>
<include>${basedir}/src/main/webapp/WEB-INF/scripts/myApp.js</include>
                                        </includes>
                                        <!-- files to exclude, path relative to output's directory <excludes> 
                                            <exclude>**/*.pack.js</exclude> <exclude>**/compressed.css</exclude> </excludes> -->
                                    </aggregation>
                                </aggregations>
                                <includes>
                                    <include>${basedir}/src/main/webapp/WEB-INF/scripts/*.js</include>
                                </includes>
                                <excludes>
                                    <exclude>${basedir}/src/main/webapp/WEB-INF/scripts/*min*.js</exclude>
                                    <exclude>${basedir}/src/main/webapp/WEB-INF/scripts/underscore.js</exclude>
                                    <exclude>${basedir}/src/main/webapp/WEB-INF/scripts/backbone.js</exclude>
                                    <exclude>${basedir}/src/main/webapp/WEB-INF/scripts/modernizr*.js</exclude>
                                    <exclude>${basedir}/src/main/webapp/WEB-INF/scripts/*fancybox*.js</exclude>
                                    <exclude>${basedir}/src/main/webapp/WEB-INF/scripts/jquery.easing*.js</exclude>
                                </excludes>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
4

2 回答 2

1

我弄清楚发生了什么事。实际上,有一些问题是错误的:首先,整个配置块需要与执行块相邻,而不是在其中。其次,insertNewLine 选项需要在聚合块内。第三,没有被删除的评论是 /*! */ 注释,通常包含许可信息并且 yui-compressor 不会删除。第四,也是最重要的一点,myApp.js 中有一个 eval,它被认为是“邪恶的”并阻止 yui-compressor 压缩。

希望这对人们有所帮助!

于 2012-08-03T05:31:47.087 回答
0

我建议您使用 jshint 插件并 lint 您的 js 代码作为压缩 js 的上一步。如果检测到 eval、结束错误的逗号等,则您的 maven 构建在尝试压缩之前失败。

        <plugin>
            <groupId>com.cj.jshintmojo</groupId>
            <artifactId>jshint-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>lint</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <options>forin,sub,evil,latedef,eqnull</options>
                <directories>
                    <directory>src/main/webapp/</directory>
                </directories>
                <excludes>
                    <exclude>src/main/webapp/extjs-4.2</exclude>
                </excludes>
                <reporter>jslint</reporter>
                <reportFile>target/jshint.xml</reportFile>
                <failOnError>true</failOnError>
            </configuration>
        </plugin>
于 2017-06-02T06:15:31.337 回答