Although, your HTML pages that load steal can
exist anywhere can be served up dynamically, it's
best to have all your JavaScript, CSS, and other static
resources in the root folder.
But steal allows you to configure pretty much everything as we will see ...
Configure steal's behavior
steal.config(configOptions) allows you to
change the behavior of how steal loads modules. steal.config
allows you to set rules that:
Apply for all modules. (ex: changing the location of the root folder)
Apply for a single moduleId. (ex: 'steal/dev/dev.js' should not be added to production)
Apply to startup. (ex: load myapp/myapp.js as the first JS module)
You can find a full list of options in steal.config's docs,
but the most common configuration options are:
startFile - the first moduleId to load. Example: "myapp/myapp.js".
env - the environment the page is
running in: "development" or "production". By default, env is development.
For any application that you intend to build,
startFile and env need to be set.
You can set configOptions in a variety ways:
Set startFile and env in the script tag
You can set startFile and env the queryparams of steal like:
After steal.js is loaded and run, you can call steal.config
anywhere in the application. However, after steal.js loads,
it automatically loads stealconfig.js before it loads
anything else. stealconfig.js is the best place to
configure settings that should be applied to all
projects.
A steal object that exists before steal.js is loaded
If a steal object exists before steal.js is loaded,
steal will internally call steal.config with that
object. For example:
Use steal(ids...) to load dependent
modules. Ids might look like:
// in myapp/myapp.js
steal('components/item',
'person.js',
'./view.ejs')
Steal uses steal.id to convert the id passed to steal
into a moduleId. It then uses steal.idToUri to
convert that moduleId into a URI to load the resource.
The behavior of steal.id and steal.idToUri can
be configured by steal.config's map and
paths options. But the default behavior is
as follows:
"components/item" is found in ROOT/components/item/item.js
"person.js" is found in ROOT/person.js
"./view.ejs" is found in ROOT/myapp/view.ejs
It is possible to use:
a url like: "http://cdn.com/foo.js"
a path relative to the domain like: "/foo.js"
But, we STRONGLY encourage you to use moduleId's and steal.config
to adjust the lookup path for resources outside stealroot.
Return module values
After the optional list of moduleIds, you can pass steal
a "definition" function. For example:
The "definition" function gets called with
each dependent module's value
and can optionally return a module value of its
own. Your code goes in the definition function.
If a module doesn't return a value, undefined
is passed to the definition function.
After installing StealJS, you will have steal/steal.js. This provides the steal function that loads scripts, css, and other modules into your like:
To use steal effectively, you need to know how to:
We will go into each of these in detail, but first lets look at a quick example.
Quick Example
The following walks through the steps to create an application that loads a JavaScript and LESS module.
The resulting folder structure will look like:
Follow these steps 5:
0. Create
myapp
, its contents, andstealconfig.js
.1. Add a script tag to index.html that loads
steal/steal.js
and add the path to the first file to load in the query string like:2. In stealconfig.js, configure steal to load the less engine for any modules ending in
.less
like:3. Add the following to mymodule.js:
myapp/mymodule.js
's module value is a function that sets an element's contents and changes its class attribute to "welcome".Add the following to myapp.less:
myapp/myapp.less
adds a.welcome
style to the app.4. Add the following to myapp/myapp.js:
5. Open index.html, you should find Hello World.
Read on to understand setting up steal in detail.
Add steal.js to your page
The first step to using steal is to add
steal/steal.js
to your page.With this, you can start stealing modules. For example, you could load jQuery from a CDN in a following script tag like:
The folder that contains the
steal
folder is the rootfolder. By default, all modules are loaded from the root folder unless they start with:"/bar.js"
So the following would load
public/component.js
:Although, your HTML pages that load steal can exist anywhere can be served up dynamically, it's best to have all your JavaScript, CSS, and other static resources in the root folder.
But steal allows you to configure pretty much everything as we will see ...
Configure steal's behavior
steal.config(configOptions) allows you to change the behavior of how steal loads modules.
steal.config
allows you to set rules that:myapp/myapp.js
as the first JS module)You can find a full list of options in steal.config's docs, but the most common configuration options are:
"myapp/myapp.js"
."development"
or"production"
. By default, env is development.For any application that you intend to build,
startFile
and env need to be set.You can set configOptions in a variety ways:
Set startFile and env in the script tag
You can set startFile and env the queryparams of steal like:
For example:
If you load
steal/steal.production.js
the environment defaults to production:Call
steal.config(stealConfig)
After
steal.js
is loaded and run, you can call steal.config anywhere in the application. However, aftersteal.js
loads, it automatically loadsstealconfig.js
before it loads anything else.stealconfig.js
is the best place to configure settings that should be applied to all projects.A
steal
object that exists beforesteal.js
is loadedIf a
steal
object exists beforesteal.js
is loaded, steal will internally callsteal.config
with that object. For example:Load modules
Use
steal(ids...)
to load dependent modules. Ids might look like:Steal uses steal.id to convert the id passed to steal into a moduleId. It then uses steal.idToUri to convert that moduleId into a URI to load the resource.
The behavior of steal.id and steal.idToUri can be configured by steal.config's map and paths options. But the default behavior is as follows:
ROOT/components/item/item.js
ROOT/person.js
ROOT/myapp/view.ejs
It is possible to use:
"http://cdn.com/foo.js"
"/foo.js"
But, we STRONGLY encourage you to use moduleId's and steal.config to adjust the lookup path for resources outside stealroot.
Return module values
After the optional list of moduleIds, you can pass steal a "definition" function. For example:
The "definition" function gets called with each dependent module's value and can optionally return a module value of its own. Your code goes in the definition function.
If a module doesn't return a value, undefined is passed to the definition function.