Inline require() working in requirejs but not with optimized almond build

孤者浪人 提交于 2019-12-03 17:03:09

问题


As the title says my inline require calls are working in a non-optimized requirejs run but not working when built with grunt and almondjs.

Uncaught Error: undefined missing views/some/view

The top of any file might be:

define(
['jquery', 'app'],
function($, App) {

and later on based on business logic I want to be able to require another file

require(['views/some/view'], function(SomeView){
     console.log(SomeView);
});

I tried the alternative syntax as well:

var SomeView= require('views/some/view');

And this all works using an unbuilt requirejs version. But again it fails when I build it with grunt and almond

    requirejs: {
        compile: {
            options: {
                name: "../components/almond/almond", 
                baseUrl: "src",
                mainConfigFile: "./require.config.js",
                include: ['main'], 
                insertRequire: ['main'], // Add a require step in at the end for the main module.
                wrap: true, // Wrap everything up in a closure
                generateSourceMaps: true, // Experimental
                preserveLicenseComments: false, // Needs turned off for generateSourceMaps
                optimize: "uglify2", // Supports generateSourceMaps
                out: "assets/javascripts/build.js"
            }
        }
    },

I can get it working fine in almond if I put it up at the top of the file in a define call, but isn't it preferable in AMD to keep it lean?


回答1:


According to the Almond documentation it works best with non-dynamic loading and everything packaged into the one file.

You should be able to set "findNestedDependencies" to true in your compile options to ensure that your inline require calls are included as part of the build.



来源:https://stackoverflow.com/questions/15515872/inline-require-working-in-requirejs-but-not-with-optimized-almond-build

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