使用r.js打包Javascript库时需要的输入
我们在小组中使用requirejs开发了一个基于主干、木偶和把手的内部UI小部件库。我们希望将此库分发给我们组织中的其他组,这些组的应用程序不使用require或任何其他AMD兼容的模块加载机制 目标使用r.js打包Javascript库时需要的输入,javascript,backbone.js,requirejs,r.js,Javascript,Backbone.js,Requirejs,R.js,我们在小组中使用requirejs开发了一个基于主干、木偶和把手的内部UI小部件库。我们希望将此库分发给我们组织中的其他组,这些组的应用程序不使用require或任何其他AMD兼容的模块加载机制 目标 使用r.js构建一个具体的、缩小的js文件,该文件只包含我们的库代码,而不包含主干、把手等依赖项,因为其他应用程序已经包含了它们 使用杏仁,这样就没有必要了 根据我所读到的一切,这似乎是可能的,尽管我很难开始 问题一 “empty:”指定仅适用于主干、木偶和jQuery。如果我重新引入构建文件
- 使用r.js构建一个具体的、缩小的js文件,该文件只包含我们的库代码,而不包含主干、把手等依赖项,因为其他应用程序已经包含了它们
- 使用杏仁,这样就没有必要了李>
Tracing dependencies for: main
TypeError: string is not a function
In module tree:
main
modal
Modal/javascript/controllers/modal.simple.controller
Modal/javascript/views/modal.simple.views
hbs
Error: TypeError: string is not a function
In module tree:
main
modal
Modal/javascript/controllers/modal.simple.controller
Modal/javascript/views/modal.simple.views
hbs
我的构建文件如下所示:
({
baseUrl: '.',
name: 'main',
out: 'uitoolkit.js',
mainConfigFile: 'main.js',
paths: {
'jquery': 'empty:',
'backbone': 'empty:',
'marionette': 'empty:'
/* ,
'underscore': 'empty:',
'handlebars': 'empty:',
'hbs': 'empty:',
'json2': 'empty:',
'i18nprecompile': 'empty:'
*/
}
})
require.config({
locale : "en_us",
// default plugin settings, listing here just as a reference
hbs : {
templateExtension : 'hbs',
// if disableI18n is `true` it won't load locales and the i18n helper
// won't work as well.
disableI18n : true
},
paths: {
'modal': 'Modal/javascript/widget',
'notifications': 'Notifications/javascript/widget',
'select': 'Select/javascript/widget',
'wizard': 'Wizard/javascript/widget',
'jquery': 'bower_components/jquery/jquery',
'backbone': 'bower_components/backbone/backbone',
'underscore': 'bower_components/underscore/underscore',
'handlebars': 'bower_components/handlebars/handlebars',
'marionette': 'bower_components/backbone.marionette/lib/backbone.marionette',
'hbs' : 'bower_components/require-handlebars-plugin/hbs',
'json2':'bower_components/json2/json2',
'i18nprecompile' : 'bower_components/require-handlebars-plugin/hbs/i18nprecompile',
'application': 'app'
},
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: [
'underscore',
'jquery'
],
exports: 'Backbone'
},
marionette: {
deps : ['jquery', 'underscore', 'backbone', 'handlebars'],
exports: 'Marionette'
},
handlebars: {
exports: 'Handlebars',
init: function() {
this.Handlebars = Handlebars;
return this.Handlebars;
}
},
json2: {
exports: 'JSON'
},
application: {
exports: 'Application'
},
},
baseUrl : './'
});
require(
[
'application',
'modal',
'notifications',
'select',
'wizard'
],
function(Application, modal, notifications, select, wizard) {
var uitoolkit = $.namespace('com.namespace.uitoolkit');
uitoolkit.modal = modal;
uitoolkit.notifications = notifications;
uitoolkit.select = select;
uitoolkit.wizard = wizard;
return uitoolkit;
}
);
// stubs/jquery:
define(function() {
return window.jQuery;
}
// stubs/underscore:
define(function() {
return window._;
}
// stubs/backbone:
define(function() {
return window.Backbone;
}
({
baseUrl: '.',
name: 'bower_components/almond/almond',
include: ['main'],
out: 'uitoolkit.js',
mainConfigFile: 'main.js',
// optimize: 'none',
wrap: true,
paths: {
'jquery': './js/stubs/jquery',
'backbone': './js/stubs/backbone',
'marionette': './js/stubs/marionette',
'underscore': './js/stubs/underscore',
'handlebars': './js/stubs/handlebars',
'json2': './js/stubs/json2'
}
})
main.js如下所示:
({
baseUrl: '.',
name: 'main',
out: 'uitoolkit.js',
mainConfigFile: 'main.js',
paths: {
'jquery': 'empty:',
'backbone': 'empty:',
'marionette': 'empty:'
/* ,
'underscore': 'empty:',
'handlebars': 'empty:',
'hbs': 'empty:',
'json2': 'empty:',
'i18nprecompile': 'empty:'
*/
}
})
require.config({
locale : "en_us",
// default plugin settings, listing here just as a reference
hbs : {
templateExtension : 'hbs',
// if disableI18n is `true` it won't load locales and the i18n helper
// won't work as well.
disableI18n : true
},
paths: {
'modal': 'Modal/javascript/widget',
'notifications': 'Notifications/javascript/widget',
'select': 'Select/javascript/widget',
'wizard': 'Wizard/javascript/widget',
'jquery': 'bower_components/jquery/jquery',
'backbone': 'bower_components/backbone/backbone',
'underscore': 'bower_components/underscore/underscore',
'handlebars': 'bower_components/handlebars/handlebars',
'marionette': 'bower_components/backbone.marionette/lib/backbone.marionette',
'hbs' : 'bower_components/require-handlebars-plugin/hbs',
'json2':'bower_components/json2/json2',
'i18nprecompile' : 'bower_components/require-handlebars-plugin/hbs/i18nprecompile',
'application': 'app'
},
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: [
'underscore',
'jquery'
],
exports: 'Backbone'
},
marionette: {
deps : ['jquery', 'underscore', 'backbone', 'handlebars'],
exports: 'Marionette'
},
handlebars: {
exports: 'Handlebars',
init: function() {
this.Handlebars = Handlebars;
return this.Handlebars;
}
},
json2: {
exports: 'JSON'
},
application: {
exports: 'Application'
},
},
baseUrl : './'
});
require(
[
'application',
'modal',
'notifications',
'select',
'wizard'
],
function(Application, modal, notifications, select, wizard) {
var uitoolkit = $.namespace('com.namespace.uitoolkit');
uitoolkit.modal = modal;
uitoolkit.notifications = notifications;
uitoolkit.select = select;
uitoolkit.wizard = wizard;
return uitoolkit;
}
);
// stubs/jquery:
define(function() {
return window.jQuery;
}
// stubs/underscore:
define(function() {
return window._;
}
// stubs/backbone:
define(function() {
return window.Backbone;
}
({
baseUrl: '.',
name: 'bower_components/almond/almond',
include: ['main'],
out: 'uitoolkit.js',
mainConfigFile: 'main.js',
// optimize: 'none',
wrap: true,
paths: {
'jquery': './js/stubs/jquery',
'backbone': './js/stubs/backbone',
'marionette': './js/stubs/marionette',
'underscore': './js/stubs/underscore',
'handlebars': './js/stubs/handlebars',
'json2': './js/stubs/json2'
}
})
问题二
我甚至不知道从哪里开始介绍杏仁。这是我将包含在main.js中的内容吗?这会允许我所认为的,也就是说,给我们一个选择,向不使用require/AMD的开发人员分发一个库吗
非常、非常提前地感谢您。最后从那边一个非常好的家伙那里得到了关于requirejs列表的答案 由于我不能在自己的构建中包括jQuery、主干网等,因此我最终创建了一系列类似以下的“存根”模块:
({
baseUrl: '.',
name: 'main',
out: 'uitoolkit.js',
mainConfigFile: 'main.js',
paths: {
'jquery': 'empty:',
'backbone': 'empty:',
'marionette': 'empty:'
/* ,
'underscore': 'empty:',
'handlebars': 'empty:',
'hbs': 'empty:',
'json2': 'empty:',
'i18nprecompile': 'empty:'
*/
}
})
require.config({
locale : "en_us",
// default plugin settings, listing here just as a reference
hbs : {
templateExtension : 'hbs',
// if disableI18n is `true` it won't load locales and the i18n helper
// won't work as well.
disableI18n : true
},
paths: {
'modal': 'Modal/javascript/widget',
'notifications': 'Notifications/javascript/widget',
'select': 'Select/javascript/widget',
'wizard': 'Wizard/javascript/widget',
'jquery': 'bower_components/jquery/jquery',
'backbone': 'bower_components/backbone/backbone',
'underscore': 'bower_components/underscore/underscore',
'handlebars': 'bower_components/handlebars/handlebars',
'marionette': 'bower_components/backbone.marionette/lib/backbone.marionette',
'hbs' : 'bower_components/require-handlebars-plugin/hbs',
'json2':'bower_components/json2/json2',
'i18nprecompile' : 'bower_components/require-handlebars-plugin/hbs/i18nprecompile',
'application': 'app'
},
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: [
'underscore',
'jquery'
],
exports: 'Backbone'
},
marionette: {
deps : ['jquery', 'underscore', 'backbone', 'handlebars'],
exports: 'Marionette'
},
handlebars: {
exports: 'Handlebars',
init: function() {
this.Handlebars = Handlebars;
return this.Handlebars;
}
},
json2: {
exports: 'JSON'
},
application: {
exports: 'Application'
},
},
baseUrl : './'
});
require(
[
'application',
'modal',
'notifications',
'select',
'wizard'
],
function(Application, modal, notifications, select, wizard) {
var uitoolkit = $.namespace('com.namespace.uitoolkit');
uitoolkit.modal = modal;
uitoolkit.notifications = notifications;
uitoolkit.select = select;
uitoolkit.wizard = wizard;
return uitoolkit;
}
);
// stubs/jquery:
define(function() {
return window.jQuery;
}
// stubs/underscore:
define(function() {
return window._;
}
// stubs/backbone:
define(function() {
return window.Backbone;
}
({
baseUrl: '.',
name: 'bower_components/almond/almond',
include: ['main'],
out: 'uitoolkit.js',
mainConfigFile: 'main.js',
// optimize: 'none',
wrap: true,
paths: {
'jquery': './js/stubs/jquery',
'backbone': './js/stubs/backbone',
'marionette': './js/stubs/marionette',
'underscore': './js/stubs/underscore',
'handlebars': './js/stubs/handlebars',
'json2': './js/stubs/json2'
}
})
然后我在构建文件中引用这些。它们是由r.js内联的,然后我可以在没有require的环境中发布一个使用require(嗯,almond)的库。另外,更改构建文件以使其指向模块的真实版本也很简单。现在构建文件如下所示:
({
baseUrl: '.',
name: 'main',
out: 'uitoolkit.js',
mainConfigFile: 'main.js',
paths: {
'jquery': 'empty:',
'backbone': 'empty:',
'marionette': 'empty:'
/* ,
'underscore': 'empty:',
'handlebars': 'empty:',
'hbs': 'empty:',
'json2': 'empty:',
'i18nprecompile': 'empty:'
*/
}
})
require.config({
locale : "en_us",
// default plugin settings, listing here just as a reference
hbs : {
templateExtension : 'hbs',
// if disableI18n is `true` it won't load locales and the i18n helper
// won't work as well.
disableI18n : true
},
paths: {
'modal': 'Modal/javascript/widget',
'notifications': 'Notifications/javascript/widget',
'select': 'Select/javascript/widget',
'wizard': 'Wizard/javascript/widget',
'jquery': 'bower_components/jquery/jquery',
'backbone': 'bower_components/backbone/backbone',
'underscore': 'bower_components/underscore/underscore',
'handlebars': 'bower_components/handlebars/handlebars',
'marionette': 'bower_components/backbone.marionette/lib/backbone.marionette',
'hbs' : 'bower_components/require-handlebars-plugin/hbs',
'json2':'bower_components/json2/json2',
'i18nprecompile' : 'bower_components/require-handlebars-plugin/hbs/i18nprecompile',
'application': 'app'
},
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: [
'underscore',
'jquery'
],
exports: 'Backbone'
},
marionette: {
deps : ['jquery', 'underscore', 'backbone', 'handlebars'],
exports: 'Marionette'
},
handlebars: {
exports: 'Handlebars',
init: function() {
this.Handlebars = Handlebars;
return this.Handlebars;
}
},
json2: {
exports: 'JSON'
},
application: {
exports: 'Application'
},
},
baseUrl : './'
});
require(
[
'application',
'modal',
'notifications',
'select',
'wizard'
],
function(Application, modal, notifications, select, wizard) {
var uitoolkit = $.namespace('com.namespace.uitoolkit');
uitoolkit.modal = modal;
uitoolkit.notifications = notifications;
uitoolkit.select = select;
uitoolkit.wizard = wizard;
return uitoolkit;
}
);
// stubs/jquery:
define(function() {
return window.jQuery;
}
// stubs/underscore:
define(function() {
return window._;
}
// stubs/backbone:
define(function() {
return window.Backbone;
}
({
baseUrl: '.',
name: 'bower_components/almond/almond',
include: ['main'],
out: 'uitoolkit.js',
mainConfigFile: 'main.js',
// optimize: 'none',
wrap: true,
paths: {
'jquery': './js/stubs/jquery',
'backbone': './js/stubs/backbone',
'marionette': './js/stubs/marionette',
'underscore': './js/stubs/underscore',
'handlebars': './js/stubs/handlebars',
'json2': './js/stubs/json2'
}
})
太长,读不下去了我希望找到一种方法告诉require一些资源将在require之外加载,即主干、jQuery、下划线等将加载到HTML页面本身,而不是通过AMD。有办法做到这一点吗?需要弄清楚我的r.js构建和almond的含义。谢谢。我建议您在构建配置文件中使用选项,而不是覆盖路径,以排除不需要的模块。