Why is my RequireJS ignoring the code in my optimized main.js?

我的梦境 提交于 2019-12-25 02:34:28

问题


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>

回答1:


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"]);


来源:https://stackoverflow.com/questions/21396579/why-is-my-requirejs-ignoring-the-code-in-my-optimized-main-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!