SeaJS入门

SeaJS过时了?所谓的过时,并不是指现在就不能用了,而是说出现了明显更加先进的理念(或者标准),这会导致未来它的使用场景大为减少,整体趋势已经步入衰落。
知乎回答:
https://www.zhihu.com/question/34756861

还是记录下!

<span style="font-family:Microsoft YaHei;font-size:18px;"><!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
	<meta charset="utf-8" />
    <script type="text/javascript" src="../js/sea.js"></script>
 </head>
<body>
    <script>

        seajs.config({
            base: "../js/",
            alias: {
                "jquery": "../js/jquery.min.js"
            }
        });
        console.log("hello SeaJS");
        var a = seajs.use("main_seajs");
        console.log("goodbye SeaJS");
    </script>
</body>
</html></span>

入口模块, sea.js 在下载完成后,会自动加载入口模块。

<span style="font-family:Microsoft YaHei;font-size:18px;">define(function (require) {
    var m = require("test_seajs");
    m.sayhello();
});</span>

test_seajs.js里依赖test0_seajs.js

<span style="font-family:Microsoft YaHei;font-size:18px;">define(function (require, exports, module) {
    console.log("hello world in test " + new Date());
    var p = require("test0_seajs");
    p.print();
    module.exports.sayhello = function () {
        console.log("something you like in test " + new Date());
    };
});</span>

test0_seajs.js

<span style="font-family:Microsoft YaHei;font-size:18px;">define(function (require, exports, module) {
    console.log("hello world in test0 " + new Date());
    module.exports.print = function () {
        console.log("print something you like in test0 " + new Date());
    };
});</span>


控制台输出


配置

可以对 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: 'a'
});
define(function(require, exports) {
  // 此处执行时,不能保证模块 a 已经加载并执行好
});

debug Boolean

值为 true 时,加载器不会删除动态插入的 script 标签。插件也可以根据 debug 配置,来决策 log 等信息的输出。

base String

Sea.js 在解析顶级标识时,会相对 base 路径来解析。详情请参阅模块标识

seajs@2.3.0 之前,Sea.js 会根据 sea.js 的路径去猜测base 路径,一般为路径上含有 seajs 字符串的上一级路径。在seajs@2.3.0 后,去掉了这个模糊的猜测。我们推荐始终手动设置一个准确的base 路径

charset 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 标签在页面中同步引入。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值