内联require()在requirejs中工作,但不在优化的杏仁构建中工作 [英] Inline require() working in requirejs but not with optimized almond build
问题描述
未捕获错误:未定义缺失视图/某些/视图
任何文件的顶部可能是:
$ b $
define(
['jquery','app'],
function($,App ){
以及稍后基于业务逻辑我希望能够要求另一个文件
require(['views / some / view'],function(SomeView){
console.log(SomeView);
});
我也尝试了另一种语法:
var SomeView = require('views / some / view');
这一切都是使用非构建的requirejs版本,但是当我用grunt和almond构建它时,它又失败了
requirejs:{
pre>
compile:{
options:{
名称:../components/almond/almond,
baseUrl:src,
mainConfigFile:./require.config.js,
include:['main '],
insertRequire:['main'],//在主模块的最后添加一个require步骤。
wrap:true,//将所有东西封装在闭包中
generateSourceMaps:true,//实验性
preserveLicenseComments:false,//需要关闭generateSourceMaps
优化:uglify2 ,//支持generateSourceMaps
out:assets / javascripts / build.js
}
}
},
如果我在定义调用中将它放在文件的顶部,我可以在杏仁中正常工作,但在AMD中保留它是否可取它依赖于它?
解决方案根据Almond文档,它非常适合非动态加载,并且所有内容都打包到一个文件中。 / p>
您应该能够在编译选项中将findNestedDependencies设置为true,以确保您的内联require调用包含在构建中。
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?
解决方案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.
这篇关于内联require()在requirejs中工作,但不在优化的杏仁构建中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!