简体   繁体   English

为什么我必须在grunt-requirejs中再次使用'include'来优化成单个js文件?

[英]Why I have to use 'include' again in grunt-requirejs to optimize into a single js file?

I'm trying to use grunt-requirejs to optimize my requirejs project into a single production js file. 我正在尝试使用grunt-requirejs将我的requirejs项目优化为单个生产js文件。 However, I'm not sure if my Gruntfile.js is correct because I have to specify which model to include during the optimization again, which seems wrong to me as I specify mainConfigFile already. 但是,我不确定我的Gruntfile.js是否正确,因为我必须再次指定在优化过程中要包括的模型,这对我来说似乎是错误的,因为我已经指定了mainConfigFile。 I thought it's going to read everything from my requirejs file. 我以为它将从我的requirejs文件中读取所有内容。 I asked this because I have a lot of modules and I don't want to DRY. 我问这个问题是因为我有很多模块,而且我不想干。

Here's my Gruntfile.js 这是我的Gruntfile.js

requirejs : {

            compile : {
                options: {
                    almond: true,
                    baseUrl : 'src/Web/Scripts',
                    mainConfigFile : 'src/Web/Scripts/main-new.js',
                    out: 'src/Web/Scripts/out.js',
                    optimize: 'uglify2',

                    include: ['main-new', 
                        'app/app.js',
                        'app/viewmodels/home/home.mobile.js',
                        'zepto', 
                        'hammer',
                        'zepto-hammer',
                        'zepto-dragswipe',
                        'knockout',
                        'knockout-validation',
                        'knockout-postbox',
                        'knockout-mapping',
                        'knockout-notification',
                        'q',
                        'underscore'
                    ]
                }
            }
        },

And this is my main-new.js file 这是我的main-new.js文件

requirejs.config({
        baseUrl: '/m/Scripts',
        paths: {
            'jquery': 'vendors/jquery/jquery-1.10.2',
            'jquery-cookie': 'vendors/jquery/jquery.cookie-1.4.1',
            'zepto': 'vendors/zepto/zepto',
            'hammer': 'vendors/hammer/hammer',
            'zepto-hammer': 'vendors/zepto/zepto.hammer',
            'zepto-dragswipe': 'vendors/zepto/zepto.dragswipe',
            'knockout': 'vendors/knockout/knockout-3.1.0',
            'knockout-validation': 'vendors/knockout/knockout.validation',
            'knockout-postbox': 'vendors/knockout/knockout-postbox',
            'knockout-mapping': 'vendors/knockout/knockout.mapping',
            'knockout-notification': 'vendors/knockout/knockout-notification-1.1.0',
            'viewmodels': 'app/viewmodels',
            'service': 'app/services',
            'config': 'app/config',
            'helpers': 'app/helpers',
            'q': 'vendors/q/q-0.9.6',
            'underscore': 'vendors/underscore/underscore-min'
        },
        shim: {
            'knockout-validation': ['knockout'],
            'knockout-mapping': ['knockout'],
            'knockout-postbox': ['knockout'],
            'knockout-notification': ['knockout'],
            'jquery-cookie': ['jquery'],
            'hammer': ['zepto'],
            'zepto-hammer': ['zepto', 'hammer'],
            'zepto-dragswipe': ['zepto', 'hammer'],
            'zepto': {
                exports: 'Zepto'
            },
            'underscore': {
                exports: '_'
            }
        }
    });

In include section you should put modules that is required dynamically like: include部分中,您应该放置动态需要的模块,例如:

require(['path/' + moduleName], function() { ... })

Because r.js can't predict at build time what you will be requiring in runtime. 因为r.js无法在构建时预测运行时的要求。

Everything else should be resolved by dependency arrays in your modules. 其他所有内容都应通过模块中的依赖项数组来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用grunt-requirejs优化Require.js后不加载手把 - Handlebars not loading after Require.js optimization with grunt-requirejs 使用requirejs将项目构建为单个文件,然后对其进行优化(创建源映射) - Use requirejs to build project to single file and then optimize it (creating source map) 使用 RequireJs 和 Grunt 将 TypeScript 编译成单个文件 - Compiling TypeScript with RequireJs and Grunt into a single file 为什么我的grunt-contrib-requirejs没有优化任何内容? - Why does my grunt-contrib-requirejs does not optimize anything? 在节点上使用RequireJS来优化创建单个输出文件并不包括所有必需的文件 - Using RequireJS with node to optimize creating single output file does not include all the required files JS作为RequireJS中的单个JavaScript文件 - JS as a Single JavaScript File in RequireJS 如果我使用r.js优化RequireJS项目,是否需要更改路径和依赖项配置? - If I optimize my RequireJS project using r.js, do I have to change the path and dependency configuration? RequireJS:优化脚本并在其中包含RequireJS本身 - RequireJS: optimize scripts and include RequireJS itself in it yeoman / grunt和requirejs-不要连接(优化)输出 - yeoman / grunt and requirejs - dont concatenate (optimize) output 为什么使用requireJS代替有序包含列表? - Why use requireJS instead of an ordered include list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM