如何从uglifying / optimize中排除某些requireJS文件 [英] How to exclude certain requireJS files from uglifying/optimizing

查看:277
本文介绍了如何从uglifying / optimize中排除某些requireJS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在使用grunt进行构建和部署的工作requirejs项目。如果没有使用任何优化,构建工作没有问题,我得到一个大的js文件在生产中部署。



我遇到的问题是,我有一些外部框架(如angularJS),我已经有了它的最小化/优化版本,并且不想再次优化它。

目前没有优化,我通过gruntfile中的单独路径配置包含了此框架的缩小版本。而在我的正常main.js中,我有用于开发的非缩小版本。



现在我想使用优化器来优化我自己的代码,但是不要优化外部框架。但是外部框架应该包含在生成的大JavaScript文件中。基本上我想告诉优化器他在某些情况下应该使用原始文件。



我可以这样做吗?

我只找到了一个全局排除选项,所以一些模块根本不包含在最终的优化js中。



这是我的咕噜配置:

  requirejs:{
编译:{
选项:{
baseUrl:<%= pkg.folders.jsSource%>,
名称:../external-libs/almond-0.1 .1,
包括:main,
mainConfigFile:<%= pkg.folders.jsSource%> /main.js,
out:<%= pkg.folders.build + pkg.name +' - '+ pkg.version%> /js/main.js,
// logLevel:0,
优化:uglify2,
// optimize:none,
paths:{
'angular':'../ external-libs / min / angular-1.0.4',
'jquery': '../external-libs/min/jquery-1.7.2',
'jquery.mobile':'../ external-libs / min / jquery.mobile-1.2.0',
适配器: ../external-libs/min/jquery-mobile-angular-adapter-1.2.0',
'moment':'../external-libs/moment-1.6.2.min',
'iscroll':'../external-libs/min/iscroll-4.2.5',
'iscrollview':'../external-libs/min/jquery.mobile.iscrollview-1.2.6' ,
'add2Home':'../external-libs/min/add2home',
'config / config':config /<%= configDatei%>
}
}
}
},

这是main.js的相关部分:

  require.config({
paths:{
'angular':'../ external-libs / angular-1.0.4',
'jquery':'../ external-libs / jquery-1.7.2',
'jquery .mobile':'../ external-libs / jquery.mobile-1.2.0',
'adapter':'../external-libs/jquery-mobile-angular-adapter-1.2.0',
'moment':'../external-libs/moment-1.6.2.min',
'iscroll':'../external-libs/iscroll-4.2.5',
'iscrollview':'../external-libs/jquery.mobile.iscrollview-1.2.6',
'add2Home':'../external-libs/add2home'
},
shim:{
'angular':{deps:['jquery'],出口:'angular'},
'iscroll':{deps:['jque ry'],出口:'iscroll'},
'iscrollview':{deps:['jquery.mobile','iscroll'],出口:'iscrollview'}
}
} );

感谢您的帮助。

解决方案

只有一些建议使用empty:来指示 NOT 以将模块包含在优化中。

在require.config中将指出min文件的用法。

  requirejs:{
compile:{
options:{
{{all other settings}}
paths:{
'angular':'empty:' ,
}
}
}
},

require.config:{
paths:{
'angular': '../external-libs/min/angular-1.0.4',
},
}

请注意,这是空:,最后冒号,而不是空。如果你省略冒号,你会得到这样的错误:

 没有这样的文件或目录[...] /empty.js

希望这有助于您。


I have a working requirejs project that is using grunt for building and deployment. If using no optimization at all, the build is working without problems and I get one big js file to deploy it on production.

The problem I have is, that I have some external frameworks (like angularJS) where I already have a minimized/optimized version of it and don't want to optimize it again.

Currently without optimization I include the minified version of this framework via a seperate path config in my gruntfile. Whereas in my normal main.js I have the non-minified version for development.

Now I want to use the optimizer to optimize my own code, but to NOT optimize the external frameworks. But the external frameworks should be included in the resulting big javascript file. Basically I want to tell the optimizer that he should use the original file in some cases.

Can I do it something like this?

I only have found a global exclude option, so that some modules are not included in the resulting optimized js at all.

This is my grunt configuration:

requirejs: {
            compile: {
                options: {
                    baseUrl: "<%= pkg.folders.jsSource %>",
                    name: "../external-libs/almond-0.1.1",
                    include: "main",
                    mainConfigFile: "<%= pkg.folders.jsSource %>/main.js",
                    out: "<%= pkg.folders.build + pkg.name + '-' + pkg.version %>/js/main.js",
                    //logLevel: 0,
                    optimize: "uglify2",
                    //optimize: "none",
                    paths: {
                        'angular':'../external-libs/min/angular-1.0.4',
                        'jquery':'../external-libs/min/jquery-1.7.2',
                        'jquery.mobile':'../external-libs/min/jquery.mobile-1.2.0',
                        'adapter': '../external-libs/min/jquery-mobile-angular-adapter-1.2.0',
                        'moment': '../external-libs/moment-1.6.2.min',
                        'iscroll': '../external-libs/min/iscroll-4.2.5',
                        'iscrollview': '../external-libs/min/jquery.mobile.iscrollview-1.2.6',
                        'add2Home': '../external-libs/min/add2home',
                        'config/config': "config/<%=configDatei%>"
                    }
                }
            }
        },

And this is the relevant part of the main.js:

require.config({
        paths:{
            'angular':'../external-libs/angular-1.0.4',
            'jquery':'../external-libs/jquery-1.7.2',
            'jquery.mobile':'../external-libs/jquery.mobile-1.2.0',
            'adapter': '../external-libs/jquery-mobile-angular-adapter-1.2.0',
            'moment': '../external-libs/moment-1.6.2.min',
            'iscroll': '../external-libs/iscroll-4.2.5',
            'iscrollview': '../external-libs/jquery.mobile.iscrollview-1.2.6',
            'add2Home': '../external-libs/add2home'
        },
        shim:{
            'angular':{ deps:['jquery'], exports:'angular' },
            'iscroll':{ deps:['jquery'], exports:'iscroll' },
            'iscrollview':{ deps:['jquery.mobile', 'iscroll'], exports:'iscrollview' }
        }
    });

Thanks for any help.

解决方案

Just some suggestions use empty: to indicate NOT to include the module in the optimization.

In require.config will then indicate the usage of the min file.

requirejs: {
            compile: {
                options: {
                    {{ all other settings }}
                    paths: {
                        'angular':'empty:',
                    }
                }
            }
        },

   require.config:{  
              paths:{
              'angular':'../external-libs/min/angular-1.0.4',
        },
   }

Please note, that's "empty:" with a colon at the end, not "empty". If you omit the colon, you'll get an error like this:

"No such file or directory [...]/empty.js" 

Hope this helps.

这篇关于如何从uglifying / optimize中排除某些requireJS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆