能够对 Sea.js 进行配置,让模块编写、开发调试更方便。html
seajs.config(options)
用来进行配置的方法。jquery
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'});
支持如下配置选项:git
Object
当模块标识很长时,可使用 alias
来简化。github
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
,可让文件的真实路径与调用标识分开,有利于统一维护。json
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
配置一块儿使用,让模块引用很是方便。app
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}
来表示变量。ui
Array
该配置可对模块路径进行映射修改,可用于路径转换、在线调试等。this
seajs.config({ map: [ [ '.js', '-debug.js' ] ]});
define(function(require, exports, module) { var a = require('./a'); //=> 加载的是 path/to/a-debug.js});
更多用法可参考:调试实践
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: 'a'});define(function(require, exports) { // 此处执行时,不能保证模块 a 已经加载并执行好});
Boolean
值为 true
时,加载器不会删除动态插入的 script 标签。插件也能够根据 debug 配置,来决策 log 等信息的输出。
String
Sea.js 在解析顶级标识时,会相对 base
路径来解析。详情请参阅 模块标识
注意:通常请不要配置 base
路径,把 sea.js
放在合适的路径每每更简单一致。
String | Function
获取模块文件时,<script>
或 <link>
标签的 charset
属性。 默认是 utf-8
charset
还能够是一个函数:
seajs.config({ charset: function(url) { // xxx 目录下的文件用 gbk 编码加载 if (url.indexOf('http://example.com/js/xxx') === 0) { return 'gbk'; } // 其余文件用 utf-8 编码 return 'utf-8'; }});
seajs.config
能够屡次运行,每次运行时,会对配置项进行合并操做:
seajs.config({ alias: { 'jquery': 'path/to/jquery.js', 'a': 'path/to/a.js' }, preload: ['seajs-text']});
seajs.config({ alias: { 'underscore': 'path/to/underscore.js', 'a': 'path/to/biz/a.js' }, preload: ['seajs-combo']});
上面两处 config
运行的结果是:
alias = { 'jquery': 'path/to/jquery.js', 'underscore': 'path/to/underscore.js', 'a': 'path/to/biz/a.js' }; preload = ['seajs-text', 'seajs-combo'];
即:config
会自动合并不存在的项,对存在的项则进行覆盖。
插件能够给 Sea.js 添加配置项,请查看具体插件了解相关配置。
配置能够直接写在 html 页面上,也能够独立出来成为一个文件。
config.js
seajs.config({ ...});
独立成一个文件时,通常经过 script 标签在页面中同步引入。
经常使用的配置项是 alias
、paths
、base
,其余配置项有须要时,来查查文档就会用了。