5 分钟上手 Sea.js

为何使用 Sea.js ?

Sea.js 简单、天然的代码书写和组织方式javascript

兼容性好css

技术比较成熟,运用比较广泛。html

目录结构:java

examples/
  |-- sea-modules      存放 seajs、jquery 等文件,这也是模块的部署目录
  |-- static           存放各个项目的 js、css 文件
  |     |-- hello
  |     |-- lucky
  |     `-- todo
  `-- app              存放 html 等文件
        |-- hello.html
        |-- lucky.html
        `-- todo.html

在页面中记载模块:
jquery

在 hello.html 页尾,经过 script 引入 sea.js 后,有一段配置代码:app

// seajs 的简单配置
  seajs.config({
  base: "../sea-modules/",
  alias: {
    "jquery": "jquery/jquery/1.10.1/jquery.js"
  }})
  // 加载入口模块
  seajs.use("../static/hello/src/main")

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

代码模块:

这个小游戏有两个模块 spinning.js 和 main.js,遵循统一的写法:spa

// 全部模块都经过 define 来定义
define(function(require, exports, module) {

  // 经过 require 引入依赖
  var $ = require('jquery');
  var Spinning = require('./spinning');

  // 经过 exports 对外提供接口
  exports.doSomething = ...

  // 或者经过 module.exports 提供整个接口
  module.exports = ...
  });

上面就是 Sea.js 推荐的 CMD 模块书写格式。code