Sea.js Manual & Documentation


Table of Contents


Module Identifier

Module identifiers can be used to identify the current module being defined. Loading functions such as require, require.async take module id as their first parameter, and module identifiers are also used in the dependencies argument.

Module identifiers in SeaJS are a superset of what is allowed in CommonJS Module Identifiers:

  1. A module identifier is a String of "terms" delimited by forward slashes.
  2. A term must be a camelCase identifier, ".", or "..".
  3. Module identifiers may not have file-name extensions like ".js".
  4. Module identifiers may be "relative" or "top-level". A module identifier is "relative" if the first term is "." or "..".
  5. Top-level identifiers are resolved off the conceptual module name space root.
  6. Relative identifiers are resolved relative to the identifier of the module in which "require" is written and called.

Relative Identifier

Relative identifiers start with a dot ("."), and MUST be in a module environment. They are resolved relative to the uri of the current module:

// In http://example.com/js/a.js:
require('./b');
  // => http://example.com/js/b.js

Top-Level Identifier

Top-level identifiers do not start with a dot (".") or a slash ("/"). They are resolved relative to the conceptual namespace root. SeaJS will attempt to locate modules referenced with top-level paths relative to the base path.

// Assume base path is: http://example.com/js/libs/

// In some module factory:
require('jquery/1.7.1/jquery');
  // => http://example.com/js/libs/jquery/1.7.1/jquery.js

The default value of base is related to the path of sea.js:

If the sea.js path is:
  http://example.com/js/libs/sea.js
Then the base path is:
  http://example.com/js/libs/

When the path of sea.js contains version number, the default value of base will ignore seajs/x.y.z. This way is more friendly for hosting multiple versions of libraries.

If the sea.js path is:
  http://example.com/libs/seajs/1.0.0/sea.js
Then the base path is:
  http://example.com/libs/

Of course, you can config the base path manually.

seajs.config({
  base: 'http://code.jquery.com/'
});

// In some module factory:
require('jquery');
  // => http://code.jquery.com/jquery.js

Normal Path

All identifiers except relative and top-level identifiers are normal paths. They are resolved just like the script.src in html files.

// In http://example.com/js/main.js:
require('http://example.com/js/a');
  // => http://example.com/js/a.js

// In http://example.com/js/a.js:
require('/js/b');
  // => http://example.com/js/b.js

// In any where:
seajs.use('./c');
  // => http://example.com/path/to/page/c.js

The module identifiers in the seajs.use(ids, ...) and define(id, ...) are always normal paths, because those functions are designed to work in the global environment.

Tips About File Extensions

SeaJS always adds the file extension (".js") when attempting to locate JavaScript modules, except when a hash ("#") or question mark ("?") are present in the path. An easy way to suppress the automatic file extension is to add a hash ("#") to the end of the path.

// The ".js" extension can be omitted:
require('http://example.com/js/a');
require('http://example.com/js/a.js');
  // => http://example.com/js/a.js

// The ".css" extension can NOT be omitted:
require('http://example.com/css/a.css');
  // => http://example.com/css/a.css

// When a question mark ("?") is present, nothing will be added to the path:
require('http://example.com/js/a.json?callback=define');
  // => http://example.com/js/a.json?callback=define

// When the path ends with a hash ("#"), the hash will be ignored:
require('http://example.com/js/a.json#');
  // => http://example.com/js/a.json