??????? seajs.config配置可以对 Sea.js 进行配置,让模块编写、开发调试更方便。
??????? seajs.config seajs.config(options)用来进行配置的方法。
seajs.config({
// 别名配置
alias: {
'es5-safe': 'gallery/es5-safe/0.9.3/es5-safe',
'json': 'gallery/json/1.0.2/json',
'jquery': 'jquery/jquery/1.10.1/jquery'
},
// 路径配置
paths: {
'gallery': 'https://a.alipayobjects.com/gallery'
},
// 变量配置
vars: {
'locale': 'zh-cn'
},
// 映射配置
map: [
['http://example.com/js/app/', 'http://localhost/js/app/']
],
// 预加载项
preload: [
Function.prototype.bind ? '' : 'es5-safe',
this.JSON ? '' : 'json'
],
// 调试模式
debug: true,
// Sea.js 的基础路径
base: 'http://example.com/path/to/base/',
// 文件编码
charset: 'utf-8'
});
??????? 支持以下配置选项:
alias?Object
??????? 当模块标识很长时,可以使用?alias?来简化。
seajs.config({
alias: {
'jquery': 'jquery/jquery/1.10.1/jquery',
'app/biz': 'http://path/to/app/biz.js',
}
});
define(function(require, exports, module) {
var $ = require('jquery');
//=> 加载的是 http://path/to/base/jquery/jquery/1.10.1/jquery.js
var biz = require('app/biz');
//=> 加载的是 http://path/to/app/biz.js
});
??????? 使用?alias,可以让文件的真实路径与调用标识分开,有利于统一维护。
paths?Object
??????? 当目录比较深,或需要跨目录调用模块时,可以使用?paths?来简化书写。
seajs.config({
paths: {
'gallery': 'https://a.alipayobjects.com/gallery',
'app': 'path/to/app',
}
});
define(function(require, exports, module) {
var underscore = require('gallery/underscore');
//=> 加载的是 https://a.alipayobjects.com/gallery/underscore.js
var biz = require('app/biz');
//=> 加载的是 path/to/app/biz.js
});
??? paths?配置可以结合?alias?配置一起使用,让模块引用非常方便。
vars?Object
??????? 有些场景下,模块路径在运行时才能确定,这时可以使用?vars?变量来配置。
seajs.config({
vars: {
'locale': 'zh-cn'
}
});
define(function(require, exports, module) {
var lang = require('./i18n/{locale}.js');
//=> 加载的是 path/to/i18n/zh-cn.js
});
??? vars?配置的是模块标识中的变量值,在模块标识中用?{key}?来表示变量。
map?Array
??????? 该配置可对模块路径进行映射修改,可用于路径转换、在线调试等。
seajs.config({
map: [
[ '.js', '-debug.js' ]
]
});
define(function(require, exports, module) {
var a = require('./a');
//=> 加载的是 path/to/a-debug.js
});
preload?Array
??????? 使用?preload?配置项,可以在普通模块加载前,提前加载并初始化好指定模块。
// 在老浏览器中,提前加载好 ES5 和 json 模块
seajs.config({
preload: [
Function.prototype.bind ? '' : 'es5-safe',
this.JSON ? '' : 'json'
]
});
????preload?中的空字符串会被忽略掉。
??????? 注意:preload?中的配置,需要等到?use?时才加载。比如:
seajs.config({
preload: 'a'
});
// 在加载 b 之前,会确保模块 a 已经加载并执行好
seajs.use('./b');
??????? preload?配置不能放在模块文件里面:
seajs.config({
preload: '