为什么我的RequireJS忽略了我的优化main.js的code? [英] Why is my RequireJS ignoring the code in my optimized main.js?

查看:347
本文介绍了为什么我的RequireJS忽略了我的优化main.js的code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直与RequireJS了一点,但最近我全新的优化,我试图将其添加到我的构建步骤,但我遇到了一个问题。它运行并合并我的JS文件到一个后,我的主不再执行的主体(细优化之前的工作)。所有的依赖JS正确加载,但里面的实际code我的主要定义()不会(即使我可以看到它的最终输出文件这里是我的意思是:

假设的简化版本,我的 main.js 的:

 的console.log(这句话输出劝慰);
定义([jQuery的,Modernizr的,j​​query.joyride],功能($){
    的console.log(这句话不);
}

正如我所说,我会看到jQuery的,现代化主义,兜风两者的console.log()在输出文件中内嵌的语句,所以我相信这是越来越正确生成。我的猜测是,它已经与我的要求的配置做的:

这是简化版本我的 app.js 的:

  requirejs.config({
    的baseUrl:/资产/ JS
    路径:{
        jQuery的:jQuery的
    },
    垫片:{
        jquery.joyride:jQuery的]
    }
});requirejs([应用程序/主]);

我的假设是没有得到执行第二requirejs()调用。那可能吗?什么会导致这样的事情发生的,什么是正确的解决?

我基本上优化版本的 main.js 的看起来像以下内容:

 (功能(E,T){..jquery code ..};定义(jquery.joyride功能(){});
(函数(){... code在这里...});的console.log(这句话输出劝慰);
定义('主',[jQuery的,Modernizr的,j​​query.joyride],功能($){
    的console.log(这句话不);
};

下面是构建配置:

 ({
    APPDIR:../../,
    的baseUrl:资产/ JS
    mainConfigFile:../../assets/js/app.js',
    导演:'../../../BUILD/',
    optimizeCss:默认,
    模块:
        {
            名称:主
        }
    ]
    removeCombined:真实,
    preserveLicenseComments:假的,
    优化:'无'/​​/我这个残疾人帮我尝试和调试
})

此外,它有帮助,这是我使用的,包括我的JS行(前完全相同的行boht后,我优化文件):

 <脚本数据主要=/资产/ JS /应用SRC =/资产/ JS / require.js>< / SCRIPT>


解决方案

所以通过看你上面的共享code接近......问题是,main.js的优化版本没有加入正确的模块名称(定义(主[定义(应用程序/主[。我怀疑这有做的两件事之一:


  1. 在r.js配置文件错误的模块名称

  2. 在app.js不当模块参考

首先,测试/验证我的假设: manualy 编辑main.js的优化版本,以配合我上面提到的(应用程序/主的地方只是主)。如果这样的作品,然后尝试这些东西(一次一个,而不是在同一时间):


  1. 解决#1,在你的r.js配置文件...变化名称:主名称:应用程序/主

  2. #2,在你的app.js,变化 requirejs([应用程序/主]); requirejs([主]);

I've been working with RequireJS for a bit lately but I am brand new to the optimizer and I am trying to add it into my build steps but I am running into an issue. After it runs and merges my JS files into one, the body of my "main" no longer executes (worked fine prior to optimizing). All of the dependency JS loads correctly but the actual code inside of my main define() does not (even though I can see it in the final output file. Here is what I mean:

Assuming simplified version of my main.js:

console.log("This statement outputs to console");
define(["jquery", "modernizr", "jquery.joyride"], function($) {
    console.log("This statement does not");
}

As I mentioned I will see jquery, modernizer, joyride and both console.log() statements in inline in the output file so I believe it is getting generated correctly. My guess is that it has to do with my require config:

This is simplified version of my app.js:

requirejs.config({
    "baseUrl": "/assets/js",
    "paths": {
        "jquery": "jquery"
    },
    "shim": {
        "jquery.joyride": ["jquery"]
    }
});

requirejs(["app/main"]);

My assumption is that the second requirejs() call is not getting executed. Is that possible? What would cause that to happen and what is the proper fix?

Essentially my optimized version of main.js looks something like the following:

(function(e,t){ ..jquery code.. };

define("jquery.joyride", function(){});
(function(a){ ...code here... });

console.log("This statement outputs to console");
define('main',["jquery", "modernizr", "jquery.joyride"], function($) {
    console.log("This statement does not");
};

Here is by build config:

({
    appDir: '../../',
    baseUrl: "assets/js",
    mainConfigFile: '../../assets/js/app.js',
    dir: '../../../BUILD/',
    optimizeCss: 'default',
    modules: [
        {
            name: "main"
        }
    ],
    removeCombined: true,
    preserveLicenseComments: false,
    optimize: 'none' // I Disabled this to help me try and debug
})

Also it it helps, this is the line I'm using to include my JS (same exact line boht before and after I optimize the file):

<script data-main="/assets/js/app" src="/assets/js/require.js"></script>

解决方案

so by looking closer at the code you've shared above... the issue is that the optimized version of main.js is not adding the proper module name (define("main"[ should be define("app/main"[. I suspect this has to do with one of 2 things:

  1. wrong module name in the r.js config file
  2. improper module reference in your app.js

first, to test/prove my assumption: manualy edit the optimized version of main.js to match what I mentioned above ("app/main" in place of just "main"). if that works, then try these things (one at a time, not at the same time):

  1. for addressing #1, in your r.js config file... change name:"main" to name:"app/main"
  2. for #2, in your app.js, change requirejs(["app/main"]); to requirejs(["main"]);

这篇关于为什么我的RequireJS忽略了我的优化main.js的code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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